/* Toggle */
function basculeSigne(tagA){
	//get effect type from 
	if(tagA.html()=="Voir la suite"){
		tagA.html("Masquer la suite");
	}
	else {
		tagA.html("Voir la suite");
	}
};
// Declenchement du toggle
function selMenu(tagA,prefix){
	$("[id^=lien" + prefix + "]").each(function() {
		if($(this).attr('class')=="menuActif"){
			$(this).attr('class',"menuInactif");
			var idToggle = $(this).attr("id").substring(4);
			// runToggleSansBascule($(this),idToggle);
			$("#" + idToggle).hide("slow");
		}
	});
	idToggle = tagA.attr("id").substring(4);
	//runToggleSansBascule(tagA,idToggle);
	$("#" + idToggle).show("slow");
	tagA.attr('class',"menuActif");
}
function runToggle(tagA,idToggle){
	var selectedEffect = "blind";
	var options = {};
	$("#" + idToggle).toggle(selectedEffect,options,500);
	basculeSigne(tagA);
};
function runToggleSansBascule(tagA,idToggle){
	var selectedEffect = "blind";
	var options = {};
	$("#" + idToggle).toggle(selectedEffect,options,500);
};
/* Objets du panier */
function noOrdreFromId(idDom){
	var tIdDom = idDom.split("-");
	var noOrdre = tIdDom[1];
	return noOrdre;
}
function panierGtcp(domArticles){
	this.sessid = "";
	this.noOrdre = -1;
	this.domArticles = domArticles;
	this.commande = new Object();
	this.domDialog = "";
	this.commandeVide = false;
	//
	this.initCommande = function (){
		this.commandeVide = true;
		this.commande = new Object();
		this.commande["action"] = "commande";
		this.commande["PHPSESSID"] = this.sessid;
	};
	this.commandeAjouterProduit = function (nomInput,qtte){
		var tIdDom = nomInput.split("-");
		if (tIdDom[0]=="QTTE"){
			this.commandeVide = false;
			this.commande[nomInput] = qtte;
		}
	};
	this.passerCommande = function (){
		//$("#panierContenu").html("<h2>Preparation de la commande</h2>");
		$.get("/commandes/ajaxPanier.php", this.commande,function(data){lePanier.commandeOk(data);});
	};
	this.commandeOk = function(data){
		$("#rappelPanier").html(data);
		$("#menuTerminer").html($('#panierTerminer').html());
		//$("#panierContenu").append("<h2>Votre commande a été ajoutée au panier</h2>");
		//$.get("/commandes/ajaxPanier.php", {action: "recap"},function(data){$("#panierContenu").html(data);comportementsRecap()});
		// $("[id^='DLG-']").html("");
		$("#menuCommander").html("");
	};
	this.commandeAnnuler = function(){
		$('#' + this.domDialog).dialog('destroy');
		$('#' + this.domDialog).replaceWith("");
		$("#dialog").html("");
		this.domDialog="";
	};
	this.setDialogQttes = function(IdDomCb){
		var tIdDom = IdDomCb.split("-");
		var noOrdre = tIdDom[1];
		this.noOrdre = noOrdre;
		$.get("/commandes/ajaxPanier.php", {action: "setDialog", noOrdre: this.noOrdre},function(data){lePanier.ouvrirDialogQttes(data);});
	};
	this.ouvrirDialogQttes = function(data){
		//alert(data);
		//
		//$("[id^='DLG-']").html("");
		$("#DLG-" + lePanier.noOrdre).html(data);
		comportementsDialogQttes(lePanier.noOrdre);
	};
	//
	// Gestion de la recap commande
	this.removeLigne = function(IdDomCb){
		var tIdDom = IdDomCb.split("-");
		var noLigne = tIdDom[1];
		$.get("/commandes/ajaxPanier.php", {action: "remove", noLigne: noLigne},function(data){lePanier.htmlRecap(data);});
	};
	this.htmlRecap = function(data){
		//alert(data);
		$("#action").html(data);
		comportementsPanierTest();
		$.get("/commandes/ajaxPanier.php", {action: "rappel"},function(data){$("#rappelPanier").html(data);});
	};
}
/*---*/
function comportementsDialogQttes(noOrdre){
	$("#commander-" + noOrdre).click(function() {
		lePanier.initCommande();
		$("#saisies input[value!=0]").each(function() {
			lePanier.commandeAjouterProduit($(this).attr("name"),$(this).attr("value"));
		});
		$("#saisies select[value!=0]").each(function() {
			lePanier.commandeAjouterProduit($(this).attr("name"),$(this).attr("value"));
		});
		if (lePanier.commandeVide == true){
			$("#saisies").append("<h2>vous devez saisir une quantite au moins</h2>");
		}
		else{
			lePanier.passerCommande();
		}
		return true;
	});
	$("#annuler-" + noOrdre).click(function() {
		$("#DLG-" + noOrdre).html("");
		return true;
	});
	$("#DLG-" + noOrdre + " input[type='checkbox']").click(function() {
		var nameCb = $(this).attr("name");
		var nameInput = nameCb.replace("CB-","QTTE-");
		//alert(nameInput);
		var domInput = $("[name=" + nameInput + "]");
		if(domInput.attr("value")==0){
			domInput.attr("value",1);
		}
		else {
			domInput.attr("value",0);
		}
	});
}
function comportementsPanierTest(){
	$("a[idToggle]").click(function() {
		runToggle($(this),$(this).attr('idToggle'));
		return false;
	});
	$("#lienAutreRecherche").click(function() {
		runToggleSansBascule($(this),"rechercheAutre");
		return false;
	});
	$("[id^='lienRecherche']").click(function() {
		selMenu($(this),"Recherche");
		return false;
	});
	$("input[idToggle]").click(function() {
		runToggleSansBascule($(this),$(this).attr('idToggle'));
	});
	$("[id^='SEL-']").click(function() {
		var idDom = $(this).attr("id");
		var noOrdre = noOrdreFromId(idDom);
		var nomZoneQtte = "QTTE-CC-" + noOrdre + "-ECRAN";
		//
		if($(this).attr("class")=="prdSel"){
			$(this).attr("class","prdSel");
		}
		else {
			lePanier.initCommande();
			lePanier.commandeAjouterProduit(nomZoneQtte,1);
			lePanier.passerCommande();
			$(this).attr("class","prdSel");
		}
	});
	comportementsRecap();
	$("#commander").click(function() {
		lePanier.initCommande();
		$("input[value!=0]").each(function() {
			lePanier.commandeAjouterProduit($(this).attr("name"),$(this).attr("value"));
		});
		$("select[value!=0]").each(function() {
			lePanier.commandeAjouterProduit($(this).attr("name"),$(this).attr("value"));
		});
		if (lePanier.commandeVide == true){
			$("#panierContenu").prepend("<p class='erreur'>vous devez saisir une quantite au moins</p>");
		}
		else{
			lePanier.passerCommande();
		}
		return true;
	});
}
function comportementsRecap(){
	$("[id^='REMOV-']").click(function() {
		//alert($(this).attr("id"));
		lePanier.removeLigne($(this).attr("id"));
	});
}
/*---*/
var lePanier = new panierGtcp("panierNbArticles");


