/* ------------------------ VARIABLES GLOBALES ------------------------ */
var sErrorCreditVote = '';
var speedShowBoxCreditVote = 1500;
var effectShowBoxCreditVote = "easeOutExpo";

var speedHideBoxCreditVote = 800;
var effectHideBoxCreditVote = "easeInExpo";


var viewportHeight;
var boxCreditVoteHeight = 500;

/* ------------------------ INIT ------------------------ */
$(document).ready(function(){
	posTopBoxCreditVote = (windowHeight - boxCreditVoteHeight)/2;
});

/* ------------------------ EVENEMENTS ------------------------ */
/* initOverlayCreditVote
 * 
 * Initialisation des evenements lies a l'overlay de gain de crédit de vote
 * 
 */
function initOverlayCreditVote(){
	// On adapte la hauteur des overlay à la fenêtre courante
	$("#overlayCreditVote").css({
		"height": viewportHeight+"px"
	});
	
	// Lorsque'on clique sur le gagnez des crédit de vote, on affiche le formulaire de gain de crédit de vote
	$(".btnCreditVote").click(showOverlayCreditVote);
	
	// Lorsque'on clique sur en dehors du bloc de formulaire de gain de crédit de vote, on masque l'overlay
	$("#overlayCreditVote").click(handleClickOnOverlayCreditVote);
	
	// Submit
	$("[name=form_gain_credit_vote]").submit(function(){return false;});
	$("[name=form_gain_credit_vote]").submit(handleSubmitGainCreditVote);
}

/* ------------------------ FONCTIONS ------------------------ */

function showFormGainCredit(){
	$('[name=form_gain_credit_vote]').show();
	$('[name=form_gain_credit_vote] [name=list_emails]').val('');
	$('.messageNumCredits').hide();
}

function showOverlayCreditVote(){
	showFormGainCredit();
	$("#overlayCreditVote").show();
	$("#boxCreditVote").animate({
		"top":posTopBoxCreditVote
	}, speedShowBoxCreditVote, effectShowBoxCreditVote);
	
	// On met le focus sur le textarea des e-mails
	$("[name=form_gain_credit_vote] [name=list_emails]").focus();
	
	return false;
}


function handleClickOnOverlayCreditVote(e){
	if (e.target.id == "overlayCreditVote") {
		hideOverlayCreditVote();
	}
}

function hideOverlayCreditVote(){
	$("#boxCreditVote").animate({
		"top":"-150px"
	}, speedHideBoxCreditVote, effectHideBoxCreditVote, function(){
		$("#overlayCreditVote").hide();
	});
}

function shakeOverlayCreditVote(callback){
	// On recupere la position
	curOverlayPos = $("#boxCreditVote").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){
	$("#boxCreditVote").animate({left:+nextDeplacement}, 75, "easeOutExpo", function(){
		nextDeplacement = nextDeplacement < 0 ? -nextDeplacement - stepDecrease : -(nextDeplacement - stepDecrease);

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

function handleSubmitGainCreditVote(){
	// On recupere la liste d'emails
	aPost = {
		list_emails: $("[name=list_emails]").val()
	};

	// Action du formulaire
	curAction = $('[name=form_gain_credit_vote]').attr("action");
	
	// On envoie les mails
	$.ajax({
		type: "POST",
		url: curAction,
		data: aPost,
		dataType:"json",
		success:function(returnedJson){			
			if (!returnedJson["b_error"]) {
				$('[name=form_gain_credit_vote]').hide();
				$('.messageNumCredits').html(returnedJson["s_msg"]).show();
				initRetourGainCredit();
			}
			else{
				sErrorCreditVote = returnedJson["s_error"];
				shakeOverlayCreditVote(showErrorOnCreditVote);
			}
		}
	});
}

function showErrorOnCreditVote(){
	setInfoBulleAtElement(sErrorCreditVote, $(".btnSubmitCreditVote INPUT"), 0, $(".btnSubmitCreditVote INPUT").outerHeight(), false);	
}

function initRetourGainCredit(){
	$('.retourGainCredit').hover(
		function(){
			$(this).css('color','#2E6081');
		},
		function(){
			$(this).css('color','#999999');
		}
	);
	
	$('.retourGainCredit').click(function(){
		showFormGainCredit();
	});
}

