
// cart function instance.
cart = {
	
	products : [],
	
	add : function(info)
	{
		// alert("Adding product: " + info['name']);
		
		var cImgOffset = $("#cartImage").offset(),
			voucherOffset = $("#voucher").offset(),
			voucherDim = {'width' : $("#voucher").width(), 'height' : $("#voucher").height() },
			
			// determine center of voucher block
			voucherCenter =
			{ 	top : voucherOffset['top'] + (voucherDim['height'] / 2) - 24,
				left : voucherOffset['left'] + (voucherDim['width'] / 2) - 24 }; 

		// put at cartimage position	
		$("#imgAnimationBlock").css(
			{
				left : "" + (cImgOffset['left'] + 20) + "px", 
				top : "" + cImgOffset['top'] + "px", 
				display : "none"
			});
		
		// fade in fast
		$("#imgAnimationBlock").fadeIn("fast",
			
				// after fade in move to #voucher center
				function()
				{
					$("#imgAnimationBlock").animate(
						{
							left : voucherCenter['left'], 
							top : voucherCenter['top']
						},
						1000,
						
						// after animation, fade out and highlight voucher background
						function()
						{
							$("#imgAnimationBlock").fadeOut("slow");

							// add product information
							cart.products.push(info);
							
							cart.setupCartHtml();
						}
					);
				}
			);
	},
	
	
	priceString : function(val)
	{
		return "&euro; " + Math.floor(val / 100) + "," + (val % 100 < 10 ? "0" : "") + val % 100;		
	},
	
	
	setupCartHtml : function()
	{
		$("#voucher").css("backgroundColor", "#EAFFA4");
		$("#voucher").animate({"backgroundColor" : "#FFFFFF"}, 1000); 

		if (cart.products.length == 0)
		{
			$("#vContent").html("<em style='color: silver;'>Geen producten in winkelwagen</em>");
			return;
		}
		
		var content = "";
		var totalPrice = 0;
		
		// setup the contents of the voucher using the content of the products list
		for (var key in cart.products)
		{
			var pInfo = cart.products[key];
			var price = "&euro; " + pInfo.price / 100 + "," + (pInfo.price % 100 < 10 ? "0" : "") + pInfo.price % 100;

			totalPrice += pInfo.price;
			
			content += 
				"<div class='cartElement'>" +
					"<div class='price'>" + 
						cart.priceString(pInfo.price) + "<br>" +
						"<a onclick='cart.removeProduct(\"" + key + "\")' href=\"#\"><img src='images/basket_delete.png'></a>" +
					"</div>" +
					"<span class='name'>" + pInfo.label + "</span>" +
				"</div>";
		}
		
		// add total price
		content += 
			"<div style='border-top: 1px solid green; padding-top: 3px;'>" + 
				"<div class='price' style='color: green; font-weight: bold;'>" +
					cart.priceString(totalPrice) + "<br>" +
				"</div>" +
				"<strong style='color: green;'>Totaal (per jaar):</strong></div>";
		
		
		$("#vContent").html(content);
	},
	
	removeProduct : function(key)
	{
		cart.products.splice(key, 1);
		cart.setupCartHtml();
	}
};
