/**
 * 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 class="page" containers and a <form id="test-form"> to catch submits
 */

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;
function animateButton()
{
	if( etape >= res.length ){
		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]}, res[etape][1]);
	
	setTimeout(animateButton,res[etape][1]);
	etape++;
	
	return(0);
}




$(document).ready(function(){
/*
	if( $(".test-result").length ){
		$(".test-result").append('<span class="tete"><span><span><img src="<?php echo BASE_URI ?>images/bonhomme1.gif" alt=""/></span></span>');
		$("#hommemalade").append('<img id="mecfond" src="../images/visual-tiredness/mecmalade_fond.gif"/>');
		$("#mecmalade").css({ position: "absolute", top: "31px", left: "35px", width: "145px", height: "153px" });
		$("#mecfond").css({ position: "absolute", top: "0px", left: "0px", width: "220px", height: "230px"});
		$(".symptoms").css({ background: "none"});

	}
*/
	animateButton();
	var pages = $('.page')
	var current_page = false;

    /**
     * Fades to the current page using a JQuery 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)
        $("#inside-content").css("display", "block") // IE6 fix
//		animateButton();
    }

    // when trying to submit, switch to next page instead
	$('#test-form').submit(function() {
		var page = pageInHash() + 1
		if (page >= pages.length) 
			return true
		window.location.hash = 'page' + page
		switchPage(page)
		return false
    })
    
    // check every 500 milliseconds, change page if needed
    switchPage(pageInHash());
    setInterval(function() { if (current_page !== pageInHash()) switchPage(pageInHash()) }, 500);
})
