var carousel_param = {
    direction:"horizontal",
    autoSlide: true,
    loop: true,
    autoSlideInterval: 6000,
    pagination: true,
    animSpeed: "slow",
    slideEasing: "easeOutQuint"
};

function mainmenu(){
    $(" #nav ul ").css({
        display: "none"
    }); // Opera Fix
    $(" #nav li").hover(function(){
        $(this).find('ul:first').css({
            visibility: "visible",
            display: "none"
        }).slideDown(400);
    },function(){
        $(this).find('ul:first').css({
            visibility: "hidden"
        });
    });
}

$(document).ready(function() {

	  document.write(lang);

    $(".actus").carousel(carousel_param);

    mainmenu();
    
    $(function(){
        $("#carrousel_home").carousel({
            autoSlide: true,
            autoSlideInterval: 5000,
            pagination: true
        });
    });
    
    $("#inscription-form").validate({
        rules : {
            CLI_MAIL2: {
                equalTo: "#CLI_MAIL"
            },
            CLI_PASS2: {
                equalTo: "#CLI_PASS"
            }
        }
    });
 

    
    $("#newsletter-form form").validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "/include/tpl/tpl_ajaxNewsletter.php",
                data: "email="+$("#email").val(),
                success: function(msg, textStatus, jqXHR){
                    if (msg == 1) {
                        $("#newsletter-form form").trigger("reset");
                        $("#button-submit").attr("disabled","disabled");
                        switch (lang) {
                          case "fr" :  alert("Votre inscription a bien été prise en compte.");
                           break
                           case "en" :  alert("Your subscription is registered..");
                           break
                            
                            }
                       }
                        
                    
                    else{ switch (lang) {
                          case "fr" :   alert("Il y a eu un problème lors de l'inscription.");
                           break
                           case "en" :  alert("An error is occured");
                           break
                            
                            }
                       
                    }
                    return false;
                }
            
            });
            return false;
        }
    });
  	$("#SendFriendForm").validate({
		submitHandler: function(form) {
		
			var str = $("#SendFriendForm").serialize();
			$.ajax({
				   type: "POST",
				   url: "/include/tpl/tpl_ajaxMail.php",
				   data: "action=sendfriend&"+str,
				   success: function(msg){					
						alert(msg);
						$("#sendFriends").dialog('close');
						return false;
				   }
			});
			return false;
		}	
		
	});
	
	$("#sendFriends").dialog({
		autoOpen: false,
		height: 400,
		width: 400,
		modal: true,
		buttons: {
			'Ok': function() {
				$("#SendFriendForm").trigger("submit");
			},
			
		},
		  "Close": function() { $(this).dialog("close"); }
		
	});
        
	$('a.sendFriends').click(function() {
		$('#sendFriends').dialog('open');
		return false;
	});

 

    $("#contact-form").validate({
        submitHandler: function(form) {
        
            str = $("#contact-form").serialize();
            $.ajax({
                type: "POST",
                url: "/include/tpl/tpl_ajaxContact.php",
                data: str,
                success: function(msg, textStatus, jqXHR){
                    if (msg == 1) {
                        $("#contact-form").trigger("reset");
                        $("#button-submit").attr("disabled","disabled");
                        
                          switch (lang) {
                          case "fr" :   alert("Votre message à bien été envoyé.");
                           break
                           case "en" :  alert("Message send");
                           break
                            
                            }
                    }
                    else{switch (lang) {
                          case "fr" :   alert("Il y a eu un problème lors de l'envoie de votre message.");
                           break
                           case "en" :  alert("An error is occured");
                           break
                            
                            }
                        
                    }
                    return false;
                }
            
            });
            return false;
        }
    });
    
    
    $('#login-form').validate({
        submitHandler: function(form) {
            
            str = $("#login-form").serialize();

            $("#login-tmp").fadeIn();
            $('#ajax-loader').remove(); // on retire le loader
            $("#login-tmp").html('');

            // on envoie la valeur recherché en GET au fichier de traitement
            $.ajax({
                type : 'POST', // envoi des données en GET ou POST
                url : '/include/tpl/tpl_ajaxLogin.php' , // url du fichier de traitement
                data : str, // données à envoyer en  GET ou POST
                beforeSend : function() { // traitements JS à faire AVANT l'envoi
                    $('#login-tmp').html('<img src="../../images/annecycosmetic/ajax-loader.gif" alt="loader" id="ajax-loader" />'); // ajout d'un loader pour signifier l'action
                },
                success : function(msg, textStatus, jqXHR){ // traitements JS à faire APRES le retour d'ajax-search.php
                    $('#ajax-loader').remove(); // on enleve le loader
                    if (msg == 1) {
                        window.location=document.location.href;
                    }
                    else{
                        $('#ajax-loader').remove();
                        $('#login-tmp').html(msg).delay("2500"); // affichage des résultats dans le bloc
                        $("#login-tmp").fadeOut();
                    }
                }
            });
        }
    });
    
    $('#pre-inscription-form').validate();
    
    $('#inscription-form').validate();
    
    //ajout d'un article au panier
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    
    $( "#dialog-form" ).dialog({
        autoOpen: false,
        height: 220,
        width: 260,
        modal: true,
        buttons: {
            "Ajouter au panier": function() {
                    
                str = $("#dialog-form form").serialize();

                $.ajax({
                    type : 'POST', // envoi des données en GET ou POST
                    url : '/include/tpl/tpl_ajaxPanier.php' , // url du fichier de traitement
                    data : str, // données à envoyer en  GET ou POST
                    success : function(msg, textStatus, jqXHR){ // traitements JS à faire APRES le retour
                        if (msg == 1) {
                            $( "#dialog-form" ).dialog( "close" );
                            $("#dialog-message").dialog({
                    			modal: true,
                    			dialogClass: 'confirm-panier',
                    			buttons: {
                    				Ok: function() {
                    					$( this ).dialog( "close" );
                    				}
                    			}
                            });
                        }
                    }
                });
            }
        }
    });

    $( "a.addcart" ).click(function() {
        var id = this.getAttribute('rel');
        $("#id_produit").attr("value",id);
        $( "#dialog-form" ).dialog( "open" );
        return false;
    });
        
    $('.produit-pict a').zoombox();    
        
    $("#ajout-panier-unique").submit(function(){
    	
        str = $("#ajout-panier-unique").serialize();  
        $.ajax({
            type : 'POST', // envoi des données en GET ou POST
            url : '/include/tpl/tpl_ajaxPanier.php' , // url du fichier de traitement
            data : str, // données à envoyer en  GET ou POST
            success : function(msg){ // traitements JS à faire APRES le retour
                if (msg == 1) {
                	$("#dialog-message").dialog({
            			modal: true,
            			dialogClass: 'confirm-panier',
                                height: 500,
            			buttons: {
            				Ok: function() {
            					$( this ).dialog( "close" );
            				}
            			}
                    });                    
                }
            }
        });
        
        return false;
    });    
    
    $('button.delArticlePanier').click(function() {
        
        var id = this.getAttribute('rel');
        // on envoie la valeur recherché en GET au fichier de traitement
        $.ajax({
            type : 'POST', // envoi des données en GET ou POST
            url : '/include/tpl/tpl_ajaxPanier.php' , // url du fichier de traitement
            data: "id="+id+"&type=removeLine",
            success : function(msg, textStatus, jqXHR){ // traitements JS à faire APRES le retour
                
                if (msg != "") {
                    var tab = msg.split("|");
                   $("#listePanier tr#article"+id+" span.quantite").html(tab[0]);
                   $("#listePanier tr#article"+id+" .total span").html(tab[1]);
                   $("#panier-total .total span").html(tab[2]);
                   $("#panier-livraison .total span").html(tab[3]);
                   $("#panier-final .total span").html(tab[4]);
                }
            }
        });
             
    });
    
    $('button.plusArticlePanier').click(function() {
        
        var id = this.getAttribute('rel');
        var qte = $("#listePanier tr#article"+id+" span.quantite").text()
        qte++;
        // on envoie la valeur recherché en GET au fichier de traitement
        $.ajax({
            type : 'POST', // envoi des données en GET ou POST
            url : '/include/tpl/tpl_ajaxPanier.php' , // url du fichier de traitement
            data: "id="+id+"&type=addOne",
            success : function(msg, textStatus, jqXHR){ // traitements JS à faire APRES le retour
                
                if (msg != "") {
                    var tab = msg.split("|");
                   $("#listePanier tr#article"+id+" span.quantite").html(tab[0]);
                   $("#listePanier tr#article"+id+" .total span").html(tab[1]);
                   $("#panier-total .total span").html(tab[2]);
                   $("#panier-livraison .total span").html(tab[3]);
                   $("#panier-final .total span").html(tab[4]);
                }
            }
        });
             
    });
    
    $('button.moinsArticlePanier').click(function() {
        
        var id = this.getAttribute('rel');
        var qte = $("#listePanier tr#article"+id+" span.quantite").text();
        qte--;
        // on envoie la valeur recherché en GET au fichier de traitement
        $.ajax({
            type : 'POST', // envoi des données en GET ou POST
            url : '/include/tpl/tpl_ajaxPanier.php' , // url du fichier de traitement
            data: "id="+id+"&type=removeOne",
            success : function(msg, textStatus, jqXHR){ // traitements JS à faire APRES le retour
               
               if (msg != "") {
                    var tab = msg.split("|");
                   $("#listePanier tr#article"+id+" span.quantite").html(tab[0]);
                   $("#listePanier tr#article"+id+" .total span").html(tab[1]);
                   $("#panier-total .total span").html(tab[2]);
                   $("#panier-livraison .total span").html(tab[3]);
                   $("#panier-final .total span").html(tab[4]);
                }
            }
        });
             
    });
    
    $('#commandes a.voir-plus').click(function(){
        var id = this.getAttribute('rel');
        $(this).fadeOut(400, function(){
            $(this).next("a").fadeIn(400);
        });
        $("#commande-details"+id).slideDown(400);
    });
    
    $('#commandes a.voir-moins').click(function(){
        var id = this.getAttribute('rel');
        $(this).fadeOut(400, function(){
            $(this).prev("a").fadeIn(400);
        });
        $("#commande-details"+id).slideUp(400);
    });
  
});
