$(document).ready(function() {

	$("#dialog").dialog({
		bgiframe: true,
		height: 140,
		modal: true,
		autoOpen: false
	});
	$('.displayPrice').each(function() {
	    var x = Number($(this).html()).toFixed(2);
	    $(this).val(x).text(x);
	});	
	
	$(".addtocart").click(function() {
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		var subproductIDVal			= $("#subproducts option:selected").val();
		
		var productX 		= $("#productImageWrapID_" + productIDVal).offset().left;
		var productY 		= $("#productImageWrapID_" + productIDVal).offset().top;

		var basketX 		= $("#basket").offset().left;
		var basketY 		= $("#basket").offset().top;
		
		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;
		
		var newImageWidth 	= $("#productImageWrapID_" + productIDVal+" img").width() / 3;
		var newImageHeight	= $("#productImageWrapID_" + productIDVal+" img").height() / 3;

		$("#productImageWrapID_" + productIDVal + " img:first")
		.clone()
		.css({'margin-left' : '100px'})
		.prependTo("#productImageWrapID_" + productIDVal)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.5}, 100 )
		.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() { 
			$.ajax({  
				type: "POST",  
				url: $(".addtocart").attr('href'), 
				data: "productid="+ productIDVal +"&subproductid="+ subproductIDVal,  
				success: function(theResponse) {
					updatecart();
					$("#productImageWrapID_" + productIDVal + " img:first").remove();
				}  
			});
		})
	});
	
	$(".amountm").click(function() {	
		var productIDSplitter 	= (this.id).split("_");
		var productID 			= productIDSplitter[1];
		var amount				= parseFloat($("#amount_" + productID).attr("value"));
		if($(this).attr('name')=="up")
		{
			newamount = amount + 1;
		}
		else
		{
			newamount = amount - 1;
		}
		$("#amount_" + productID).attr("value", newamount);
		changeamount(productID, newamount)
	});
	
	$(".subproducts").change(function() {
		var tempIDSplitter 		= (this.id).split("_");
		var tempID 				= tempIDSplitter[1];
		var productIDSplitter 	= (tempID).split("-");
		var productID 			= productIDSplitter[0];		
		var old_subproductID 	= productIDSplitter[1];	
		
		var new_subproductID 	= parseFloat($("#subproducts_" + tempID + " option:selected").attr("value"));
		
		changesubproduct(productID, old_subproductID , new_subproductID);
		//alert("Productid : " + productID + "\n old_subproductID : " + old_subproductID + "\n new_subproductID : " + new_subproductID);
	})
});

function changesubproduct(productID, old_subproductID , new_subproductID)
{
	$.ajax({  
		type: "POST",  
		url: $("#siteurl").html() + "cart/changesubproduct/", 
		data: "productid="+ productID +"&oldsubproduct="+ old_subproductID +"&newsubproduct="+ new_subproductID,  
		success: function(theResponse) {
			$.ajax({  
				type: "POST",  
				url: $("#siteurl").html() + "cart/", 
				success: function(theResponse) {
					$("#rightCntr").html(theResponse);	
					updatecart();
				}  
			});			
		}  
	});
}

function changeamount()
{
	var productIDSplitter 	= (this.id).split("_");
	var productID 			= productIDSplitter[1];
	var amount				= parseFloat($("#amount_" + productID).attr("value"));
	changeamount(productID, amount)
}

function changeamount(productid, amount)
{
	$.ajax({  
		type: "POST",  
		url: $("#siteurl").html() + "cart/change/", 
		data: "productid="+ productid +"&amount="+ amount,  
		success: function(theResponse) {
			if(amount == 0)
			{			
				$("#product_" + productid).animate({ opacity: 0.0 }, 500, function () {
					$("#product_" + productid).animate({ height: "0px" }, 500, function() {
						$("#product_" + productid).hide();
					}); 
				}); 
				
			}
			else
			{
				var price = Number($("#price_" + productid).val()).toFixed(2);
				$("#total_" + productid).html(displayprice(price * amount));
			}
			updatecart();
		}  
	});
}


function updatecart()
{
	$.ajax({  
		type: "POST",  
		url: $("#siteurl").html() + "cart/getupdate/", 
		success: function(theResponse) {
			
			// Split response for different prices
			var arr_prices = theResponse.split("|");
			var productamount = arr_prices[0];
			var disposalfee = arr_prices[1];
			var shipping = arr_prices[2];
			var subtot = arr_prices[3];			
			var tot = arr_prices[4];
			
			$("#basket").html(productamount);
			$("#basket_price").html(displayprice(subtot));	
			$("#price_disposalfee").html(displayprice(disposalfee));	
			$("#price_shipping").html(displayprice(shipping));		
			$("#price_subtot").html(displayprice(subtot));
			$("#price_tot").html(displayprice(tot));
		}  
	});
}

function displayprice(price)
{   
    return Number(price).toFixed(2);
}


