$(document).ready
(
    function()
    {
	/*
	   Gestion de la navigation
	*/

	// gestion du hover
	var childNavHeight = $("#view-navigation-header ul ul").height();
	var accroche = $("#zone-visuel div.accroche");
	var accrochePos = accroche.position();
	var accrocheDown = false;

	$("#view-navigation-header > ul > li").hover
	(
	    function()
	    {
		var $t = $(this).find("> ul").stop(true).css("z-index", 2);
		if ( !$t.length )
		{
		    return;
		}

		if ( !$t.is(":visible") )
		{
		    $t.height(0).show();
		}

		$t.animate({height:childNavHeight});

		// on définit qu'on ne doit pas remonter l'accroche
		accroche.clearQueue("state").synchronize
		(
		    "state",
		    function()
		    {
			$(this).data("up", false);
		    }
		)
		.stop(true).animate({top:accrochePos.top + childNavHeight});
	    },
	    function()
	    {
		var $t = $(this).find("> ul").css("z-index", 1);
		if ( !$t.length )
		{
		    return;
		}

		// on définit qu'on doit remonter l'accroche
		accroche.synchronize
		(
		    "state",
		    function()
		    {
			$(this).data("up", $t.get(0));
		    }
		);

		$t.delay(300).queue
		(
		    function(next)
		    {
			accroche.synchronize
			(
			    "state",
			    function()
			    {
				if ( accroche.data("up") == $t.get(0) )
				{
				    accroche.stop(true).animate({top:accrochePos.top});
				}
			    }
			);

			next();
		    }
		)
		.slideUp();
	    }
	);

	// adptation des tailles
	$("#view-navigation-header ul ul").each
	(
	    function()
	    {
		var $t	= $(this);
		var w	= $t.width();
		var lis = $t.find("> li");
		var lw	= Math.floor(w / lis.length);

		lis.width(lw);
	    }
	);

	/*
	   Gestion des langues
	*/

	var langDiv = $("div#languages");
	var langUL  = langDiv.find("ul");
	var popup   = $("<div></div>").addClass("popup").appendTo(langDiv);

	langUL.clone().appendTo(popup).find("li.selected").remove();
	
	var removed = langUL.find("li").not("li.selected");
	var height  = removed.height() * removed.length;
	removed.remove();

	// on rajoute le bouton
	var button = $("<a></a>").attr("href", "#").addClass("button").insertAfter(langUL)
	.click
	(
	    function(e)
	    {
		e.stopPropagation();
		e.preventDefault();

		$t = $(this);

		popup.fadeIn();
		$t.slideUp();
		langDiv.animate({top:"-=" + height});

		window.setTimeout
		(
		    function()
		    {
			popup.fadeOut();
			$t.slideDown();
			langDiv.animate({top:"+=" + height});
		    },
		    3000
		);
	    }
	);

	langDiv.mouseenter
	(
	    function()
	    {
		if ( button.is(":visible") && !button.is(":animated") )
		{
		    button.click();
		}
	    }
	);

	height -= button.height();
	height  = Math.round(height/2); 

	langDiv.css("top", langDiv.position().top + height);

	/*
	    Taille du footer
	*/

	var $footer = $("#legalContainer");
	$(window).resize
	(
	    function()
	    {
		var top		    = $footer.offset().top;
		var footerHeight    = $footer.find("#legal").height();
		var bodyHeight	    = Math.max($(window).height(), $("body").get(0).scrollHeight);

		if ( bodyHeight - top > footerHeight )
		{
		    $footer.height(bodyHeight - top);
		}
		else
		{
		    $footer.height("auto");
		}
	    }
	)
	.resize();

	/*
	   Transparence des titres
	*/

	$("#view-title").each
	(
	    function()
	    {
		var $t	= $(this);
		var o	= $t.css("opacity");
		var p	= $t.position();

		var d = $("<div></div>").css
		({
		    background	: "black",
		    opacity	: o,
		    position	: 'absolute',
		    top		: p.top,
		    left	: p.left
		})
		.width($t.width()).height($t.height())
		.insertBefore($t);

		$t.css
		({
		    background	: "transparent",
		    opacity	: 1
		});
	    }
	);
    }
);

