﻿$(document).ready(function() {
	// pour coins arrondis
	$(".corner").prepend('<div class="tl">&nbsp;</div><div class="tr">&nbsp;</div>');
	$(".corner").append('<div class="bl">&nbsp;</div><div class="br">&nbsp;</div>');
	$(".pagination a").prepend('|&nbsp;');
	
	//pour le menu accordeon
	$(".accordion ul").hide();
	
	$(".accordion h3").click(function(){
		$(this).next("ul").slideToggle("fast")
		.siblings("ul:visible").slideUp("fast");
		 $(this).toggleClass("active");
		 $(this).siblings("h3").removeClass("active");
	});
	
	$(".accordion .niv2 li a").click(function(){
		$(this).next("ul").slideToggle("fast");
	});
	
	//pour re-ouvrir le menu accordeon au bon endroit 
	var sous_fam = "0"; 
	var id_theme = "0"; 
	
	if ($("#ctl00_pageContent_menu_cod_sou_fam").length > 0) sous_fam = $("#ctl00_pageContent_menu_cod_sou_fam").val();
	//formater le code sous famille
	if (sous_fam.length == 1) sous_fam = "0" + sous_fam;
	if ($("#ctl00_pageContent_menu_id_theme").length > 0) id_theme = $("#ctl00_pageContent_menu_id_theme").val();
	
	//re-ouverture uniquement is on a trouvé les identifiants sous_fam et id_theme
	if ((sous_fam > 0) && (id_theme > 0)){
	    $(".accordion h3.icon" + id_theme).next("ul").slideToggle("fast")
		.siblings("ul:visible").slideUp("fast");
		 $(".accordion h3.icon" + id_theme).toggleClass("active");
		 $(".accordion h3.icon" + id_theme).siblings("h3").removeClass("active");
		 
		 $(".accordion .niv2 li a.sous_fam_" + sous_fam).next("ul").slideToggle("fast");
	}
	
	
	
	//pour le menu accordeon de la partie produit
		$(".accordionhaut h3").click(function(){
		    if ($(this).next(".listeNavigationHaute").length>0){
		    
		        if ((!$(this).hasClass("active"))&&($(".accordionhaut h3").hasClass("active"))){
		            $(".accordionhaut h3.active").next(".listeNavigationHaute").slideToggle("fast");
		            $(".accordionhaut h3.active").toggleClass("active");
		        }
    		    
		        $(this).next(".listeNavigationHaute").slideToggle("fast");
		        $(this).toggleClass("active"); 
		        return false;
            
            }
		});
		
		$(".accordionhaut .listeNavigationHaute").hide();
	    $(".accordionhaut ul ul ").hide(); //sous-sous-menus
	    $(".accordionhaut ul ul ul").show(); //sous-sous-menus
			
			$(".accordionhaut ul li a").click(function(){
			    //$(".accordionhaut ul li").removeClass("active");
			    //$(this).parent("li").addClass("active");
		        $(this).parent().parent().children("li").find("ul.montre").slideToggle("fast").removeClass("montre").addClass("matched");
		        $(this).parent().children("ul").not(".matched").addClass("montre").slideToggle("fast");
		        $(this).parent().parent().children("li").find("ul.matched").removeClass("matched");
		       
		        if ($(this).attr("href") == "#") {
		            return false;
		        }
		    });
		    
	//pour le menu accordeon
	$("#volet").css('display', 'none');
	$("#voletpanier").css('display', 'none');
	$("#voletcompte").css('display', 'none');
		
	// volet
	$(".btn-slide").click(function(){
		$("#volet").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});
	
	// volet
	$(".btn-panier").click(function(){
	    
	    if ($("#contentPanier table").length>0){
	        $("#voletpanier").slideToggle("slow",function(){
               $("#contentPanier").empty();
             });
            $(".btn-panier").toggleClass("active"); 
            return false;
	    }else{
	      $.post('/pages/request/panier-recap.aspx', 
                { type: 'getPanier'}, 
                function(result) { 
                    if (result!=""){
                        $("#voletpanier .fieldvalid a").show();
                        $("#contentPanier").html(result);
                        initClicPanier()
                    }else{
                        // panier vide
                        $("#voletpanier .fieldvalid a").hide();
                    }
                    $("#voletpanier").slideToggle("slow");
                    $(".btn-panier").toggleClass("active"); 
                    return false;
                } 
            );
	    }
	});
	
	// volet
	$(".btn-compte").click(function(){
	    if ($("#voletcompte h2").length>0){
	        $("#voletcompte").slideToggle("slow",function(){
               $("#voletcompte .contenu").empty();
             });
            $(".btn-compte").toggleClass("active"); 
            return false;
	    }else{
	      $.post('/pages/request/compte-recap.aspx', 
                { type: 'getCompte'}, 
                function(result) { 
                    $("#voletcompte .contenu").append(result);
                    initClicPanier()
                    
                    $("#voletcompte").slideToggle("slow");
                    $(".btn-compte").toggleClass("active"); 
                    return false;
                } 
            );
	    }
		
	});
	
	// lightbox si besoin
    $('.seeMore').lightBox(
	    {
		    overlayBgColor: '#FFF',
			    overlayOpacity: 0.6,
			    imageLoading: '/global/img/lightbox/lightbox-ico-loading.gif',
			    imageBtnClose: '/global/img/lightbox/lightbox-btn-close.gif',
			    imageBtnPrev: '/global/img/lightbox/lightbox-btn-prev.gif',
			    imageBtnNext: '/global/img/lightbox/lightbox-btn-next.gif',
			    containerResizeSpeed: 350,
			    txtImage: 'Image(s)',
			    txtOf: 'de'
	    }
	    );	  
    
    // Panier si besoin
    if ($("#Dv_panierPage").length>0){
        $.post('/pages/request/panier-recap.aspx', 
                { type: 'getPanier', page: '1'}, 
                function(result) { 
                    $("#Dv_panierPage").append(result);
                    initClicPanier()
                    return false;
                } 
            );
    }
    
    // Panier si besoin
    if ($("#volet .deconnect").length>0){
        $("#volet").show();
    }
    
    
    // Modifier compte
	$("#compteModifier").click(function(){
	      
        // Bouton
        $("#compteModifier").hide();  
        $("#compteEnregistrer").show();  
        $("#compteAnnuler").show();  
          
        // Champs
        $("#voletcompte .contenu span.compteInfo").each(function(i){
            $(this).hide();
        });
        $("#voletcompte .contenu input").each(function(i){
            $(this).show();
        });
	    
	    return false;
	});  
	
	// Validation compte
	$("#compteEnregistrer").click(function(){
	   	   
	        
	    $.post('/pages/request/compte-recap.aspx', 
                { 
                type: 'saveCompte', 
                nom: $(".Lb_compteSocieteInpt").val(), 
                email: $(".Lb_compteEmailInpt").val(),
                adresse1: $(".Lb_compteAdresse1Inpt").val(),
                adresse2: $(".Lb_compteAdresse2Inpt").val(),
                cp: $(".Lb_compteCpInpt").val(),
                ville: $(".Lb_compteVilleInpt").val(),
                adresseliv1: $(".Lb_compteAdresseLiv1Inpt").val(),
                adresseliv2: $(".Lb_compteAdresseLiv2Inpt").val(),
                cpliv: $(".Lb_compteCpLivInpt").val(),
                villeliv: $(".Lb_compteCpLivInpt").val(),
                tel: $(".Lb_compteTephoneInpt").val(),
                fax: $(".Lb_compteVilleLivInpt").val() }, 
                function(result) { 
                    annulationFormComte();
                    alert("Votre demande a bien été enregistrée");
                    return false;
                } 
            );  
        
	    
	    return false;
	}); 
	
	// Annulation compte
	$("#compteAnnuler").click(function(){
	    annulationFormComte()  
        return false;
	}); 
    
    
    // oubli mot de passe
    $("#Lk_oubliPassword").click(function () {
	      
      $.post('/pages/request/formOubli.aspx', 
            { type: 'oubli'}, 
            function(result) { 
                $.blockUI({ 
                    message: result, 
                    css: {  
                    top:  (jQuery(window).height() - 200) /2 + 'px', 
                    left: (jQuery(window).width() - 400) /2 + 'px', 
                    width: '400px',
                    height: '130px'
                    }  
                }); 
                
                //----- Clic sur fermer
                $("#oubliAnnuler").click(
                    function() {
                        $.unblockUI(); 
                        return false;
                    });	
                    
                //----- Clic sur valider
                $("#oubliValidation").click(
                    function() {
                          // request
                          $.post('/pages/request/gestion.aspx', 
                                { type: 'oubli', url: window.location.href, email: $("#Tb_email").val()}, 
                                function(result) { 
                                    if (result.substring(0,5)=="ERROR"){
                                        $("#oubliMessage").html(result.substring(6));
                                    }else{
                                        $("#oubliMessage").html(result);
                                        setTimeout("$.unblockUI()", 2000) 
                                    }
                                    return false;
                                } 
                            );
                        
                        return false;
                    });	                        
                
                return false;
            } 
            );
          return false;
	    });
	    
	    // changer mot de passe
    $("#changerMotDePasse").click(function () {
	      
      $.post('/pages/request/resetPassword.aspx', 
            { type: 'reset'}, 
            function(result) { 
                $.blockUI({ 
                    message: result, 
                    css: {  
                    top:  (jQuery(window).height() - 200) /2 + 'px', 
                    left: (jQuery(window).width() - 400) /2 + 'px', 
                    width: '400px',
                    height: '130px'
                    }  
                }); 
                
                //----- Clic sur fermer
                $("#resetAnnuler").click(
                    function() {
                        $.unblockUI(); 
                        return false;
                    });	
                    
                //----- Clic sur valider
                $("#resetValidation").click(
                    function() {
                          if ($("#Tb_nouveauMotDePasse").val() !== $("#Tb_nouveauMotDePasseConfirm").val()){
	                          $("#resetPwdMessage").html("Les mots de passe ne correspondent pas");
	                          return;
	                      }                     
                          // request
                          $.post('/pages/request/gestion.aspx', 
                                { type: 'reset', 
                                  url: window.location.href, 
                                  oldPwd: $("#Tb_motDePasseActuel").val(),
                                  newPwd: $("#Tb_nouveauMotDePasse").val()
                                }, 
                                function(result) { 
                                    if (result.substring(0,5)=="ERROR"){
                                        $("#resetPwdMessage").html(result.substring(6));
                                    }else{
                                        $("#resetPwdMessage").html(result);
                                        setTimeout("$.unblockUI()", 5000) 
                                    }
                                    return false;
                                } 
                            );
                        
                        return false;
                    });	                        
                
                return false;
            } 
            );
          return false;
	    });
});


