// Fonctions JavaScript Casal

var chemin_img = PATHRES + "/img";
var chemin_flash = PATHRES + "/flash";
var url_zoom_aptique = "http://zoom.pictime.fr/casal";

//Execution du code au chargement de la page (IfixPNG,prechargement des images, survol)
$(document).ready(function(){
	$.ifixpng(chemin_img+'/00-commun/1ptrans.gif');$('.bgpng').ifixpng();
	$.ifixpng2(chemin_img+'/00-commun/1ptrans.gif');$('img,.bgpng2').ifixpng2();
	var preload = 
  ["00-header/btn_aide_on.png",
   "00-header/btn_boutiqueparticulier_on.png",
   "00-header/btn_commanderapide_de.png",
   "00-header/btn_commanderapide_on.png",
   "00-header/btn_devis_on.png",
   "00-header/btn_moncompte_de.png",
   "00-header/btn_moncompte_on.png",
   "00-header/btn_total_de.png",
   "00-header/btn_votrepanier_de.png",
   "00-header/btn_votrepanier_on.png",
   "00-header/btn_catalogues_on.png",
   "00-header/btn_univers_on.png",
   "00-header/btn_marques_on.png",
   "00-header/btn_univers_marques.png",
   "00-header/btn_sports_marques.png",
   "00-header/btn_univers_sports.png",
   "00-header/btn_marques_sports.png",
   "00-header/btn_sports_on.png",
   "00-header/fond_sports_p.png",
   "00-header/fond_univers.png",
   "00-header/fond_marques.png",
   "00-header/fond_sports.png"];
   preloadIMG(preload);
	
	//Definition des survols
	$("#btn_devis").hover(
		function(){$(this).attr("src",chemin_img+"/00-header/btn_devis_on.png");$('img, .bgpng').ifixpng();},
		function(){$(this).attr("src",chemin_img+"/00-header/btn_devis_off.png");$('img, .bgpng').ifixpng();}
	);
	
	$("#btn_aide").hover(
		function(){$(this).attr("src",chemin_img+"/00-header/btn_aide_on.png");$('img, .bgpng').ifixpng();},
		function(){$(this).attr("src",chemin_img+"/00-header/btn_aide_off.png");$('img, .bgpng').ifixpng();}
	);
	
	$(".btn_catalogues img").hover(
		function(){$(this).attr("src",chemin_img+"/00-header/btn_catalogues_on.png");$('img, .bgpng').ifixpng();},
		function(){$(this).attr("src",chemin_img+"/00-header/btn_catalogues_off.png");$('img, .bgpng').ifixpng();}
	);
	
	//Bind de certains elements
	bind_nav();

	btn_search();
	
	
	//Menu Over de l'Arche
	$(".btn_univers img").mouseover(
		function(){
			$(".calque_sortie").show();
			$(".calque_sortie img").css({width: $(window).width(), height: $(document).height()});
			$(this).attr("src",chemin_img+"/00-header/btn_univers_on.png").css("z-index","40");
			$(".calque_univers").show();
			$('img,.bgpng2').ifixpng2();
			}
			
	);
	
	$(".btn_marques img").mouseover(
		function(){
			$(".calque_sortie").show();
			$(".calque_sortie img").css({width: $(window).width(), height: $(document).height()});
			$(".btn_univers img").attr("src",chemin_img+"/00-header/btn_univers_marques.png");
			$(".btn_sports img").attr("src",chemin_img+"/00-header/btn_sports_marques.png");
			$(this).attr("src",chemin_img+"/00-header/btn_marques_on.png").css("z-index","40");
			$(".calque_marques").show();
			$('img,.bgpng2').ifixpng2();
		}
	);
	
	$(".btn_sports img").mouseover(
		function(){
			$(".calque_sortie").show();
			$(".calque_sortie img").css({width: $(window).width(), height: $(document).height()});
			$(".btn_univers img").attr("src",chemin_img+"/00-header/btn_univers_sports.png");
			$(".btn_marques img").attr("src",chemin_img+"/00-header/btn_marques_sports.png");
			$(this).attr("src",chemin_img+"/00-header/btn_sports_on.png").css("z-index","40");
			$(".calque_sports").show();
			$('img,.bgpng2').ifixpng2();
		}
	);
	
	
	//Sortie du Over de l'Arche
	$(".calque_sortie img").mouseover(
		function(){
			$(".calque_sortie").hide();
			$(".btn_univers img").attr("src",chemin_img+"/00-header/btn_univers_off.png");
			$(".btn_marques img").attr("src",chemin_img+"/00-header/btn_marques_off.png");
			$(".btn_sports img").attr("src",chemin_img+"/00-header/btn_sports_off.png");
			$(".btn_univers img, .btn_marques img, .btn_sports img").css("z-index","0")
			$(".calque_univers, .calque_marques, .calque_sports").hide();
			$('img,.bgpng2').ifixpng2();
			}
	);
	
	
	// Box Perte de mot de passe
	$(".btnLostPass").click(function(){
		Box.open({
			modal: true, 
			url: PATHRACINE + "/LostPass", 
			urldata: PARAMGLOBAL, 
			show:function(){
				$("#email", this.el).focus(function(){
					if ($(this).val() == "Format d'email incorrect" || $(this).val() == "Veuillez saisir votre adresse email" || $(this).val() == "Adresse email inconnue") {
						$(this).attr("value", "");
					}
					$(this).css("color","#666666");
				});
				$("#frmlostpass", this.el).submit(function(){
					$("#btn_envoyer", this.el).click();
					return false;
				});
				$("#btn_envoyer", this.el).click(function(){
					var mail=$("#email", Box.el).val();
					if(mail != "Format d'email incorrect" && mail != "Veuillez saisir votre adresse email" && mail != "Adresse email inconnue"){
						if(mail != ""){
							var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
							var value = $.trim(mail);
							if(reg.test(value) == true){
								$(".lostpass_tab,.lostpass_txt", Box.el).css("visibility", "hidden");
							$(".lostpass").append('<div class="lostpass_loading"><img src="'+ chemin_img +'/00-commun/loading.gif"/></div>');
								
								$.ajax({
									type: "POST",
									url: PATHRACINE + "/LostPass",
									data: PARAMGLOBAL + "&" + $("#frmlostpass", Box.el).serialize(),
									success: function(data){
										Box.maj(data);
									}
								});
							}
							else{
								$("#email", Box.el).val("Format d'email incorrect").css("color","red");
							}	
						}
						else{
							$("#email", Box.el).val("Veuillez saisir votre adresse email").css("color","red");
						}
					}
					return false;
				})
			}
		});
		return false;
	});
});


