$(document).ready(function(){

	$("#contenuPanier li:first").hide();

});

var ajaxPanier = {

	add : function(idProduct, type, numero, nombreNumero){

		var productX 		= $("#productImageWrapID_" + idProduct).offset().left;
		var productY 		= $("#productImageWrapID_" + idProduct).offset().top;

		if( $("#productID_" + idProduct).length > 0){
			var basketX 		= $("#productID_" + idProduct + "_" + type).offset().left;
			var basketY 		= $("#productID_" + idProduct + "_" + type).offset().top;
		} else {
			var basketX 		= $("#basketTitleWrap").offset().left;
			var basketY 		= $("#basketTitleWrap").offset().top;
		}

		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;

		var newImageWidth 	= $("#productImageWrapID_" + idProduct + " img").width() / 3;
		var newImageHeight	= $("#productImageWrapID_" + idProduct + " img").height() / 3;

		$("#productImageWrapID_" + idProduct + " img")
		.clone()
		.prependTo("#productImageWrapID_" + idProduct)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.4}, 100 )
		.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
			$(this).remove();

			$("#notificationsLoader").html('<img src="images/loader.gif">');

			$.ajax({
				type: "GET",
				url: "panier_ajax.php",
				data: 'ajout=1&ajax=true&type=' + type + '&id=' + idProduct + '&numero=' + numero + '&nombreNumero=' + nombreNumero,
				success: function(theResponse) {

					if( $("#productID_" + idProduct + "_" + type).length > 0){
						$("#productID_" + idProduct + "_" + type).animate({ opacity: 0 }, 500);
						$('#pod_panier').html(theResponse);
						$("#productID_" + idProduct + "_" + type).animate({ opacity: 0 }, 500);
						$("#productID_" + idProduct + "_" + type).animate({ opacity: 1, 'filter': '' }, 500);
						$("#notificationsLoader").empty();

					} else {
						$('#pod_panier').html(theResponse);
						$("#productID_" + idProduct + "_" + type).animate({ opacity: 0 }, 500);
						$("#productID_" + idProduct + "_" + type).animate({ opacity: 1, 'filter': '' }, 500);
						$("#notificationsLoader").empty();
					}

				}
			});

		});
	},

	remove : function(idProduct, type, numero) {
		$("#notificationsLoader").html('<img src="images/loader.gif">');

		$.ajax({
			type: "GET",
			url: "panier_ajax.php",
			data: 'suppression=1&ajax=true&type=' + type + '&id=' + idProduct + '&numero=' + numero,
			success: function(theResponse) {

				$("#productID_" + idProduct + "_" + type).hide("slow",  function() {
					$(this).remove();
					$('#pod_panier').html(theResponse);
				});
				$("#notificationsLoader").empty();

			}
		});
	}
}
