$(document).ready(function(){

//jQuery('a[rel*=facebox]').facebox();

	/* ==================== On charge le menu gauche ============================================== */

/*		
			$.ajax({
			  url: "menu.html",
			  cache: false,
			  success: function(html){
				$("#menuGauche").append(html);
*/				
				$("#accordion").accordion({
					header: "h3"
				});
				
				$(".menuLink").hover(function () {
				  $(this).css({'font-weight' : 'bolder'}).css({'color' : 'white'});
				}, function () {
				  var cssObj = {
					'font-weight' : ''
				  }
				  $(this).css(cssObj);
				});
/*				
				
			  }
			});
*/



/* ==================== Fonction effet accordeon pour le menu gauche ============================================ */
//				$("#accordion").accordion({
//					header: "h3"
//				});
				
/* ============ JFlow pour la page pagePhotoV2.php ============================================= */
			$("#myController").jFlow({
				slides: "#mySlides",
				controller: ".jFlowControl", // must be class, use . sign
				slideWrapper : "#jFlowSlide", // must be id, use # sign
				selectedWrapper: "jFlowSelected",  // just pure text, no sign
				width: "99%",
				height: "425px",
				duration: 400,
				prev: ".jFlowPrev", // must be class, use . sign
				next: ".jFlowNext" // must be class, use . sign
			});
/* ===================== hover pour les liens du menu de gauche ==================================== */
 
 
	 $(".menuLink").hover(function () {
		  $(this).css({'font-weight' : 'bolder'});
		}, function () {
		  var cssObj = {
			'font-weight' : ''
		  }
		  $(this).css(cssObj);
		});
		
		
});
/* ================================= Oublie de mot de passe ======================================= */
 function oubliePassword(){
            jPrompt('Veuillez saisir votre email:', '', 'Mot de passe perdu!', function(r) {
            if( r ){
                	 $.ajax({
			   type: "POST",
			   url: "oubliePassword.php",
			   async: false,
			   data: 	"emailPerdu="+r
						,

			   success: function(msg){
                    if(msg == "OK"){
                        jAlert("Vos identifiants vous ont été envoyés sur votre boite mail !", "Merci");
                    }else{
                        jAlert("Cet email n'existe pas chez nous !", "Erreur");
                    }
			   },
			   error : function (msg){
				jAlert("Désolé, nous subissons un problème technique. Veuillez réessayer plus tard. Merci.", "Erreur");
			   }

			 });

            };
            });
        }

/* ========================= Verification format email ============================= */

    function verifMail(a)
	{
	isValide = false;
	for(var j=1;j<(a.length);j++){
		if(a.charAt(j)=='@'){
			if(j<(a.length-4)){
				for(var k=j;k<(a.length-2);k++){
					if(a.charAt(k)=='.') isValide=true;
				}
			}
		}
	}
	return isValide;
	}
	
	/* ======================================= fonction qui bloque la page ========================= */

	function blockWindow(message){
		$.blockUI({
		  message: '<h1 style="color: white;">'+message+' ...<h1>',
			  css: {
				border: 'none',
				padding: '15px',
				backgroundColor: '#000',
				'-webkit-border-radius': '10px',
				'-moz-border-radius': '10px',
				opacity: '.8',
				color: '#fff'
				} });
	}
	


				/* ==================== On charge le cartouche d'identification (à droite) ============================================== */

				function changeCartouche(nom, prenom){
					$("#titreCartouche").text("Bonjour");
					$("#titreCartouche").css("margin-bottom", "0em");
					$("#detailCartouche").html("");
					$("#detailCartouche").append("<div span-4 style='margin-bottom: 10px;'>"+prenom+" "+nom+" </div>");
					$("#detailCartouche").append("<div span-4 style='margin-bottom: 6px; font-size: 11px;'> <a class='link' href='suiviCommande.php' style='color: white;'>Suivi commande </a></div>");
					$("#detailCartouche").append("<div span-4 style='margin-bottom: 6px; font-size: 11px;'> <a class='link' href='modifierCompteClient.php' style='color: white;'>Vos coordonnées</a></div>");
					$("#detailCartouche").append("<div span-4 style='margin-bottom: 6px; font-size: 11px;'> <a class='link' href='javascript: deconnexion();' style='color: white;'>déconnexion</a></div>");
					
					$(".link").hover(function () {
					  $(this).css({'font-weight' : 'bolder'});
					}, function () {
					  var cssObj = {
						'font-weight' : ''
					  }
					  $(this).css(cssObj);
					});
				}
		
		function chargeCartouche(prenom, nom){
			$.ajax({
			  url: "cartoucheIdentification.php",
			  cache: false,
			  success: function(html){
				$("#cadreInfoResa").append(html);
				// On change le cartouche si la personne est déjà authentifiée.
				if(nom != null && nom != ""){
					changeCartouche(prenom, nom);
				}
				
			  }
			});
		}
		
		function identification(){
					blockWindow("Authentification en cours");
			
					$.ajax({
					   type: "POST",
					   url: "validationIdentification.php",
					   async: false,
					   dataType : "json",
					   data: 	"email="+$('#emailIdentification').val()+
								"&password="+$('#passwordIdentification').val()+
								"&isConnexAlreadyDone=false"
								,
								
					   success: function(msg){
							if(msg.isOK == "OK"){
								//jAlert("Identification OK !", "Felicitation");
								changeCartouche(msg.prenomPersonne, msg.nomPersonne);
								
								
								
							}else{
								jAlert("Identification erronée.<br/>Vous vous êtes peut être trompé de mot de passe ?", "Erreur");
								
							}
					   },
					   error : function (msg){
						jAlert("Identification erronée.<br/>Vous vous êtes peut être trompé de mot de passe ?", "Attention");
					   }
					
					 });
					$.unblockUI();
				}
				
	function changeCartoucheToDeconect(){
		$("#titreCartouche").text("Déjà inscrit ?");
		$("#titreCartouche").css("margin-bottom", "1em");
		$("#detailCartouche").html("");
		
		var inside='Votre e-mail : <br/>'+
					'<input id="emailIdentification" name="emailIdentification" type="text" value="" size="20" style="font-size:10px; border-width: 1px;"/>'+
					'<br/>Votre mot de passe : <br/>'+
					'<input id="passwordIdentification" name="passwordIdentification" type="password" value="" style="font-size:10px; border-width: 1px;" size="15"/>'+
					'<br/>'+
					'<a href="#" onclick="javascript: oubliePassword();">Mot de passe oublié?</a>'+
					'<br/>'+
					'<div style="text-align: right;"><a class="ui-state-default ui-corner-all menuLink" id="identification" href="javascript: identification();">&nbsp;OK&nbsp;</a></div>'+
					'<br/>'+
					'Pas encore inscrit? <a href="inscription.php">Creez votre compte</a>';
		$("#detailCartouche").html(inside);
	}
	
	function deconnexion(){
					
					
		blockWindow("Deconnexion en cours");

		$.ajax({
			type: "GET",
			url: "deconnexion.php",
			async: false,
			dataType : "json",
			success: function(msg){
				//jAlert("Déconnexion OK!", "Informations");
				changeCartoucheToDeconect();
				window.location.replace("http://www.linstant-dart.com/index.php");
				
		   },
		   error : function (msg){
			jAlert("Un problème technique est survenu.", "Attention");
		   }
		
		 });
		$.unblockUI();
	}

