var intervalID;
var unikSansPanier;
var isEnableUn = 1;
var typeCommande = null;
var typeAchat = null;
var currentTypePaiement=null;
var currentLivraison = 0;
//------------------------------------------------------------------------------------
$(document).ready(function(){
	$("#villeSearch").autocomplete("php/autocompleteSearch.php", {
				matchContains: true,
				scrollHeight:100,
				minChars: 2
	});
	$("#fast_search").autocomplete("php/autocompleteFastSearch.php", {
				matchContains: true,
				scrollHeight:100,
				minChars: 2
	});
	$("#fast_search").focus(function(){
		if($(this).val() == "Rechercher") $(this).val("");
	});
	//------------------------------------------------------------------------------------	
	$("#send-zone").click(function(){
		$.post("php/isLivrable.php", { q: $("#villeSearch").val() },
		function(data){
			if(data==1){
				$("#reponseImageVilleDesservie")
				.html("<div style=\"background-image:url('img/design/ville-oui-desservie.jpg');width:292px;height:127px;\"><p style=\"padding:10px 5px;width:240px;\">Votre ville est dans notre zone de livraison</p></div>");
			}else{
				$("#reponseImageVilleDesservie").html("<div style=\"background-image:url('img/design/ville-non-desservie.jpg');width:292px;height:127px;\"><p style=\"padding:10px 5px;width:240px;\">Désolé, votre ville n'est pas dans notre zone de livraison</p></div>");
			}
		});
		return false;
	});
	//------------------------------------------------------------------------------------
	$('#accordeon div.title, #accordeon .form_login h4').click(function(event){
		event.stopPropagation();
		$(this).next("div").slideToggle("fast");
	});
	//------------------------------------------------------------------------------------
	bindCaddie();
	bindItem();
	bindCategorieTrigg();
	//------------------------------------------------------------------------------------
	/* TODO ajouter ajax pour l'ajout du premier item du panier */

function bindCategorieTrigg(e){
	$("a.categ-trigg").click(function(){
		$(this).parent().find(".sub-categ").toggle();
		if($(this).parent().find(".sub-categ").is(":visible")){$.scrollTo($(this).parent().find("h2"), 500 );}
		return false;
	});
}

function bindItem(){
	$("form.product-item p.submit input, form.paniere_form p.submit input").live("click", function(){
		if($("#TB_window").length > 0){
			tb_remove();
		}
		form = $(this).parent().parent();
		params = form.serialize();
		// if($("#caddie .panier_item").length > 0) {  
			$.ajax({ type: "POST",
					url: "php/panier/add_item.php",
					dataType:"json",
					data: params,
					success: function(item){
						myItem = $("#"+item.id);
						if($(myItem).length > 0){
							elem = $(myItem).find("p.quantite span.edit");
							totElem = $(myItem).parent().find("p.total");
							elem.text(parseFloat(item.quantite));
							totElem.text("TOTAL : "+item.total+"€");
							$(form).find("p.submit").effect("highlight", {}, 2000);
							$(myItem).effect("highlight", {}, 2000);
		bindCaddie();
						}  else {
			createPanierItem(item, params, form);
						}
					}
			});
			return false; 
	//	} 
	});
}
	//------------------------------------------------------------------------------------	  
	function createPanierItem(item, params, form){
		$(form).find("p.submit").effect("highlight", {}, 2000);
		html = "<div class=\"panier_item\" id=\""+item.id+"\" style=\"\">";
		html += "<span class=\"hidden product_id\">"+item.idNew+"</span>";
		html += "<div class=\"col-left\">";
		html += "<img alt=\"image produit\" src=\"img/products/minis/"+item.img+"\"/>";
		html += "</div>	<div class=\"col-right\">";
		html += "<p><strong>"+item.nom+"</strong></p>	<p>"+item.modepesee+" : <strong>"+item.prixht+"€</strong></p>";
		html += "<p class=\"quantite\">Quantité : <span class=\"edit\">"+item.quantite+"</span><span> "+item.modepesee+" </span>";
		html += "<img title=\"Supprimer cet article\" width=\"22\" height=\"22\" alt=\"supprimer\" src=\"img/supprimer.jpg\" class=\"supprimer\"/></p>";
		html += "</div><div style=\"clear: both;\"/></div>";
		if($("#caddie .panier_item").length > 0) {
			$("#caddie .panier_item:last").after(html);
			$("#caddie p.total").text("TOTAL : "+item.total+"€");
		}else{
	/*		$.ajax({ type: "POST",
					url: "index.php",
					dataType:"html",
					data: params,
					success: function(html){
						console.log("good");
						$("#caddie").replaceWith($(html).find("#caddie"));
						bindCaddie();	
					}
			}); */
			$("#caddie").load("index.php #caddie", function(){bindCaddie();	});
		}
		bindCaddie();

			return false; 
	}
	//------------------------------------------------------------------------------------
	function updateProductQuantite(element){
		id=$(element).parent().parent().parent().attr("id");
		quantite =$(element).find("input.editable").val();
		params = {'id' : id, 'quantite' : quantite};
		$.ajax({ type: "POST",
				url: "php/panier/update_quantite.php",
				dataType:"json",
				data: params,
				success: function(item){
					$(element).prev("span").text(item.quantite);
					$("#caddie p.total").text("TOTAL : "+item.total+"€");
					hideEditable(element);
				}
		});
	}
	//------------------------------------------------------------------------------------
	function hideEditable(element){
		$(element).fadeOut("fast", function(){
			//$(element).prev("span").text($(this).find("input.editable").val());
			$(element).parent().find("span").fadeIn("fast");
			$(element).parent().find("img").show();
			$("form.editable-form").remove();
		});
	};
	//------------------------------------------------------------------------------------
	function edit(element){
		valeur = $(element).text();
		$(element).parent().find("img.supprimer, span.unite").hide();
		/*$(element).parent().find("span.unite").hide();*/
		$(element).fadeOut("fast", function(){
			html = "<form class=\"editable-form\" style=\"display:none;\"action=\"#\"><input class=\"editable\" type=\"text\" value=\""+valeur+"\" /><input type=\"image\" src=\"img/design/boutonOk.png\" value=\"ok\" /></form>";
			$(element).after(html);
			$(element).next("form").fadeIn("fast").submit(function(){
												updateProductQuantite($(this));   
												return false;
			});
			$(element).next("form").find(".editable").select();
		});
	} 
	//------------------------------------------------------------------------------------
	function bindCaddie() {
		$("#caddie img.supprimer").unbind("click");
		$("#caddie img.supprimer").click(function(){
			parentElement =$(this).parent().parent().parent(); 
			id = $(parentElement).find("span.product_id").text();
			$.ajax({ type: "POST",
					url: "php/panier/remove_item.php",
					dataType:"json",
					data: "item_id="+id,
					success: function(item){
						$(parentElement).slideUp("fast", function(){
												$(parentElement.remove());
												$("#caddie p.total").text("TOTAL : "+item.total+"€");
						});
					}
			});
		});
		$("#caddie p.quantite span").unbind("click");
		$("#caddie p.quantite span").click(function(){
			hideEditable($("form.editable-form"));
			edit($(this).parent().find("span.edit"));
		});
			return false; 
	}
	$("#accordeon div.up").slideUp("fast");
	//------------------------------------------------------------------------------------
	intervalID = setTimeout("rotateSlider()", 5000); 
	//------------------------------------------------------------------------------------
	$("#slider").click(function(event){event.stopPropagation()});
	$("#slider ul li").click(function(event){
					event.stopPropagation();
					clearTimeout(intervalID);
					$("#slider a.curent img").stop();
					index = $("#slider li").index(this);
					myImg =  $("#slider img")[index];
					if(index < 4) {
						$("#slider a.next").removeClass("next");
						$(myImg).parent().next().addClass("next").find("img").css({"display" : "block" });
						$("li.selected").removeClass("selected");
						$(this).addClass("selected");
						$("#slider a.curent").removeClass("curent next");
						$(myImg).parent().addClass("curent").find("img").css({"display" : "block"});
					} else {
						myImg =  $("#slider img")[0];
						$(myImg).parent().addClass("next").find("img").css({"display" : "block" });
						$("li.selected").removeClass("selected");
						$(this).addClass("selected");
						myImg =  $("#slider img")[index];
						$("#slider a.curent img").hide().parent().removeClass("curent next");
						$(myImg).parent().addClass("curent").css({"display" : "block"});
					}
					$("#slider a.next").removeClass("curent next");
					$(myImg).parent().next("a").addClass("next");
	});
	//------------------------------------------------------------------------------------
	$(document).click(function(){
		clearTimeout(intervalID);
		hideEditable($("form.editable-form"));
		intervalID = setTimeout("rotateSlider()",5000);
	});  
	//------------------------------------------------------------------------------------
	$("#caddie").click(function(e){
		e.stopPropagation();
	});
}); //document.ready
//------------------------------------------------------------------------------------
  function rotateSlider(){
	if($("#slider a").index($("#slider a.curent")) < 4 ){
		$("#slider a.curent").next("a").addClass("next").find("img").css({"display" : "block" });
		$("#slider a.curent img").fadeOut("slow", function(){
									$(this).parent().removeClass("curent next");
									$(this).parent().next("a").removeClass("next").addClass("curent").find("img").css({"display" : "block" });
									$(this).parent().find("img").css("display", "block");
									var myLi = $("#slider ul li")[$("#slider a").index($("#slider a.curent"))];
									$("li.selected").removeClass("selected");
									$(myLi).addClass("selected");
		});
	}else{
		image = $("#slider div.content-left img")[0];
		$(image).parent().addClass("next").find("img").css("display", "block");
		$("#slider a.curent img").fadeOut("slow", function(){
									$(this).parent().removeClass("curent next");
									$(image).parent().removeClass("next").addClass("curent");
									$(this).parent().find("img").css("display", "block");
									myLi = $("#slider ul li")[0];
									$("li.selected").removeClass("selected");
									$(myLi).addClass("selected");
		});
	}
	intervalID = setTimeout("rotateSlider()", 5000);
}

