/**
*	Small shopping cart.
*/
var ShoppingCart = function(change_quantity,
							add_product,
							removeProduct
							)
{
	Number.prototype.formatPrice = function()
	{
		return Number(this.toFixed(2).toString().replace(".00", ""));
	}
	
	/**
	*	Replace the remove from cart links with ajax stuff.
	*/
	var replaceRFC = function()
	{
		var removeFromCartRX = new RegExp("removeFromCart&PARAM=([0-9]+)");
		
		jQuery(".shoppingCart .remove").each(function(i, link) 
		{
			link	= jQuery(link);
			var id	= removeFromCartRX.exec(link.attr("href"));
		
			if(id)
			{
				id = Number(id[1]);
				if(!isNaN(id))
				{
					link.attr("href", "javascript:void(null)").data("product_id", id).click(function()
					{
						link.parent().append("<span class=\"shoppingCartLoading\" style=\"float:left\"></span>").end().hide();
						
						jQuery.post(removeProduct, {removeFromCart:jQuery(this).data("product_id")}, function(data)
						{
							var price = Number(link.parent().find(".price").text());
							if(!isNaN(price))
							{
								total = jQuery(".shoppingCartTotal");
								total.text(Number(Number(total.text()) - price).formatPrice())
							}
							link.parent().slideUp("fast", function() {link.parent().remove();});
						});
					});
				}
			}
		});
	}
	
	/**
	*	Change the quantity of a product.
	*/
	var changeQuantity = function()
	{
		var box = jQuery(this);
		var q	= Number(box.val());
		
		if(!isNaN(q) && q > 0 && box.data("q") != q)
		{
			var product = Number(box.attr("id").substr(1));
			if(!isNaN(product))
			{
				jQuery.post(change_quantity, {product:product, q:q}, function(data)
				{
					var price = box.parent().find(".price");
					var oldPrice = Number(price.text());
					var newPrice = Number(price.text()/box.data("q")*q).formatPrice();
					
					if(!isNaN(newPrice) && !isNaN(oldPrice))
					{
						price.text(newPrice.formatPrice());
						box.data("q", q);
						
						var total = jQuery(".shoppingCartTotal");
						var oldTotal = Number(total.text());
						
						if(!isNaN(oldTotal))
						{
							total.text(Number(oldTotal - oldPrice + newPrice).formatPrice());
						}
					}
				});
			}
		}
	}
	
	/**
	*	Set the quantity on focus
	*/
	var setQ = function()
	{
		jQuery(this).data("q", jQuery(this).val());
	}
	
	/**
	*	Add a product to the cart
	*/
	var addToCart = function()
	{		
		var link	= jQuery(this);
		var product	= link.data("product_id");

		link.hide().after("<span class=\"shoppingCartLoading\"></span>");

		jQuery.post(add_product, {addToCart:product}, function(data) 
		{
			if(data.charAt(0) == "{")	//If we have a JSON response
			{
				var data	= eval("(" + data + ")");
				var li		= jQuery(".shoppingCart li#p" + data.product);
				
				var price		= li.find(".price");
				var quantity	= li.find(".quantity");
				var oldPrice	= Number(price.text());
				var newPrice	= Number(price.text()/quantity.val()*data.quantity);
				
				price.text(newPrice.formatPrice());
				quantity.val(data.quantity).effect("highlight", {color:"#C3BD3E"}, "slow");
				
				var total		= jQuery(".shoppingCartTotal");
				var oldTotal	= Number(total.text());
					
				if(!isNaN(oldTotal) && !isNaN(oldPrice))
				{
					total.text(Number(oldTotal - oldPrice + newPrice).formatPrice());
				}
			}
			else
			{
				var rx = new RegExp("<li.*rel=\"(.*?)\".*>");
				var name = rx.exec(data);
				var added = false;

				if(name)
				{
					name = name[1];
					jQuery(".shoppingCart li").each(function(i, li)
					{
						li = jQuery(li);
						if(li.attr("rel") > name)
						{
							added = true;
							li.before(data);
							return false;
						}
					});
				}
				
				if(!added)
				{
					jQuery(".shoppingCart").append(data);
				}
				
				var li = jQuery(".shoppingCart li#p" + product).slideDown("fast", function() {jQuery(this).effect("highlight", {color:"#C3BD3E"}, "slow");});
				replaceRFC();

				li.find(".quantity").keyup(changeQuantity).one("focus", setQ).click(selectQ);
				var price = Number(li.find(".price").text());
				
				if(!isNaN(price))
				{
					total = jQuery(".shoppingCartTotal");
					total.text(Number(Number(total.text()) + price).formatPrice());
				}
			}
			
			link.one("click", addToCart);
			jQuery(".shoppingCartLoading").remove();
			link.show();
		});
	}
	
	/**
	*	Replace any links for adding products with the ajaxified ones
	*/
	this.replaceLinks = function(searchStr)
	{
		var reg = new RegExp("addToCart=([0-9]+)");
		
		var links = jQuery(searchStr).each(function(i, link) 
		{ 
			link = jQuery(link);
			var id = Number(reg.exec(link.attr("href"))[1]);
			
			if(!isNaN(id))
			{
				link.attr("href", "javascript:void(null)").one("click", addToCart).data("product_id", id);
			}
		});
	}
	
	function selectQ()
	{
		jQuery(this).select();
	}
	
	//Initialize this thing
	replaceRFC();
	jQuery(".shoppingCart .quantity").keyup(changeQuantity).one("focus", setQ).click(selectQ);
}