var curBandeau = -1;
var timerIllustration = 0;
var timerIllustration_Delay = 5000;

var curSousMenu;

var curActualite;	
var curActualiteIndex;
var curActualiteHeight;
var ActualiteActionIs = false;

var curDiaporama;	
var curDiaporamaIndex;

function Page_Init(){
	
	//Bandeau Photo	
	Illustration_Afficher();
	
	//Active la lightbox
    $("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'facebook', opacity: 0.6 });
	
	//Bouton
	$(".Bouton_01").hover(function (e) {
		$(this).attr("src","images/charte/main/cmdenvoyer_in_background.jpg");
	},
	function () {
		$(this).attr("src","images/charte/main/cmdenvoyer_out_background.jpg");
	});
	
	$(document).click(function (e) {
		$(".En_Tete_Haut_Sous_Menu").hide();
	});
	
	//Sous Menu
	$("[rel^='Menu[']").hover(function (e) {
	    $(".En_Tete_Haut_Sous_Menu").hide();
	    var itemWidth = $(this).outerWidth();
		var itemHeight = $(this).outerHeight();
	    var itemPosition = $(this).offset();
		
	    //Recupère l'index du sous menu à afficher
	    var Sous_Menu_Index = $(this).attr("rel");	
	    Sous_Menu_Index = Sous_Menu_Index.substr(5, Sous_Menu_Index.length - 6);
		Sous_Menu_Index = FormatEntier(Sous_Menu_Index);
		
		//Positionne le sous menu	
		curSousMenu = $("#En_Tete_Haut_Sous_Menu_" + Sous_Menu_Index);
		curSousMenu.width(itemWidth);
		curSousMenu.css("top",itemPosition.top + itemHeight);
		curSousMenu.css("left", itemPosition.left);
		curSousMenu.slideDown();
		curSousMenu.addClass("hover");
	
	},
	function () {
		//curSousMenu.slideUp();
	});	

	
	//Raccourcie
	$("#Main_Content_Raccourcie a").hover(function (e) {
		if($(this).find("span").css("bottom") == "0px"){
			$(this).find("span").animate({ 
				bottom: "55px"
      		}, 400 );
		}
	},
	function () {
		$(this).find("span").animate({ 
			bottom: "0px"
      	}, 400 );
	});	
	
	
	//Actualités
	
	$("#Actualite_Precedente img").hover(function (e) {
		$(this).attr('src','images/charte/main/actualite_precedente_hover.jpg');
	},
	function () {
		$(this).attr('src','images/charte/main/actualite_precedente.jpg');
	});	
	
	$("#Actualite_Suivante img").hover(function (e) {
		$(this).attr('src','images/charte/main/actualite_suivante_hover.jpg');
	},
	function () {
		$(this).attr('src','images/charte/main/actualite_suivante.jpg');
	});	
	
	curActualite = $(".Actualite_Item").first();
	if($(".Actualite_Item").size() >= 1){
		curActualiteHeight = $(".Actualite_Item").first().outerHeight();
		$("#Main_Content_Actualite_Content").height(curActualiteHeight);
		curActualiteIndex = 0;
	}
	
	curActualite = $(".Actualite_Item").first();
	
	$("#Actualite_Suivante img").hide();
	if($(".Actualite_Item").size() <= 1){
		$("#Actualite_Precedente img").hide();
	}
	
	//Evenement sur les actualités
	$("#Actualite_Precedente img").click(function (e) {
		
		if(ActualiteActionIs){return false;}
		ActualiteActionIs = true;
		//Si ce n'est pas l'actualité la plus ancienne
		if(curActualiteIndex < $(".Actualite_Item").size() - 1){	
			curActualiteHeight = curActualite.outerHeight();
			$("#Actualite_Wrapper").animate({ 
				top : "-=" + curActualiteHeight + "px"
      		}, 400 );
			
			curActualiteHeight = curActualite.next(".Actualite_Item").outerHeight();
			
			$("#Main_Content_Actualite_Content").animate({ 
				height : curActualiteHeight
      		}, 400 );
			
			curActualiteIndex = FormatEntier(curActualite.next(".Actualite_Item").index());
			curActualite = curActualite.next(".Actualite_Item");
			
			if(curActualiteIndex == $(".Actualite_Item").size() - 1){
				$("#Actualite_Precedente img").hide();
				$("#Actualite_Suivante img").show();
			}
			else{
				$("#Actualite_Precedente img").show();
				$("#Actualite_Suivante img").show();
			}
		}
		ActualiteActionIs = false;
	});	
	
	$("#Actualite_Suivante img").click(function (e) {
		if(ActualiteActionIs){return false;}
		ActualiteActionIs = true;
		if(curActualiteIndex > 0){
			
			curActualiteHeight = curActualite.prev(".Actualite_Item").outerHeight();
			
			$("#Actualite_Wrapper").animate({ 
				top : "+=" + curActualiteHeight + "px"
      		}, 400 );
			
			$("#Main_Content_Actualite_Content").animate({ 
				height : curActualiteHeight
      		}, 400 );
			
			curActualiteIndex = FormatEntier(curActualite.prev(".Actualite_Item").index());
			curActualite = curActualite.prev(".Actualite_Item");
			
			if(curActualiteIndex == 0){
				$("#Actualite_Suivante img").hide();
				$("#Actualite_Precedente img").show();
			}
			else{
				$("#Actualite_Precedente img").show();
				$("#Actualite_Suivante img").show();
			}
		}
		ActualiteActionIs = false;
	});	

	//Diaporama
	curDiaporama = $(".Diaporama_Item").first();
	curDiaporamaIndex = FormatEntier($(".Diaporama_Item").first().index());
	
	afficheBoutons($("#Diaporama_Content_Bas a"), $("#Diaporama_Content_Haut a"), curDiaporamaIndex, $(".Diaporama_Item").size() );
	
	$("#Diaporama_Content_Haut a").click(function (e) {
		$("#Diaporama_Wrapper").animate({ 
				top : "+=203px"
      	}, 400 );
		
		curDiaporamaIndex = parseInt(curDiaporamaIndex - 1);
		curDiaporama = $(".Diaporama_Item").get(curDiaporamaIndex);		
		afficheBoutons($("#Diaporama_Content_Bas a"), $("#Diaporama_Content_Haut a"), curDiaporamaIndex, $(".Diaporama_Item").size() );
		return false;
	});
	
	$("#Diaporama_Content_Bas a").click(function (e) {
		$("#Diaporama_Wrapper").animate({ 
				top : "-=203px"
      	}, 400 );
		
		curDiaporamaIndex = parseInt(curDiaporamaIndex + 1);
		curDiaporama = $(".Diaporama_Item").get(curDiaporamaIndex);		
		afficheBoutons($("#Diaporama_Content_Bas a"), $("#Diaporama_Content_Haut a"), curDiaporamaIndex, $(".Diaporama_Item").size() );
		return false;
	});	
	
	/*Type de biens*/
	$(".Type_Biens_Item img").css("opacity", "0.6");
	$(".Type_Biens_Item img").hover(function (e) {
		$(this).animate({ 
				opacity : "1"
      	}, 400 );
	},
	function () {
		$(this).animate({ 
				opacity : "0.6"
      	}, 400 );
	});				
}

function getMaxHeight(item1, item2){
	var maxHeight = 0;
	if(item1.outerHeight() > item2.outerHeight()){
		maxHeight = item1.outerHeight();
	}
	else{
		maxHeight = item2.outerHeight();
	}
	return maxHeight;
}

function Illustration_Afficher()
{	
	window.clearInterval(timerIllustration);
	
	//Affiche l'illustration
	curBandeau++;
	if (curBandeau >= tIllustration.length) { curBandeau = 0; }

	var img = new Image();
	$(img).load(function () {    
        $(this).hide();            
		  
		$('#En_Tete_Bas_Bandeau_Photo').fadeOut(1000, function()
			{  
				$('#En_Tete_Bas_Bandeau_Photo').empty();
				$('#En_Tete_Bas_Bandeau_Photo').append(img);  
				$(img).show();
				$('#En_Tete_Bas_Bandeau_Photo').fadeIn(2000);
				
				
				if (tIllustration.length > 1) { timerIllustration = window.setInterval('Illustration_Afficher()', timerIllustration_Delay) };
			}
		);			
	}).error(function () {
	}).attr('src', 'images/bandeau/' + tIllustration[curBandeau]);
}


function FormatEntier(intValeur) {
	/* Initialisations */
	var strSepMilliers = String.fromCharCode(160);
	var strSepDecimal = ",";
	var strResultat = "";
	var strPartieEntiere = String(Math.floor(intValeur));
 
	//Milliers
	var intNbBlocs = parseInt(strPartieEntiere.length / 3);
	var intLongPremBloc = strPartieEntiere.length % 3;
	strResultat = strPartieEntiere.substr(0,intLongPremBloc);
	
	for (var intIdxBloc = 0; intIdxBloc < intNbBlocs; intIdxBloc++)
	{		
		strResultat += strSepMilliers + strPartieEntiere.substr(intIdxBloc*3+intLongPremBloc,3);
	}
	
	//Supprime le premier séparateur si besoin
	if (strResultat.substr(0,1) == strSepMilliers) { strResultat = strResultat.substr(1, strResultat.length - 1); }
	
		
	return strResultat;
}

function Objet_Absolute_X(objItem)
{
	//Renvoi la valeur absolue en X d'une element
	var objParent = null
	var intX = 0
	var intY = 0
	do
	{
		intX += objItem.offsetLeft;
		objParent = objItem.offsetParent.tagName;
		objItem = objItem.offsetParent;
	}
	while((objParent != 'BODY') && (objParent != 'HTML'))
	
	return intX;
}

function afficheBoutons(next, prev, index, size){
	if(size == 1){
		next.hide();
		prev.hide();
	}
	else if(size > 1){
		if(index == 0){
			next.css("display", "block");
			next.show();
			prev.hide();
		}
		else if(index == size - 1){
			prev.css("display", "block");
			prev.show();
			next.hide();
		}
		else{
			prev.css("display", "block");
			prev.show();
			next.css("display", "block");
			next.show();
		}
	}
}



