// JavaScript Document
var position="c";
var idCurrent="default";
var idSubmenu=null;
var moving=false;
function fontenelle(elm)
{
	if($(".corps:hidden").length>0)
		$(".corps").show();
	e=$(elm);
	fonction=function(){};
	if(arguments.length>1)
		fonction=arguments[1];
	
	off=e.offset();
	offp=e.parent().offset();
	vtop=offp.top-off.top;
	vleft=offp.left-off.left;
	if ($(".slided").length>0)
		$(".slided").removeClass("slided");
	
	e.addClass("slided")
	e.parent().animate({"top":vtop+"px","left":vleft+"px"},1000,fonction);
}
function getAnnim(e)
{
	n=e.parent().queue("fx").length;
	return (n==0)
}
function goCenter()
{
	afn=goCenter;
	el=$("#center");
	if(arguments.length>0 && typeof arguments[0] == "function")
		afn=arguments[0];
	if(position=="c" || !getAnnim(el))
		return false;
	switch(position)
	{
		case 't':
			$("#topBlock").fadeOut(1000);
		break;
		case 'l':
			$("#leftBlock").fadeOut(1000);
		break;
		case 'r':
			$("#rightBlock").fadeOut(1000);
		break;
		case 'b':
			$("#bottomBlock").fadeOut(1000);
		break;
	}
	
	$(".search_example").fadeIn(1000);

	fontenelle(el,afn);
	position="c";
	$(".active").removeClass("active");
	unlightPresta();
	return false;
}
function goLeft(part)
{
	el=$("#leftBlock");
	if(position=="l" || !getAnnim(el))
		return false;
	if(position!="c")
		return goCenter(goLeft);
	el.fadeIn(1000,lightPresta);
	$(".search_example").fadeOut(1000);
	fontenelle(el);
	position="l";
	$(".active").removeClass("active");
	$(".alpha_plan a").addClass("active");
	if (part!='')
		$("a[rel='"+part+"']").click();
	else
		$("a[rel='alpha_presentation']").click();
	return false;
}
function goTop(part)
{
	el=$("#topBlock");
	if(position=="t" || !getAnnim(el))
		return false;
	if(position!="c")
		return goCenter(goTop);
	
	
	
	$(".search_example").fadeOut(800,function() {
		el.fadeIn(1000);
		lightPresta;
		fontenelle(el);
		position="t";	
		$(".active").removeClass("active");
		$(".fontenelle_microcopie a").addClass("active");
		if (part!='')
			$("a[rel='"+part+"']").click();
		else
			$("a[rel='micro_presentation']").click();
	});
	return false;
}
function goRight(part)
{
	el=$("#rightBlock");
	if(position=="r" || !getAnnim(el))
		return false;
	if(position!="c")
		return goCenter(goRight);
	el.fadeIn(1000,lightPresta);
	$(".search_example").fadeOut(1000);
	fontenelle(el);
	position="r";
	$(".active").removeClass("active");
	$(".archivages a").addClass("active");
	if (part!='')
		$("a[rel='"+part+"']").click();
	else
		$("a[rel='archi_presentation']").click();
	return false;
}
function goBottom(part)
{
	el=$("#bottomBlock");
	if(position=="b" || !getAnnim(el))
		return false;
	if(position!="c")
		return goCenter(goBottom);
	el.fadeIn(1000);
	$(".search_example").fadeOut(1000);
	fontenelle(el);
	position="b";
	$(".active").removeClass("active");
	$("#groupe_fontenelle").addClass("active");
	if (part!='')
		$("a[rel='"+part+"']").click();
	else
		$("a[rel='groupe_actualites']").click();
	return false;
}

function lightPresta()
{
	$("#image_arbre li a").addClass("light");
	$("#shortcuts li a").addClass("light");
}
function unlightPresta()
{
	$("#image_arbre li a").removeClass("light");
	$("#shortcuts li a").removeClass("light");
}

