/**
 * Switch pages by looking at the current hash, using JQuery
 *
 * Current page has a display: block, others are display: hidden.
 * Switches to the next page when the user tries to submit.
 *
 * Needs <div class="page" id="pageN"> containers and a
 * <form id="game-form"> to catch submits
 */
 
 // On page load :
$(document).ready(function(){
	var savoir = $("#savoirplus a").text();
	$("#savoirplus a").text("> "+savoir);
	
	addHover("#savoirplus");
});
 

var duree = 600;
var val = -80;

var res = [ // Aller
			[ parseInt(0.5*val), parseInt(0.1*duree) ],
			[ parseInt(0.8*val), parseInt(0.2*duree) ],
			[ parseInt(0.9*val), parseInt(0.2*duree) ],
			[ val,     			 parseInt(0.5*duree) ],
			// Retour
			[ parseInt(0.9*val), parseInt(0.5*duree) ],
			[ parseInt(0.8*val), parseInt(0.2*duree) ],
			[ parseInt(0.5*val), parseInt(0.2*duree) ],
			[ 0,       			 parseInt(0.1*duree) ]
]


var etape = 0;
HeadTimeout = 0;
function animateButton()
{
	if( etape >= res.length ){
		clearTimeout(HeadTimeout);
		etape = 0;
	}

	var imgAnimate = 0;

	if( $(".tete span img").length )
		imgAnimate = $(".tete span img");
	else if( $(".tete-result").length ){
		imgAnimate = $(".tete-result img");
	}
	else
		return(0);

	imgAnimate.animate({top: res[etape][0]+"px"}, res[etape][1])
	
	HeadTimeout = setTimeout("animateButton()",res[etape][1]);
	etape++;
	
	return(0);
}


$(function() {
    
    var ERROR_NAME_EMPTY = "Veuillez spécifier votre nom et votre prénom"
    var ERROR_ADDRESS_EMPTY = "Veuillez spécifier votre adresse"
    var ERROR_WRONG_EMAIL = "Votre adresse email n'est pas valide"
    
    var SUBMIT_TEXT_NEXT = "Suivant"
    var SUBMIT_TEXT_END = "Valider"
    
    var pages = $('.page')
	var current_page = false
	animateButton();
	/**
	 * Returns the current page by looking at the hash or the first with
	 * the class "current"
	 */
    function getPage() {
    	if($(".page.current").length)
    		return(parseInt($(".page.current").attr("id").replace('page', ''), 10))
    	else
		    return pageInHash()
    }

	/**
	 * Switch to the current page using a JQuery fade effect
	 * @param page_num int page number to switch to
	 */
	function switchPage(page_num) {
	    current_page = page_num
   		pages.hide().eq(page_num).fadeIn(300)
   		
   		// handle submit button
    	if (page_num == 2)
    		$("p.submit").hide()
    	else if (page_num == 0)
            $("input.submit").attr("value", SUBMIT_TEXT_NEXT)
        else
            $("input.submit").attr("value", SUBMIT_TEXT_END)
        
        $("#inside-content").css("display", "block") // IE6 fix
	}

	/**
	 * Check the name, address and email fields in the form
	 * and launches an alert() if an error has been found.
	 *
	 * @return Boolean to false if the field contained errors
	 */
	function checkForm() {
		if ($("#fg_nomp").val().length == 0) {
			alert(ERROR_NAME_EMPTY)
			return false
		}
		if ($("#fg_adresse").val().length == 0) {
			alert(ERROR_ADDRESS_EMPTY)
			return false
		}
		var str = $("#fg_email").val()
		var res = str.search(/^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+\.[a-zA-Z]{2,4}$/)
		if (str.length > 0 && res == -1) {
			alert(ERROR_WRONG_EMAIL)
			return false
		}
		return true
	}


	// when trying to submit, switch to next page instead or check the form
	// and submit if on second page
	$('#game-form').submit(function() {
		var page = getPage() + 1
		if (page == 2)
			return checkForm()
		window.location.hash = 'page' + page
		switchPage(page)
		return false
	})

	// check every 500 milliseconds, change page if location hash changed
	switchPage(getPage())
	setInterval(function() { if (current_page !== getPage()) switchPage(getPage()) }, 500)
})
