﻿function AddToCart(button, itemId)
{
	SetLoadingButton(button);
	
	var url = "available.aspx";
	var params = "callback=addToCart&itemId=" + itemId;

	var ajax = new Ajax.Request( url, { method: 'get', parameters: params, onComplete: AddToCartComplete, onException: function(request, e){throw(e);}});

	button.blur();
}

function SetLoadingButton(button)
{
	button.originalHtml = button.innerHTML;
	button.innerHTML = "Adding ...";
}

function AddToCartComplete(request)
{
	if(request.status == "200")
	{
		var XmlResponse = request.responseXML.getElementsByTagName("ajaxResponse")[0];
		var itemId = XmlResponse.getAttribute("itemId");
		var cartButton = $("cartButton_" + itemId);

		if(XmlResponse.getAttribute("status") == "Success")
		{			
			// animate the add
			AnimateAddToCardBox(XmlResponse.getAttribute("itemId"), XmlResponse.getAttribute("cartQuantity"));
				
			var cartItemQuantity = $("cartItemQuantity_" + XmlResponse.getAttribute("itemId"));
			
			if(cartItemQuantity)
			{
				cartItemQuantity.innerHTML = XmlResponse.getAttribute("cartItemQuantity");
				cartItemQuantity.style.backgroundColor = "#ffe762";
				setTimeout(function() {cartItemQuantity.style.backgroundColor = "";}, 800);
			}
			
			//cartButton.innerHTML = cartButton.originalHtml;
			cartButton.className += " LinkButton_disabled";
			cartButton.innerHTML = "In Cart"
			cartButton.onclick = null;
			cartButton.removeAttribute("href");
			
			var itemAddedToCart = $("itemAddedToCart");
			if(itemAddedToCart)
				itemAddedToCart.style.visibility = "visible";
		}
		else
		{
			if(XmlResponse.getAttribute("errorCode"))
			{
				if(XmlResponse.getAttribute("errorCode") == "itemUnavailable")
				{
					if($("itemQuantity_" + itemId))
						$("itemQuantity_" + itemId).innerHTML = 0;
					
					cartButton.className += " LinkButton_disabled";
					cartButton.innerHTML = "Out of Stock"
					cartButton.onclick = null;
				}
			}
			alert(XmlResponse.getAttribute("errorMessage"));
		}
	}
	else
	{
		alert("Error submitting data");
	}
}

function AnimateAddToCardBox(itemId, cartQuantity)
{
	// try and use the item as the zoom from location
	var itemBox = $("item_" + itemId);
	// if the item is not available then use the cart button
	if(!itemBox)
	{
		itemBox = $("cartButton_" + itemId);
	}
	var itemBoxLocation = FindTargetLocation(itemBox);

	var box = document.createElement("div");
	box.className = "MovingCartBox";
	box.style.width = itemBox.offsetWidth + "px";
	box.style.height = itemBox.offsetHeight + "px";

	box.style.top = itemBoxLocation.y + "px";
	box.style.left = itemBoxLocation.x + "px";

	document.body.appendChild(box);

	var cart = $("header_cartQuantity");
	var cartLocation = FindTargetLocation(cart);
	
	var yTarget = cartLocation.y;
	
	// if the cart is above the top of the visible page then go to the visible page top
	if(yTarget < document.documentElement.scrollTop)
	{
		yTarget = document.documentElement.scrollTop;
	}
	
	var addToCartAnimation = new YAHOO.util.Anim(box, { width: { to: 0 }, height: { to: 0 }, top: { to: yTarget }, left: { to: cartLocation.x }}, .5);
	addToCartAnimation.onComplete.subscribe(function() { cart.innerHTML = cartQuantity; document.body.removeChild(box);  });
	addToCartAnimation.animate();
	
	cart.parentNode.parentNode.style.visibility = "visible";
}

