/* ------------------------ VARIABLES GLOBALES ------------------------ */
var bIdent = true;
var sErrorAuthAndMdpOublie = "";

/* ------------------------ INIT ------------------------ */
$(document).ready(function(){
});

/* ------------------------ EVENEMENTS ------------------------ */
/* initOverlayLogin
 * 
 * Initialisation des evenements lies a l'overlay d'authentification
 * 
 */
function initOverlayLogin(){	
	// On adapte la hauteur des overlay à la fenêtre courante
	$("#overlayLogin").css({
		"height": viewportHeight+"px"
	});
	$("#trueOverlayLogin").css({
		"height": viewportHeight+"px"
	});
	
	// Lorsque'on clique sur le bouton login, on affiche le formulaire d'authentification
	$(".btnLogin").click(showOverlayLogin);
	
	// Lorsque'on clique sur le lien de login, on affiche le formulaire d'authentification
	$(".linkLogin").click(showOverlayLogin);
	
	// Lorsque'on clique sur en dehors du bloc de formulaire d'authentification, on masque l'overlay de login
	$("#overlayLogin").click(handleClickOnOverlayLogin);
	
	// Click sur le mdp oublié
	$("#btnPasswordOublie").click(switchMdpOublieAndLogin);
	
	// Submit
	$("[name=form_authentification]").submit(function(){return false;});
	$("[name=form_authentification]").submit(handleSubmitAuthentificationAndMdpOublie);
}

/* ------------------------ FONCTIONS ------------------------ */
function showOverlayLogin(){
	// On masque le flash de home
	//$("#flashHome").hide();
	$("#overlayLogin").show();
	if(jQuery.browser['msie'] && parseInt(jQuery.browser['version'],10) < 8)
	{
		$("#trueOverlayLogin").css('opacity',0);
	}
	$("#trueOverlayLogin").show();
	if(jQuery.browser['msie'] && parseInt(jQuery.browser['version'],10) < 8)
	{
		$("#trueOverlayLogin").animate({
			"opacity":0.5
		});
	}
	$("#boxLogin").show();
	$("#boxLogin").animate({
		"top":posTopBoxLogin
	}, speedShowBoxLogin, effectShowBoxLogin);

	// Au cas où il est mis, on retire le mode mot de passe oublié
	removeModeMdpOublie();
	
	// On met le focus sur le champs du mail
	$("[name=form_authentification] [name=emailAuth]").focus();
}


function handleClickOnOverlayLogin(e){
	if (e.target.id == "overlayLogin") {
		hideOverlayLogin();
	}
}

function hideOverlayLogin(){
	$("#boxLogin").animate({
		"top":"-150px"
	}, speedHideBoxLogin, effectHideBoxLogin, function(){
		$("#overlayLogin").hide();
		$("#trueOverlayLogin").hide();
	
		// On affiche le flash de home
		//$("#flashHome").show();
	});
}

function shakeOverlayLogin(callback){
	// On recupere la position
	curOverlayPos = $("#boxLogin").offset();

	// Max deplacement
	maxDeplacement = 10;
	
	// Deplacement à partir duquel on arrête
	zoneNeutre = 4;
	
	// Prochain deplacement
	nextDeplacement = maxDeplacement;

	// Vitesse à laquelle on descend
	stepDecrease = 1;
	
	shakeAnimateToNextDeplacement(nextDeplacement, stepDecrease, zoneNeutre, callback);
}

function shakeAnimateToNextDeplacement(nextDeplacement, stepDecrease, zoneNeutre, callback){
	$("#boxLogin").animate({left:+nextDeplacement}, 75, "easeOutExpo", function(){
		nextDeplacement = nextDeplacement < 0 ? -nextDeplacement - stepDecrease : -(nextDeplacement - stepDecrease);

		if (Math.abs(nextDeplacement) > zoneNeutre) 
			shakeAnimateToNextDeplacement(nextDeplacement, stepDecrease, zoneNeutre, callback);
		else {
			$("#boxLogin").css("left", 0);
			callback();
		}
	});
}

function switchMdpOublieAndLogin(){
	if($(".btnSubmitLogin INPUT").hasClass("inputSubmitLogin"))
		setModeMdpOublie();
	else
		removeModeMdpOublie();
}

function setModeMdpOublie(){
	// On cache le field mdp
	$("#fieldPasswordConnu").slideUp("500", "easeInOutExpo");
	
	// On affiche le texte
	$("#txtPasswordOublie").slideDown("500", "easeInOutExpo");
	
	// On change la classe du bouton submit
	$(".btnSubmitLogin INPUT").removeClass("inputSubmitLogin");
	$(".btnSubmitLogin INPUT").addClass("inputSubmitMdpOublie");
	
	bIdent = false;
}

function removeModeMdpOublie(){
	// On cache le field mdp
	$("#fieldPasswordConnu").slideDown("500", "easeInOutExpo");
	
	// On affiche le texte
	$("#txtPasswordOublie").slideUp("500", "easeInOutExpo");
	
	// On change la classe du bouton submit
	$(".btnSubmitLogin INPUT").addClass("inputSubmitLogin");
	$(".btnSubmitLogin INPUT").removeClass("inputSubmitMdpOublie");
	
	bIdent = true;
}

function handleSubmitAuthentificationAndMdpOublie(){
	// On check le mode dans lequel on est
	// Identification
	if(bIdent){
		// On recupere les champs d'identification
		aPost = {
			email: $("[name=emailAuth]").val(),
			mdp: $("[name=mdpAuth]").val()
		};
		
		// On tente l'ident
		// Action du formulaire
		curAction = $('[name=form_authentification]').attr("action");
		
		// On sauvegarde
		$.ajax({
			type: "POST",
			url: curAction,
			data: aPost,
			dataType:"json",
			success:function(returnedJson){			
				if (!returnedJson["b_error"]) {
					hideOverlayLogin();
					// récupération de la page en cours
					curLink = document.location.href;
					// on supprime le param deco s'il existe
					curLink = curLink.replace('deco','');
					// on supprime les ancres
					var reg = new RegExp('#.*');
					curLink = curLink.replace(reg,'');
					// on charge la page transformée
					document.location.href = curLink;
				}
				else{
					sErrorAuthAndMdpOublie = returnedJson["s_error"];
					shakeOverlayLogin(showErrorOnAuthAndMdpOublie);
				}
			}
		});
	}
	// Oublie de mot de passe
	else{
		// On recupere les champs d'identification
		aPost = {
			email: $("[name=emailAuth]").val()
		};
		
		// On tente l'ident
		// Action du formulaire
		curAction = "ws/identification/send_password.php";
		
		// On sauvegarde
		$.ajax({
			type: "POST",
			url: curAction,
			data: aPost,
			dataType:"json",
			success:function(returnedJson){			
				if (!returnedJson["b_error"]) {
					removeModeMdpOublie();
					setInfoBulleAtElement(returnedJson["s_message"], $(".btnSubmitLogin INPUT"), 0, $(".btnSubmitLogin INPUT").outerHeight()-10, false);
				}
				else{
					sErrorAuthAndMdpOublie = returnedJson["s_error"];
					shakeOverlayLogin(showErrorOnAuthAndMdpOublie);					
				}
			}
		});
	}
}

function showErrorOnAuthAndMdpOublie(){
	setInfoBulleAtElement(sErrorAuthAndMdpOublie, $(".btnSubmitLogin INPUT"), 0, $(".btnSubmitLogin INPUT").outerHeight(), false);	
}
