(function(jQuery) {
	jQuery.fn.homecart = function(options) {
		var defaults = {
				animateInSpeed: 500,	/* speed used to open the cart */
				animateOutSpeed: 300,	/* speed used to close the cart */
				clickOpenPercent: 0.5,	/* percentage of animateInSpeed to open the cart when the header is clicked */
				startColor: "#172641", 	/* beginning background color of div.cartHeading */
				endColor: "#ced011",	/* ending background color of div.cartHeading */
				mouseOutBuffer: 250, 	/* number in ms to delay before closing the cart. if the user moves back into the cart within this time, it is not closed */
				overExtendPadding: 20	/* amount in pixels to extend the cart over its original height */
			},
			setTimeout = window.setTimeout,
			clearTimeout = window.clearTimeout;
		
		if (options) {
			jQuery.extend(defaults, options);
		}
		
		return this.each(function() {
			var $this = jQuery(this),
				$heading = $this.find(".cartHeading"),
				$headingBG = $this.find(".cartHeadingBg"),
				$cart = $this.next(".cart"),
				$close = $cart.find(".close"),
				cartHeight = $cart.innerHeight(),
				cartOut = false,
				pinned = false,
				opening = false,
				
				openCart = function(speed) {
					
					var inSpeed = speed || defaults.animateInSpeed,
						shadowSpeed = parseInt((inSpeed * (2/3)), 10);
					
					opening = true;
					
					if (($cart.queue().length + $heading.queue().length) > 0) {
						inSpeed = parseInt((inSpeed * 0.5), 10);
					}
					
					$heading.stop(true, false);
					$headingBG.stop(true, false);
					$cart.stop(true, false);
					
					$headingBG.animate({ opacity: 1 }, inSpeed);
					$headingBG.show();
								
					$heading.animate({ 
//						backgroundColor: defaults.endColor,
						boxShadow: "0px 0 10px rgba(0,0,0,0.2)"
					}, inSpeed, function() {
						$cart.animate({
							top: 100
						}, inSpeed, function() {
							$cart.animate({ boxShadow: "0 0 10px 0px rgba(0,0,0,0.2)" }, shadowSpeed);
							$heading.animate({ boxShadow: "0px 0px 0px rgba(0,0,0,0.2)" }, shadowSpeed, function() {
								opening = false;
								cartOut = true;
							});
						});
					});
				},
				
				closeCart = function() {
					var outSpeed = defaults.animateOutSpeed;
					
					cartOut = opening = pinned = false;
					
					$heading.stop(true, false);
					$headingBG.stop(true, false);
					$cart.stop(true, false);
					
					$cart.animate({
						boxShadow: "0 0 0 #000",
						top: -cartHeight
					}, outSpeed, function() {
//						$heading.animate({ backgroundColor: defaults.startColor, boxShadow: "0 0 0 #172641" }, outSpeed);
						$heading.animate({ boxShadow: "0 0 0 #172641" }, outSpeed);
						
						$headingBG.animate({ opacity: 0 }, outSpeed);
						
					});
				},
				
				checkClose = function() {
					if (!pinned && (cartOut || opening)) {
						timeoutID = setTimeout(function() {
							closeCart();
						}, defaults.mouseOutBuffer);
					}
				},
				
				timeoutID;
			
			/* setup cart */
			cartHeight = jQuery("div.Middle").innerHeight() < cartHeight ? jQuery("div.Middle").innerHeight() : cartHeight;
			$cart.css("top", -cartHeight);
			$cart.show();
			$headingBG.hide();
			
			$heading.mouseenter(function() {
				clearTimeout(timeoutID);
				if (!cartOut && !opening) {
					openCart();
				}
			});
			
			$cart.mouseenter(function() {
				clearTimeout(timeoutID);
			});

			$cart.mouseleave(function(e) {
				checkClose();
			});
			
			$heading.mouseleave(function(e) {
				checkClose();
			});
			
			$heading.click(function() {
				clearTimeout(timeoutID);
				if (!pinned) {
					pinned = true;
					if (!cartOut) {
						openCart(parseInt((defaults.animateInSpeed * defaults.clickOpenPercent), 10));
					}
				} else if (cartOut && pinned) {
					closeCart();
				}
			});
			
			$cart.click(function(){
				if (cartOut && !pinned) {
					pinned = true;
				}
			});
			
			$close.click(function() {
				if (cartOut) {
					closeCart();
				}
			});
		});
	};
})(jQuery);