/*function subMenuClick()
{
	Dopener=$(this).parents(".corps").find(".texte_a_changer:visible");
	idCurrent=$("#"+$(this).attr("rel"));
	if(Dopener.attr("id")==idCurrent.attr("id"))
		return false;
	// Recherche de celui qui est visible
	Dopener.attr("active",$(this).attr("active")!=""?$(this).attr("active"):0);
	Dopener.fadeOut("normal",function()
	{
		if(typeof idCurrent == "object")
		{
			$(idCurrent).find(".page").hide()
			id=parseInt($(this).attr("active"));
			if(isNaN(id))
				id=0;
			$(idCurrent).fadeIn("slow").find(".page").eq(id).show();
		}
	});
	$(this).parents(".corps").find(".active2").removeClass("active2");
	$(this).addClass("active2").attr("active","");
	$(".sub2Menu").remove();
	return false;
}*/
function subMenuClick(urlToLoad,leA) {
	$(".slided .texte_a_changer").parent().append('<div class="chargement"><div class="loading"></div>Chargement</div>');
	$(".slided .texte_a_changer").fadeOut(1000,function(){
		$.get(urlToLoad, function(data){
			htmlToLoad=$(data).find(".slided .texte_a_changer").html();
			$(".slided .texte_a_changer").html(htmlToLoad);
			$(".slided .texte_a_changer").fadeIn(500,function() {
				$(".slided .chargement").fadeOut(500,function() {
					$(".slided .chargement").remove();
				})
			});
			$("a[@rel='box']").facebox();
		});
		// swap de la class
		$(".active2").removeClass("active2");
		$(".ssactive").removeClass("ssactive");
		if ($(leA).parent().hasClass("leading")) $(leA).addClass("active2");
		else $(leA).addClass("ssactive").parent().parent().parent().parent().find("a:eq(0)").addClass("active2");
	});
	return false;
}
function subMenuOver() {
	id=$(this).attr("rel");
	// Recupêration des calque de sous menu
	ssPage=$("#"+id+" .page");
	if(ssPage.length<2)
		return true;
	ul="<ul>";
	for(i=0;i<ssPage.length;i++) {
		actu=ssPage.eq(i);
		ul+='<li>';
		if(actu.is(":visible") && ssPage.parent().is(":visible"))
			ul+='<a class="ssactive"';
		else {
			ul+='<a';
			ul+=' href="#'+i+'">'+actu.find("h3").text()+'</a></li>';
		}
	}
	ul+="</ul>";
	ul+='<div class="bottomSub2Menu png"></div>';
	div=$("<div></div>").addClass("sub2Menu").html(ul).hide();
	$(this).find(".sub2Menu").remove();
	$(this).append(div);
	div.find("a").click(function() {
		if($(this).hasClass("ssactive"))
			return;
		data=$(this).attr("href").split("#");
		id=parseInt(data[1]);
		div=$(this).parents("a").attr("rel");
		if($("#"+div).is(":hidden"))
			$(this).parents("a").attr("active",id).click();
		else {
			$(this).parents("ul").find(".ssactive").removeClass("ssactive");
			$(this).addClass("ssactive");
			subPageClick($("#"+div),id);
		}
		$(".sub2Menu").remove();
		return false;
	});
	div.show();
	return false;
}
function subMenuOut()
{
	$(this).find(".sub2Menu").remove();
	return false;
}
function subPageClick(e,i)
{
	idSubmenu=$(e).find(".page").eq(i);
	if($("#debug").length>0)
		$("#debug").html($("#debug").html()+" i :"+i+" : "+idSubmenu.length+"<br />");
	$(e).find(".page:visible").fadeOut('normal',function()
	{
		if(idSubmenu==null)
			return false;
		
		idSubmenu.fadeIn('normal');
		idSubmenu=null;
		
		if($("#debug").length>0)
			$("#debug").html($("#debug").html()+" nb submenu : "+idSubmenu.length+"<br />");
	});
	return false;
}
function alphaPresentation()
{
	goLeft();
	$("a[rel='alpha_presentation']").click();
	$.facebox.close();
	return false;
}
function microPresentation()
{
	goTop();
	$("a[rel='micro_presentation']").click();
	$.facebox.close();
	return false;
}
function archiPresentation()
{
	goRight();
	$("a[rel='archi_presentation']").click();
	$.facebox.close();
	return false;
}
$(document).ready(function()
{
	/*
		Initialisation des cadre alentour
	*/
	$('.leading').find(".sub2Menu").hide();
	$('.leading').hover(function() {
		if ($(this).find("ul").length>0) {
			el = $(this).find("ul");
			if(!getAnnim(el)) {
				return false;
			}

			$(this).find(".sub2Menu").show();
		}
	},function() {
		$(this).find(".sub2Menu").hide();
		
	});
	$('.leading a').click(function(){
		if($(this).attr("href")=="#")
			return false;
	});
	if ($("#topBlock").hasClass("slided")==false)
		$("#topBlock").fadeOut(1);
	
	if ($("#leftBlock").hasClass("slided")==false)
		$("#leftBlock").fadeOut(1);

	if ($("#rightBlock").hasClass("slided")==false)
		$("#rightBlock").fadeOut(1);
	
	if ($("#bottomBlock").hasClass("slided")==false)
		$("#bottomBlock").fadeOut(1);
	/*
		Initialisation des évenement des déplacements
	*/
	$(".retour_arbre").click(goCenter);
	// Boutons du header
	$(".alpha_plan").click(goLeft);
	$(".fontenelle_microcopie").click(goTop);
	$(".archivages").click(goRight);
	$("#groupe_fontenelle").click(goBottom);
	// Boutons central
	$("#alpha_plan_arbre").click(goLeft);
	$("#microcopie_arbre").click(goTop);
	$("#archivages_arbre").click(goRight);
	$(".gfma").click(goBottom);
	/*
		Initialisation des des textes
	*/
	/*$(".texte_a_changer").each(function(i) {
		if (i>0)
			$(this).fadeOut(1);
		$(this).find(".page").each(function(j) {
			if (j>0)
				$(this).fadeOut(1);							   	
		});
		
	});*/
	/*$(".submenu").each(function()
	{
		enfants=$(this).find("a");
		first=enfants.click(subMenuClick).
		hover(subMenuOver,subMenuOut).
		eq(0);
		idShow=first.addClass("active2").attr("rel");
		$("#"+idShow).fadeIn(1);
		$(this).parent().find(".page").fadeOut(1).eq(0).fadeIn(1);
		$(this).parent().find(".page").find("h3").hide();
	});*/
	//$(".page").fadeOut(1);
	$(".submenu a").each(function() {
		$(this).click(function() {
			subMenuClick($(this).attr("href"),$(this));
			return false;
		});
	});
	$("#image_arbre").find("li").css("cursor","pointer").each(function(i) {
		url=$(this).children("a").click(function(){ $(this).parent().click();return false;}).attr("href");
		
		if(url!="" && url!="#")
		{
			$(this).attr("rel","facebox").attr("href",url).facebox();
		}
		$(this).hover(function()
		{
			$(this).children("a").addClass("hover");
		},
		function()
		{
			$(this).children("a").removeClass("hover");
		});
	});
	/*
		Initialisation des bouttons presentation
	*/
	$.facebox.after=function() {
		$(".alphaPresentation").click(alphaPresentation);
		$(".microPresentation").click(microPresentation);
		$(".archiPresentation").click(archiPresentation);
	}
	/*
		Initialisation de lien lire la suite
	*/
	$("a[@rel='box']").facebox();	
});
