// JavaScript Document

function getCart(){
	$.getJSON("/ajax.php?action=getCart", function(data){
		if(data !== null ){
			var items = [];
			items.push(' ');
			$.each(data, function(i, item) {
				if( item.name == 'Total' )
					items.push('<tr class="small_cart_total"><td colspan="2">' + item.name + '</td><td  nowrap="nowrap">' + item.price + ' &euro;</td></tr>');
				else
					items.push('<tr><td class="small_cart_name">' + item.name + '</td><td class="small_cart_quantity">' + item.quantity + 'x</td><td class="small_cart_price"  nowrap="nowrap">' + item.price + ' &euro;</td></tr>');
			});
			$("#small_cart").html('');
			var total = 0;
			var table = $("<table/>", {
				"class": "table-cart",
				html: items.join('')
			});
			$("#cart_details").html(table);
		}
	});
}

function addToCart(idproduct, quantity){
	$.ajax({type: "POST", url: "/ajax.php", data: "action=addtocart&idproduct=" + idproduct + "&quantity=" + quantity, success: function(){ getCart(); } });
}

function removeProductCart(){
	var idproduct = $('#idproduct').val();
	$.ajax({type: "POST", url: "/ajax.php", data: "action=addtocart&idproduct=" + idproduct + "&quantity=-9898545", success: function(){ getCart(); } });
}

function chgQuantity(qte,idproduct){
	$.ajax({type: "POST", url: "/ajax.php", data: "action=changeproductquantity&idproduct=" + idproduct + "&quantity=" + qte, success: function(){ location.href='/panier.html'; } });
}

function deleteItemFromCart(idproduct){
	$.ajax({type: "POST", url: "/ajax.php", data: "action=changeproductquantity&idproduct=" + idproduct + "&quantity=-9898545", success: function(){ location.href='/panier.html'; } });	
}

function logOut(){
	$.ajax({type: "POST", url: "/ajax.php", data: "action=logout", success: function(){ reloadPage(); } });
}

function changeAddress(id){
	$.ajax({type: "POST", url: "/ajax.php", data: "action=changeAddress&idaddress=" + id, success: function(){ reloadPage(); } });
}

function reloadPage(){
	var reg = new RegExp("#", "g");
	var url = window.location.href;
	url = url.replace(reg,'');
	window.location = url;
}

$(document).ready(function(){ 
   // getCart();
	
	//$("#menuSite").menuDropDown();

	/*$('.button_addtocart').each( function(){
		$(this).click( function(){
			var parent = $(this).parent(".product_form");
			var idproduct = $(parent).children('.form_idproduct').val();
			var quantity = $(parent).children('.form_quantity').val();
			addToCart(idproduct, quantity);
			$('#addedProduct').show();
			$('#addedProduct').fadeOut(1500);
			return false;
		});
	});*/
	
}); 


(function($){
	$.fn.menuDropDown = function(){
		var child = $(this).children('li').children('a');
		$(child).each(function(){
			$(this).hover(function(){
				$("#menuSite .child").hide();	
				$(this).next("ul").show();
				
				var littlechild = $(this).next("ul").children('li').children('a');
				$(littlechild).each(function(){
					$(this).hover(function(){
						$("#menuSite .child li .child").hide();
						$(this).next("ul").show();
					});
				});
				
			});	
			$(this).next("ul").mouseleave(function(){
				$(this).hide();
			});
		});
	}
})(jQuery);
