// JavaScript Document
$(function(){
	// Changer l'apercu de l'image
	$("#js_photo li, #motfis_couleur").find("a").click(function(){ 
		//$(this).css({opacity: 0.5});
		ImgA = $("#big_image");
		url = $(this).attr("href");
		reg = new RegExp("med_","g");
		urlReg = url.replace(reg,"big_");
		ImgA.attr({ href : urlReg });
		ImgA.find("img").attr({ src : url });		
		
		// Change l'id_prod_coul
		idpcd = $(this).attr("class").replace("IPCD-","");
		$("input[@name=id_prod_coul_details]").val(idpcd);
		
		// Position l'index du select
		pos = $(this).attr("index");
		if( document.getElementById("selectColor") ) document.getElementById("selectColor").selectedIndex = pos;
		return false;
	});
	
	$("#selectColor").change(function(){
		ImgA = $("#big_image");
		url = $("#motfis_couleur a").eq(this.selectedIndex).attr("href");
		reg = new RegExp("med_","g");
		urlReg = url.replace(reg,"big_");
		ImgA.attr({ href : urlReg });
		ImgA.find("img").attr({ src : url });

		// Change l'id_prod_coul
		
		idpcd = $(this).val().split("|");
		idpcd = idpcd.length > 1 ? idpcd[1].replace("IPCD-","") : "";
		$("input[@name=id_prod_coul_details]").val(idpcd);
	});
	
	$("#big_image").click(function(){
		img = $(this).attr("href");;
		PopupImage(img);
		return false;
	});
	
	$("#taillePrix").change(function(){
		pos = this.selectedIndex;
		newPrix = $(this).find("option").eq(pos).attr("prix");
		ptvac = newPrix * 1.21;		
		phpSprintf(newPrix, $(".PrixHtvaJs"));
		phpSprintf(ptvac, $(".PrixTvacJS"));
		$("[@name=prix]").val( newPrix );
	});
	
	$("#copyMe").click(function(){
		if($(this).attr("checked")){
			$("#b1").val($("#a1").val());
			$("#b2").val($("#a2").val());
			$("#b3").val($("#a3").val());
			document.getElementById('b4').selectedIndex = document.getElementById('a4').selectedIndex;
		}
	});
	
	$("#form_toile").submit(function(){
		if( $("#pas1").val() != $("#pas1").val() ){
			return false;
		}
	});
	
	// Chargement Formulaire Client
	$("#info client").click(function(){
		id_client = $(this).val();
		
	});
	
	// Controle lors d'un ajout au panier
	var modele 	= $("[@name=modele]");
	var prix 	= $("[@name=prix]");
	var taille 	= $("[@name=taille]");
	var couleur = $("[@name=couleur]");
	var quantite= $("[@name=quantite]");
	var choice 	= false;
	var base	= $("base").attr("href");
	
	modele.change(function(){
		
		if( modele.val() != "no_selected" ){			
			// Changer les valeur par défaut
			tab = modele.val().split("|");
			prix.val(tab[3]);
			taille.val(tab[2]);
			couleur.val(tab[1]);
			choice = true;
			
			// Changer l'image
			ImgA = $("#big_image");
			url = base + "pictures/prod_couleur_details_prix/med_"+tab[0]+".jpg";
			urlB = base + "pictures/prod_couleur_details_prix/big_"+tab[0]+".jpg";
			ImgA.attr({ href : urlB });
			ImgA.find("img").attr({ src : url });
		}else{
			// Remettre à 0
			prix.val("");
			taille.val("");
			couleur.val("");
			choice = false;
		}
	});
	
	$("#achat").submit(function(){
		reponse = false;
		if( !isNaN( quantite.val() ) && quantite.val() > 0 ){
			reponse = true;
		}else{
			$("#error1").animate({ opacity : "show"}, 500).animate({ opacity : "hide"}, 1500);
		}
		return reponse;
	});
});
