var gSection = "";//typeof(section) != 'undefined' ? section : "accueil";	//Pour connaitre la section en cours
var gSousSection = "";//typeof(sousSection) != 'undefined' ? sousSection : "";	//Pour connaitre la sous-section en cours
var gId = 0;

var gAncSection = "accueil";
var gAncSousSection = "";



//Init
jQuery().ready(function(){
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "index.php");
	
	// set onlick event for buttons
	$("a[rel='history']").click(function(){
		// 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});

	//Rollover Menu
	$("#menu > a").hover(
		function(){
			if( $(this).children(0).attr("id").substr(4) != gSection)
				$(this).children(0).attr("src","images/"+$(this).children(0).attr("id")+"_over_"+gLangue+".jpg");
		},
		function(){
			if( $(this).children(0).attr("id").substr(4) != gSection)
				$(this).children(0).attr("src","images/"+$(this).children(0).attr("id")+"_"+gLangue+".jpg");
		}
	);
	
	//Rollover Sous-Menu
	$("#menu > div > a").hover(
		function(){
			$(this).children(0).attr("src","images/"+$(this).children(0).attr("id")+"_over_"+gLangue+".jpg");
		},
		function(){
			if( $(this).children(0).attr("id").substr(4) != gSousSection)
				$(this).children(0).attr("src","images/"+$(this).children(0).attr("id")+"_"+gLangue+".jpg");
		}
	);
	
	//Rollover Langue
	$("#header > a").hover(
		function(){
			$(this).children(0).attr("src","images/"+$(this).children(0).attr("id")+"_over.jpg");
		},
		function(){
			if( $(this).children(0).attr("id").substr(4) != gLangue){
				$(this).children(0).attr("src","images/"+$(this).children(0).attr("id")+".jpg");
			}
		}
	);
	
	//Cacher les sous-menus
	$("#menu > div").hide();
	$('#sous_'+gSection).show();
	
	if(self.document.location.hash == ""){
		afficher('accueil'); //affiche la page par défaut
	}
});
/*********************************************************************
*                        GESTION DES SECTIONS                        *
*********************************************************************/
function afficher(section,sousSection){
	sousSection = typeof(sousSection) != 'undefined' ? sousSection : "";

	if(section == "joindre" && sousSection == "")
		sousSection = "coordonnees";	//sous-section par défaut de joindre

	//$('contenu').fadeOut('slow',function(){
		adjustIFrameSize(620);
		document.getElementById('contenu').src = 'contenu.php?section='+section+'&sousSection='+sousSection;
		//$('contenu').fadeIn('slow');
	//});

	gSection = section;
	gSousSection = sousSection;

	//Menus
	changerActif();
	gestionSousMenu();
	
	$("#a_"+gLangue).children(0).attr("src","images/btn_"+gLangue+"_over.jpg");
	$("#a_FR").attr('href',"?langue=FR#"+section)
	$("#a_EN").attr('href',"?langue=EN#"+section)
}

/*********************************************************************
*                          GESTION DES MENUS                         *
*********************************************************************/
function changerActif(){
	document.getElementById('btn_'+gAncSection).src = 'images/btn_'+gAncSection+'_'+gLangue+'.jpg';
	document.getElementById('btn_'+gSection).src = 'images/btn_'+gSection+'_active_'+gLangue+'.jpg';
	gAncSection = gSection;
}

function gestionSousMenu(){
	$("#menu > div").hide();
	$('#sous_'+gSection).show();
	
	if(gAncSousSection != ""){
		document.getElementById('btn_'+gAncSousSection).src = 'images/btn_'+gAncSousSection+'_'+gLangue+'.jpg';
	}
	if(gSousSection != ""){
		document.getElementById('btn_'+gSousSection).src = 'images/btn_'+gSousSection+'_over_'+gLangue+'.jpg';
		gAncSousSection = gSousSection;
	}
}



// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// alert("pageload: " + hash);
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
			$('#load').load('untitled.html');
			if($.browser.msie) {
				// jquery's $.load() function does't work when hash include special characters like aao.
				hash = encodeURIComponent(hash);
			}
			if(hash.indexOf('_',0) > 0){
				var pos = hash.indexOf('_',0);
				var sSect = hash.substr(0,pos);
				var sSSect = hash.substr(pos+1);
				if(sSSect.indexOf('_',0) > 0){
					pos = sSSect.indexOf('_',0)
					var iId = sSSect.substr(pos+1);
					sSSect = sSSect.substr(0,pos);
					afficher(sSect,sSSect);
				}
				else{
					afficher(sSect,sSSect);
				}
			}else{
				afficher(hash);
			} 
			//$("#load").load(hash + ".html");
		} else {
			// start page
			afficher('accueil');
			//$("#load").empty();
		}
	}
