$(document).ready(function(){


	// Intro AREA
	var introID = 2;
	$(document).everyTime(4000, function() 
	{	  
	if(introID<=4)   { introSwitch(introID); }	
	introID++;
	if(introID == 5) { introID = 1; }
	});
	
	introSwitch = function(introID)
	{
		$('.intro_menu').removeAttr('id');
		$('.intro_menu[rel='+introID+']').attr('id','menu_intro_current');
		$('.intro_area').fadeOut();	
		$('.intro_area#area_'+introID).fadeIn(500);
	};
	
	
	$('.intro_menu').hover(function()
	{
		var y = $(this).attr('rel');
		$(document).stopTime();
		$('.intro_menu').removeAttr('id');
		$('.intro_menu[rel='+y+']').attr('id','menu_intro_current');
		$('.intro_area').fadeOut();	
		$('.intro_area#area_'+y).fadeIn(500);
	});
	
	
	// Inscription à la newsletter
	var zone_active_nl = false; // permet d'effacer le contenu input lors du "clickOut"
	
	$("#nl_inscription").submit(function() 
	{			
	donnees = $(this).serialize();
					 
		$.ajax({
		   type: "POST",
		   url: "ajax.nl.php",
		   data: donnees,
		   beforeSend: function(){
		   zone_active_nl = true;							
   		   $('#nl_loader').empty();
		   $('#nl_loader').append('<img src="img/loader-a.gif" alt="chargement..." id="nl_loader_img" style="width:16px; margin-left:50px; margin-top:5px;" />');   
		   },
		   success: function(donnees)
		   {
		   $('#nl_loader').empty();
		   $('#nl_msg').empty();
		   $('#nl_msg').fadeIn('slow');
		   $('#nl_msg').append(donnees);
		   
		   
			   $('body').click(function(){
			   if(zone_active_nl == true)
			   {	   
			   $('#nl_msg').hide();
			   $('#nl_mail').attr('value','');
			   zone_active_nl = false;
			   }
			   }); 
		   }
		});
	return false;	
	});





	// Envoi d'un message par le formulaire de contact
	$("#form_contact").submit(function() 
	{	
	donnees = $(this).serialize();
							 
		$.ajax({
		   type: "POST",
		   url: "ajax.contact.php",
		   data: donnees,
		   beforeSend: function(){
		   $('#contact_reponse').empty();	   
		   $('#contact_reponse').append('<img src="img/loader-a.gif" alt="chargement..." id="" style="width:16px; margin-left:50px; margin-top:5px;" />');
		   },
		   success: function(donnees)
		   {
		   $('#contact_reponse').empty();
		   $('#contact_reponse').append(donnees);
		   
		   var end = $("#contact_error").attr("class");
		   
		       if(end == 'success')
			   {
			   $('#contact_nom').attr('value','');
			   $('#contact_mail').attr('value','');
			   $('#contact_msg').attr('value','');
			   }
		   }
		});
	return false;	
	});
	
	
	
	
	
	// Inscription client
	$("#form_inscription").submit(function() 
	{	
	donnees = $(this).serialize();
							 
		$.ajax({
		   type: "POST",
		   url: "ajax.inscription.php",
		   data: donnees,
		   beforeSend: function(){
		   $('#inscription_reponse').empty();	   
		   $('#inscription_reponse').append('<img src="img/loader-a.gif" alt="chargement..." />');
		   },
		   success: function(donnees)
		   {
			   
		   var reponse = donnees;
			   
			   if(reponse == 'success')
			   {
			   $(location).attr('href','compte-client.php');
			   }
			   else if(reponse == 'commande')
			   {
			   $(location).attr('href','commande.php');
			   }
			   else
			   {
			   $('#inscription_reponse').hide();
			   $('#inscription_reponse').empty();
			   $('#inscription_reponse').append(donnees);
			   $('#inscription_reponse').fadeIn('slow');
			   }
			   

		   }
		});
	return false;	
	});
	
	
	
	
	// Identification client
	$("#form_ident").submit(function() 
	{	
	donnees = $(this).serialize();
							 
		$.ajax({
		   type: "POST",
		   url: "ajax.ident.php",
		   data: donnees,
		   beforeSend: function(){
		   $('#ident_reponse').empty();	   
		   $('#ident_reponse').append('<img src="img/loader-a.gif" alt="chargement..." />');
		   },
		   success: function(donnees)
		   {
		
		   var reponse = donnees;
		   
			   if(reponse == 'success')
			   {
			   $(location).attr('href','compte-client.php');
			   }
			   else if(reponse == 'commande')
			   {
			   $(location).attr('href','commande.php');  
			   }
			   else
			   {
			   $('#ident_reponse').hide();
			   $('#ident_reponse').empty();
			   $('#ident_reponse').append(donnees);
			   $('#ident_reponse').fadeIn('slow');
			   }
		   }
		});
	return false;	
	});
	
	

	// Modification du mot de passe
	$("#form_pass_modif").submit(function() 
	{	
	donnees = $(this).serialize();
							 
		$.ajax({
		   type: "POST",
		   url: "ajax.password.change.php",
		   data: donnees,
		   beforeSend: function(){
		   $('#profil_pass_reponse').empty();	   
		   $('#profil_pass_reponse').append('<img src="img/loader-a.gif" alt="chargement..." />');
		   },
		   success: function(donnees)
		   {
		   $('#profil_pass_reponse').hide();
		   $('#profil_pass_reponse').empty();
		   $('#profil_pass_reponse').append(donnees);
		   $('#profil_pass_reponse').fadeIn('slow');
		   
		   	   var end = $("#pass_error").attr("class");
		   
		       if(end == 'success')
			   {
			   $('#old_pass').attr('value','');
			   $('#new_pass').attr('value','');
			   $('#new_pass_confirm').attr('value','');
			   }
		   }
		});
	return false;	
	});
	
	
	
	// Affichage et modifications du profil
	// affichage du formulaire de modification profil
	$('a#a_modif_profil').click(function()
	{
    $('#profil_loader').empty();	
    $("table#tbl_profil").hide();
    $("a#a_modif_profil").hide();
    $("div#profil_form").show();
    $("a#a_annul_profil").show();
    $("a#a_annul_profil").css('display','block');
	});

	// traitement du formulaire
	$("#form_modif_profil").submit(function() 
	{	
	donnees = $(this).serialize();
							 
		$.ajax({
		   type: "POST",
		   url: "ajax.modif.profil.php",
		   data: donnees,
		   beforeSend: function(){
		   $('#profil_modif_reponse').empty();	   
		   $('#profil_modif_reponse').append('<img src="img/loader-a.gif" alt="chargement..." />');
		   },
		   success: function(donnees)
		   {
		   $('#profil_modif_reponse').hide();
		   $('#profil_modif_reponse').empty();
		   $('#profil_modif_reponse').append(donnees);
		   $('#profil_modif_reponse').fadeIn('slow');
		   }
		});
	return false;	
	});
		
	
	
	
	
	
	// Bouton ajouter au panier
	$('a#add_cart').click(function()
	{
	//var taille = $('[@type=radio][@checked]').val();
	var taille = $('input[type=radio]:checked').val();
	var pr_id  = $('a#add_cart').attr('title');

		$.ajax({
		   type: 'POST',
		   url: 'ajax.panier.php',
		   data: {cart_add_taille:taille,cart_add_pr_id:pr_id},
		   dataType :'json',
		   beforeSend: function()
		   {
		   $('#loader_cart').empty();
		   $('#loader_cart').append('<img src="img/loader-circle.gif" alt="chargement..." />');
		   $('#panier_content').empty();
		   $('#panier_content').append('<img src="img/loader-cart.gif" alt="chargement..." style="margin-left:50px;" />');
		   },
		   success: function(data)
		   {
		   $('#loader_cart').hide();
		   $('#loader_cart').empty();
		   $('#loader_cart').append('<img src="img/ico-check.gif" alt="Ajouté" />');  
		   $('#loader_cart').fadeIn('slow');
		   $('#panier_content').empty();
		   $('#panier_content').append(data.cart_content_box);
		   $('#panier_content').fadeIn('slow');
		   }
		   
		});

	return false;
	});
	
	// Bouton supprimer un produit
	$('a.del_cart').click(function()
	{
	var num =  $(this).attr('rel');
	
		$.ajax({
		   type: 'POST',
		   url: 'ajax.panier.php',
		   data: {cart_num_del:num},
		   dataType : 'json',
		   beforeSend: function()
		   {
		   $('#a_del_cart_'+num).replaceWith('<img src="img/loader-circle.gif" alt="chargement..." />') ;
		   $('#panier_content').empty();
		   $('#panier_content').append('<img src="img/loader-cart.gif" alt="chargement..." style="margin-left:50px;" />');
		   },
		   success: function(data)
		   {
			   if(data.panier == 'vide')
			   {
			   $('#cart_content').empty();
			   $('#cart_content').append(data.content_panier);
			   }
			   else
			   {
			   $('#cart_produit_'+num).slideUp('slow');
			   $('#total').empty().append('<b>'+data.total+'€</b>');
			   $('#total_hors_frais').empty().append(data.total_hors_frais+'€');
			   $('#total_frais').empty().append(data.total_frais+'€');
			   }
		   $('#panier_content').empty();
		   $('#panier_content').append(data.cart_content_box);
		   $('#panier_content').fadeIn('slow');
		   }
		});
	return false;
	});

});