/* ------------------------ VARIABLES GLOBALES ------------------------ */
// N° de page en cours
var curPageNo = "01";

// Positions
var xHiddenLeft = -521;
var xVisible = 0;
var xHiddenRight = 521;

// Temps de déplacement
var timeToMovePage = 500;

// String de message et d'erreur
var sErrors = "";
var sMessages = "";

// Positions de la box de message
var xBoxMsgHiddenLeft = -521;
var xBoxMsgVisible = 0;
var xBoxMsgHiddenRight = 521;

/* ------------------------ INIT ------------------------ */
$(document).ready(function(){
	initBtnSuivant();
	
	initClickOnSuivant();
	
	initClickOnRadioBouton();
	
	initClickOnEnregistrer();
	
	initBoxMsgOkButton();
	
	initMLOver();
});

/* ------------------------ EVENEMENTS ------------------------ */
function initBtnSuivant(){
	$(".btnNext").css("opacity", 0);
}

function initClickOnSuivant(){
	$(".btnSuivant").click(goToNextPage);	
}

function initClickOnRadioBouton(){
	// Roll-over / Roll-out sur les radio
	$(".radioBoutonQuiz").click(function () {
		// On recupere le nom de l'input correspondant
		curInputId = $(this).attr("inputId");
		
		// On recupere la valeur à définir
		curValue = $(this).attr("value");
		
		// Si la radio n'est pas clickée
		if(!$(this).hasClass("radioOn")){				
			// On declick toutes les radio du groupe
			$("[inputId="+curInputId+"]").each(function(){					
				// On la declick
				$(this).removeClass("radioOn");
				$(this).attr("src", "images/radio_off.jpg");
			});
			
			// On la click
			$(this).addClass("radioOn");
			$(this).attr("src", "images/radio_on.jpg");
			
			// On met la valeur dans l'hidden correspondant
			$("#"+curInputId).attr("value", curValue);
		}
		
		// On check si le bouton suivant doit être affiché
		showBtnSuivantIfNeeded();
	});
}

function initClickOnEnregistrer(){
	$(".btnEnregistrer").click(saveForm);
}

function initBoxMsgOkButton(){
	$("#bottomBox").click(hideBoxMsg);
}

function initMLOver(){
	$("#mentions_legales_lib_content").hover(function(){
		// On init
		$("#mentions_legales_content").css("opacity", 0);
		$("#mentions_legales_content").css("display", "block");
				
		$("#mentions_legales_content").animate({
			opacity:1
			}, 200, "linear", function(){});
	}, function(){				
		$("#mentions_legales_content").animate({
			opacity:0
			}, 100, "linear", function(){
				$("#mentions_legales_content").css("display", "none");
			});
	});
}

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

function goToNextPage(){
	// On determine la page à afficher
	switch(curPageNo){
		case "01":
			nextPage = "02";			
			break;
			
		case "02":
			nextPage = "03";
			break;
	}

	// On bouge les pages
	$("#page"+curPageNo).animate({
		left:xHiddenLeft
		}, timeToMovePage, "easeInOutCirc", function(){});
		
	$("#page"+nextPage).animate({
		left:xVisible
		}, timeToMovePage, "easeInOutCirc", function(){
			curPageNo = nextPage;
		});

	return false;
}

function showBtnSuivantIfNeeded(){	
	// Premier bouton suivant
	if($(".btnNext:has(#btnSuivant01)").css("display") == "none"
		&& $("#reponses_q1").attr("value") != ""
		&& $("#reponses_q2").attr("value") != ""){
			$(".btnNext:has(#btnSuivant01)").css("display", "block");
		
			$(".btnNext:has(#btnSuivant01)").animate({
				opacity:1
				}, 250, "easeInCirc", function(){});
		}
		
	// Deuxième bouton suivant
	if($(".btnNext:has(#btnSuivant02)").css("display") == "none"
		&& $("#reponses_q3").attr("value") != ""
		&& $("#reponses_q4").attr("value") != ""){
			$(".btnNext:has(#btnSuivant02)").css("display", "block");
		
			$(".btnNext:has(#btnSuivant02)").animate({
				opacity:1
				}, 250, "easeInCirc", function(){});
		}
}

function saveForm(){
	$.post("ws/save_participation.php", $("#formulaire_quiz").serialize(), function(data){
				var xmlSaveRetour;
				var b_error = false;

				if ($.browser.msie) {
					xmlSaveRetour = new ActiveXObject("Microsoft.XMLDOM");
					xmlSaveRetour.async = false;
					xmlSaveRetour.loadXML(data);
				} else {
					xmlSaveRetour = data;
				}

				// On vide la box de message
				$("#contentBox").html('');

				// On recupere les erreurs
				$(xmlSaveRetour).find("error").each(function(){
					$("#contentBox").addClass('error');
					$("#contentBox").append('<div class="line">'+$(this).text()+'</div>');
					b_error = true;
				});
				
				// On recupere les messages
				$(xmlSaveRetour).find("message").each(function(){
					$("#contentBox").removeClass('error');
					$("#contentBox").append('<div class="line">'+$(this).text()+'</div>');
				});
				
				// S'il n'y a pas d'erreur, on append l'iframe demandée par le client
				if (!b_error){
					if ($("#form_contact_bosch").is(":checked") == true){
						$("BODY").append('<iframe src="http://nodes.reactivpub.fr/scripts/tracking.php?params=1026|4&track='+escape($("#form_email").attr("value"))+'" width="1" height="1" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"></iframe>');
					} 	
				}
				else {
					$(".inputTextLineForm").each(function(){
						$(this).removeClass("errorInputTextLineForm");
					});
					
					$(".txtLineFormOptIn").each(function(){
						$(this).removeClass("errorTxtLineFormOptIn");
					});
					
					$(xmlSaveRetour).find("field").each(function(){
						$("#" + $(this).text()).addClass("errorInputTextLineForm");
						$("#" + $(this).text() + ".txtLineFormOptIn").addClass("errorTxtLineFormOptIn");
					});
				}
				
				// On affiche les messages/erreurs
				showBoxMsg();
			}, "text");
			
	return false;
}

function showBoxMsg(){
	$("#boxMsg").css("left", xBoxMsgHiddenRight);
	$("select").css("display", "none");
	
	$("#boxMsg").animate({
		left:xBoxMsgVisible
		}, 500, "easeOutCirc", function(){});
}

function hideBoxMsg(){
	$("#boxMsg").animate({
		left:xBoxMsgHiddenLeft
		}, 250, "easeInCirc", function(){});
	
	setTimeout('$("select").css("display", "block")', 200);
}