//Fonction : bind de la nav
function bind_nav()
{
	$("#btn_moncompte").bind("mouseover",function(){$(this).attr("src",chemin_img+"/00-header/btn_moncompte_on.png");$('.bgpng').ifixpng();$('img,.bgpng2').ifixpng2();});
	$("#btn_moncompte").bind("mouseout",function(){$(this).attr("src",chemin_img+"/00-header/btn_moncompte_off.png");$('.bgpng').ifixpng();$('img,.bgpng2').ifixpng2();});
	
	$("#btn_votrepanier").bind("mouseover",function(){$(this).attr("src",chemin_img+"/00-header/btn_votrepanier_on.png");$('.bgpng').ifixpng();$('img,.bgpng2').ifixpng2();});
	$("#btn_votrepanier").bind("mouseout",function(){$(this).attr("src",chemin_img+"/00-header/btn_votrepanier_off.png");$('.bgpng').ifixpng();$('img,.bgpng2').ifixpng2();});
	
	$("#btn_commanderapide").bind("mouseover",function(){$(this).attr("src",chemin_img+"/00-header/btn_commanderapide_on.png");$('.bgpng').ifixpng();$('img,.bgpng2').ifixpng2();});
	$("#btn_commanderapide").bind("mouseout",function(){$(this).attr("src",chemin_img+"/00-header/btn_commanderapide_off.png");$('.bgpng').ifixpng();$('img,.bgpng2').ifixpng2();});
}


//Fonction : prechargement des images
function preloadIMG(liste)
{
	var imgs = [];
	for (var i = 0; i<liste.length; i++)
	{
		imgs[i] = new Image();
		imgs[i].src = chemin_img +"/"+ liste[i];
	}
}