function initClicPanier(){
    // gestion panier --> Suppression
	$(".panierDelete").click(function(){
	      if (confirm("Merci de valider la suppression")){
	          var id = $(this).attr("rel");
	          var elem = $($(this).parents("tr").get(0));
	          // request
              $.post('/pages/request/panier-recap.aspx', 
                    { type: 'deletePanier',id: id}, 
                    function(result) { 
                        alert(result);
                        elem.remove();
                        // Total
                        var newTotal = 0;
                        $(".prix span").each(function(i){
                            var prixElement = parseFloat(strReplace($(this).html(),",","."));
                            newTotal = newTotal + prixElement;
                        });
                        $(".total2 span").html(newTotal);      
                        
                        var nbProduits = $(".design").size();
                        if(nbProduits <= 1) {
                            $("#contentPanier").html();
                            $("#contentPanier").html("Aucun produit dans le panier.");
                            $("#contentPanier").next().hide();
                        }                        
                        
                        return false;
                    } 
                );
            
          }
	    
	});    
	
	// gestion panier --> Modification
	$(".panierModif").click(function(){
	      
	      $(".panierValide").hide();  
	      $(".panierDelete").show();  
          $(".panierModif").show();
	      $(".panierQuantiteMod").hide();
	      $(".panierQuantite").show();
	      
          var id = $(this).attr("rel");
          var elem = $($(this).parents("tr").get(0));
	      elem.find("td .panierQuantite").hide();
	      elem.find("td .panierQuantiteMod").show();
          
          elem.find("td .panierValide").show();  
          elem.find("td .panierDelete").hide();  
          elem.find("td .panierModif").hide();
	    
	});  
	
	// gestion panier --> Validation
	$(".panierValide").click(function(){
	      
	      $(".panierValide").hide();  
	      $(".panierDelete").show();  
          $(".panierModif").show();
	      $(".panierQuantiteMod").hide();
	      $(".panierQuantite").show();
	      
          var id = $(this).attr("id");
          var art = $(this).attr("rel");
          var elem = $($(this).parents("tr").get(0));
	      var newVal = parseInt(elem.find("td .panierQuantiteMod input").val());
	      
	      if (!isNaN(newVal)){
	      
	          // request
              $.post('/pages/request/panier-recap.aspx', 
                    { type: 'modifPanier',id: id,art: art,qte: newVal}, 
                    function(result) { 
                          elem.find("td .panierQuantite").show();
                          elem.find("td .panierQuantiteMod").hide();
                          
                          
                          var prixUnitaire = parseFloat(strReplace(result,",","."));
                          var newTotal = newVal*prixUnitaire;
                          newTotal = newTotal.toFixed(2);
                          
                          elem.find("td.prixUnit span").html(prixUnitaire);
                          elem.find("td.prix span").html(newTotal);
                          elem.find("td span.panierQuantite").html(newVal);
                          
                          // Total
                          var newTotal = 0;
                           $(".prix span").each(function(i){
                                var prixElement = parseFloat(strReplace($(this).html(),",","."));
                                newTotal = newTotal + prixElement;
                           });
                          $(".total2 span").html(newTotal.toFixed(2));
                      
                          
                          elem.find("td .panierValide").hide();  
                          elem.find("td .panierDelete").show();  
                          elem.find("td .panierModif").show();
                        return false;
                    } 
                );
	      }
	      
          
          
	    
	});  
}

function annulationFormComte(){
    // Bouton
    $("#compteModifier").show();  
    $("#compteEnregistrer").hide();  
    $("#compteAnnuler").hide();  
      
    // Champs
    $("#voletcompte .contenu span.compteInfo").each(function(i){
        $(this).show();
    });
    $("#voletcompte .contenu input").each(function(i){
        $(this).hide();
    });
}

//----- Fonction de remplacement de caractère
function strReplace(myValue, myStrFind, myStrReplace){
    var myRetour = myValue;
    if (myRetour){
      var regMod = new RegExp(myStrFind,"gi");
    	myRetour = myRetour.replace(regMod,myStrReplace);
    }
    return myRetour;
}
