var current_page = 0;

// init page. Adds graphs, flash
$(document).ready(function() {

	// Adds hover class to the front page buttons
    addHover("#advices #toc li");
    
    // preloads hover bg of frontpage buttons
	$('#toc').after("<span id=\"pre-pause\"><span class=\"1\"></span><span class=\"2\"></span></span>");
	$('#toc').after("<span id=\"pre-eclairage\"><span class=\"1\"></span><span class=\"2\"></span></span>");
	$('#toc').after("<span id=\"pre-position\"><span class=\"1\"></span><span class=\"2\"></span></span>");
	$('#pre-pause').hide();
	$('#pre-eclairage').hide();
	$('#pre-position').hide();
	

	// Appends the head gif animation
	$("#lpensez-y").append("<span id=\"tete_bouge\"></span>");

	// Adds the hover hability to content h4
    addHover("#advices-content li h4");

	// Displays the right page and rub, depending of url  hash
	if( window.location.hash.length > 0 ){
		var rub = idtorub( window.location.hash );
		disp_page( 1, rub );
//		alert( rub );
		return( false);
	}
	else
		disp_page( 0, 0 );

	// Hide page 1 when a link in toc is clicked
	$('#toc a').click(function() {
		var rub = idtorub( $(this).parent().attr("id") );
		disp_page( 1, rub );
		return
	})

	// Displays the right part of the flash animation when a h4 is clicked
	$('#advices-content li h4').click(function() {
		return
	})

	// Animates the head
	animateButton();

}) // on load


// Display the right part of the page, page 0 or page 1
function disp_page( pg, rub ){
	if( pg == 0 ){
		$('#toc').show();
	}
	else{
		$('#toc').hide();
		// stops the moving head
		$("#tete_bouge img").stop();
		$("#tete_bouge").hide();

		// Adds flash animation
		if( $('#flash-container').length <= 0 ){
			$('#advices-content').after("<div id=\"flash-container\"></div>");

			$('#flash-container').flash({
			    src: 'base.swf',
			    version: '7.0.0',
			    width: 930,
			    height: 400,
			    flashvars: {
			    	rub: rub
			    }
			});	
		}
		$('#flash-container').show();
		
		// Adress en bug with floating right change lang link
		$('p.lang').css("top","0px");
		
		current_page = rub;
	}

	return
}

// Adds hover class function to elements
function addHover(elements) {
	$(elements).hover(
	    function(){ $(this).addClass("hover");},
	    function() { $(this).removeClass("hover");}
	);
	return
}


function idtorub( id )
{
	id = id.replace("#","");
	id = id.replace(/^l/,"");
	
	var rub;
	
	if( id == "pause" )				rub = 0;
	else if( id == "eclairage" )	rub = 1;
	else if( id == "position" )		rub = 2;
	else if( id == "pensez-y" )		rub = 3;
	else							rub = 0;

	return( rub );
}


function animateButton( ) {
	
	var imgAnimate = $("#tete_bouge");
	
	imgAnimate.animate(
		{
			"left": -5+130,
			"top": 7+15
		},
		800,
		"linear",
		function()
		{
			imgAnimate.animate(
				{
					"left": 5+130,
					"top": -7+15
				},
				800,
				"linear",
				function()
				{
					imgAnimate.animate(
						{
							"left": -8+130,
							"top": -15+15
						},
						800,
						"linear",
						function()
						{
							imgAnimate.animate(
								{
									"left": 8+130,
									"top": -5+15
								},
								800,
								"linear",
								animateButton
							);
						}
					);
				}
			);
		}
	);
}