//Fonction : deroulement du panier
function deroule_panier()
{
	//Unbind des elements
	$("#btn_moncompte").unbind("mouseover").unbind("mouseout");
	$("#btn_votrepanier").unbind("mouseover").unbind("mouseout");
	$("#btn_commanderapide").unbind("mouseover").unbind("mouseout");

	//Modification des images
	$("#btn_moncompte").attr("src",chemin_img+"/00-header/btn_moncompte_de.png");
	$("#btn_votrepanier").attr("src",chemin_img+"/00-header/btn_votrepanier_de.png");
	$("#btn_total").attr("style","background:url("+chemin_img+"/00-header/btn_total_de.png);");
	$("#btn_commanderapide").attr("src",chemin_img+"/00-header/btn_commanderapide_de.png");
	
	//Reexecution de la transparence
	$('.bgpng').ifixpng();$('img').ifixpng2();
	
	//Descente du panier
	$(".panier").slideDown("slow",function(){
		//setTimeout("enroule_panier()",2000);
		$(".panier").hover(
			function(){if (typeof enrouleTimer != "undefined"){clearTimeout(enrouleTimer);}},
			function(){enrouleTimer = setTimeout("enroule_panier()",2000);}
		);
	});
}


//Fonction : enroulement du panier
function enroule_panier()
{
	//Remontee du panier
	$(".panier").slideUp("slow",function(){
		//Retour des images a la normale
		$("#btn_moncompte").attr("src",chemin_img+"/00-header/btn_moncompte_off.png");
		$("#btn_votrepanier").attr("src",chemin_img+"/00-header/btn_votrepanier_off.png");
		$("#btn_total").attr("style","background:url("+chemin_img+"/00-header/btn_total_off.png);");
		$("#btn_commanderapide").attr("src",chemin_img+"/00-header/btn_commanderapide_off.png");
		
    	//Reexecution de la transparence
		$('.bgpng').ifixpng();$('img').ifixpng2();
		
		//Rebind des elements
		bind_nav();
	});
}	

//Fonction : mise à jour du flash sur page produit
function updateFlash(num, patternV)
{
	num = num.replace(/&/g, "--");
	var patternZ;
	var params = {
			wmode : "transparent",
			quality : "high"
		}
	switch(patternV){
		case 'PV_1': patternZ='Z_1';break;
		case 'PV_2': patternZ='Z_2';break;
		case 'PV_3': patternZ='Z_3';break;
		case 'PV_4': patternZ='Z_4';break;
		case 'PV_5': patternZ='Z_5';break;
		case 'PV':
		default: patternZ='Z';break;
	}
		
	var flashvars = {
		ZoomifyMaxZoom : "65",
		zoomifyImagePath : url_zoom_aptique+"/images_zoom/"+num+"_"+patternZ	
	}
	swfobject.embedSWF(url_zoom_aptique+"/zoomCasal_v2.swf", "zoom_flash", "638", "426", "8", chemin_flash+"/expressInstall.swf",flashvars, params, false);
}

//Fonction : parole d'expert - page produit
function cpde(ProductID, ProductCode)
{
	//Verification d'une occurence / Si oui suppression
	if($(".en_parole_form").hasClass("en_parole_form"))
	{$(".en_parole_form").remove();}
	
	//Création du contenu
	var cpde_html  = '<div class="en_parole_form">';
		cpde_html += '<div class="epf_close"><img src="'+chemin_img+'/00-commun/1ptrans.gif" width="70" height="31" border="0" alt=""></div>';
		cpde_html += '<div class="epf_contenu t12 lh14 color666666">Entrez votre commentaire pour ce produit :</div>';
		cpde_html += '<div class="epf_form">';
		cpde_html += '<div class="form_1"><input type="text" class="t12 color666666" id="nomClientCommentaire" value="Votre nom" onfocus="focusElement(this, \'Votre nom\', \'Nom invalide\');" onblur="blurElement(this, \'Votre nom\');"></div>';
		cpde_html += '<div class="form_2"><input type="text" class="t12 color666666" id="emailClientCommentaire" value="Votre email" onfocus="focusElement(this, \'Votre email\', \'E-mail invalide\');" onblur="blurElement(this, \'Votre email\');"></div>';
		cpde_html += '<div class="form_3"><textarea class="t12 color666666" id="clientCommentaire" onfocus="focusElement(this, \'Votre commentaire\', \'Merci de saisir un commentaire\');" onblur="blurElement(this, \'Votre commentaire\');">Votre commentaire</textarea></div>';
		cpde_html += '<div class="clear"></div>';
		cpde_html += '</div>';
		cpde_html += '<div class="epf_btn"><img src="'+chemin_img+'/04-produit/btn_commentaires2.png" width="175" height="20" border="0" alt="" onclick="validateCommentaireExpert(\''+ProductID+'\',\''+ProductCode+'\')"></div>';
		cpde_html += '</div>';
		
	//Ajout du contenu
	$(".en_parole").after(cpde_html);
	
	//Association de la fermeture
	$(".epf_close").click(function(){$(".en_parole_form").remove();});
}

