// JavaScript Document
//var cartBags = {};
$(function(){
	$().mousemove(function(e){
      netrade.helpers.cart.mousePosition = {x:e.pageX,y:e.pageY};
   	}); 
});

netrade.helpers.cart = {
	
	bags : {},
	
	mousePosition : {x:0,y:0},
	
	popupCount : 0,	
	popupOpen : function() {
		// dal momento che la pos del mouse inizio a prenderla dall'onload eseguo questo all'onload
		$(function(){
			var popupId = "CartPopup"+netrade.helpers.cart.popupCount++;
			var popupW = 200;
			var popupX = (netrade.helpers.cart.mousePosition.x-(popupW));
			var popupY = netrade.helpers.cart.mousePosition.y;
			var popupContent = '<div id="'+popupId+'" class="CartPopup" '+
					   'style="position:absolute;z-index:'+(50+netrade.helpers.cart.popupCount)+';display:none;width:'+popupW+'px;'+
					   'top:'+popupY+'px;left:'+popupX+'px;">'+
					   '<strong>Prodotto aggiunto al carrello.</strong><br />Il carrello ora contiene '+netrade.helpers.cart.bags.qt+' prodotti.</div>';
			$('body').append(popupContent);
			$('#'+popupId).fadeIn('slow');
			setTimeout("netrade.helpers.cart.popupClose('"+popupId+"')",3000);
		});
	},	
	popupClose : function( popupId ) {
		$('#'+popupId).fadeOut('fast');
	},
	
	addProduct : function( prodId ) {

		//alert( " TODO: segnalare visivamente inserimento");
		var qt = 1;//$("#qt_"+prodId).val();
		
		if(typeof(prodId) == "string")
			prodId = prodId.replace(/,/g, "$");
		
		var url = "/shop/cart/add/"+prodId+"/"+qt;
		
	//	alert( url ) ; return;
		// Do an ajax call
		$.ajax({
			type : 'POST',
			url : url,
			data : '',
			dataType : 'json',
			async: false,
			success : function(response) {				
				if (response.isSuccess) {
					netrade.helpers.cart.bags = response.result;
					netrade.helpers.cart.refreshInfo();
					netrade.helpers.cart.popupOpen();
					//alert( 'Posizione del mouse:\nx:'+netrade.helpers.cart.mousePosition.x+'\ny:'+netrade.helpers.cart.mousePosition.y );
				} else {
					alert(response.error);
				}
			},
			error : function(response) {
				alert('ajax error \n' + response.responseText);
			}
		});

	},
	
	refreshInfo : function() {
		$('#CarInfo').html( netrade.helpers.cart.bags.html );
	},
	
	increaseQt : function( id ) {
		var actualQt = parseInt($('#qt'+id).val());
		var qt = actualQt+1;
		netrade.helpers.cart.setQt(id,qt);
	},

	decreaseQt : function( id ) {
		var actualQt = parseInt($('#qt'+id).val());
		var qt = actualQt-1;
		if ( qt>0 ) {
			netrade.helpers.cart.setQt(id,qt);
		} else {
			netrade.helpers.cart.delProd(id);
		}
	},

	setQt : function ( id, qt ) {	
		var url = "/shop/cart/setqt/"+id+"/"+qt;
		netrade.helpers.cart.executeAndRefresh(url);		
	},
		
	delProd : function( id ) {		
		var url = "/shop/cart/removeitem/"+id+"/0";
		netrade.helpers.cart.executeAndRefresh(url);
	},
	
	empty : function() {			
		var url = "/shop/cart/clear";		
		netrade.helpers.cart.executeAndRefresh(url);		
	},
	
	executeAndRefresh : function( url ) {
		//alert( url );
		$.ajax({
			type : 'POST',
			url : url,
			data : '',
			dataType : 'json',
			async: false,
			success : function(response) {				
				if (response.isSuccess) {
					location.href=location.href ;
				} else {
					alert(response.error);
				}
			},
			error : function(response) {
				alert('ajax error \n' + response.responseText);
			}
		});
	}
	
};

/*

function getCartTotal() {
	var totalCart = 0;
	for( var i=0; i<cartBags.length; i++ ) {
		//alert( cartBags[i].item.prezzo );
		price = parseFloat(cartBags[i].item.prezzo);
		price = Math.round(price*100)/100;
		totalCart += price * cartBags[i].qt;
	}
	return totalCart;
}






*/