//Fonction : suivi de colis - page commande visualisation
function sics(orderLineId)
{
	Box.open({
		modal: true, 
		url: PATHRACINE + "/OrderLineParcels?"+PARAMGLOBAL+"&orderLineId="+orderLineId
	});
}


//Fonction du champs rechercher
function btn_search() {
	var defaut = "Rechercher";
	var field = $("#search").focus(function(){if ($.trim(this.value) == defaut) {this.value='';}})
	  			.blur(function(){if ($.trim(this.value) == '') {this.value = defaut;}}).blur();
	
	//Validation du champs rechercher
	$("#frmsearch").submit(function(){
		var value = $.trim(field.val());
		return (value != defaut && value != "");
	});
	/*
	$("#gosearch").click(function(){
		$("#frmsearch").submit();
		return false;
	});*/
}


//Fonction : changement d'état des onglets
function select_onglet(num)
{
	//On remet tout à  la normale
	$(".o_d_d").addClass("o_d").removeClass("o_d_d");
	$(".o_f_g").addClass("o_f").removeClass("o_f_g");
	$(".o_m_g,.o_m_d").addClass("o_m").removeClass("o_m_g o_m_d");
	$(".o_t_a").addClass("o_t").removeClass("o_t_a");
	
	var id = "onglet_"+num;
	var id_prec = "onglet_"+(num-1);
	var id_suiv = "onglet_"+(num+1);
	var id_aff = "div_onglet_"+(num/2); 
	
	//On change dans l'ordre : prec - id - suiv
	$("#"+id_prec+"[class='o_d']").addClass("o_d_d").removeClass("o_d");
	$("#"+id_prec+"[class='o_m']").addClass("o_m_d").removeClass("o_m");
	
	$("#"+id).addClass("o_t_a").removeClass("o_t");
	$("div:visible[id^='div_onglet_']").fadeOut("fast",function(){$("#"+id_aff).fadeIn("fast");});
	
	$("#"+id_suiv+"[class='o_f']").addClass("o_f_g").removeClass("o_f");
	$("#"+id_suiv+"[class='o_m']").addClass("o_m_g").removeClass("o_m");
}


//Calcul pour le centre
function centerEl(el){
	var top = $(window).scrollTop()+($(window).height()/2)-(el.height()/2);
	var left = ($(window).width()/2)-(el.width()/2);
	el.css({top: top +"px", left: left +"px"});	
	return el;
}

function focusElement(element, value, value2) {
	if ($(element).val() == value || $(element).val() == value2) {
		$(element).attr("value", "");
	}
	$(element).removeClass("rouge");
	if (! $(element).hasClass("color666666")) {
		$(element).addClass("color666666");
	}
}

function blurElement(element, value) {
	if ($(element).val() == "") {
		$(element).attr("value", value);
	}
}
	
// Appel catalogue interactif	
function openCatalog(src,name){
	window.open(src,name,'width=1000, height=670 ,left=5 ,top=5 ,scrollbars=no,toolbars=no,menubars=no,location=no');
}

// Ajout aux favoris
function bookmark() {
	if ( navigator.appName != 'Microsoft Internet Explorer' )
	{ window.sidebar.addPanel("Casal Sport","http://www.casalsport.com",""); }
	else { window.external.AddFavorite("http://www.casalsport.com","Casal Sport"); } 
}

function markAndGoto(marker, dest) {
	xt_med('C','18',marker,'N');
	window.location=dest;
}