// JavaScript Document

//==============================================================//
//========== Fonction pour le rollover des sous menu ==========//
//============================================================//
var timer
var menuOuvert = ""
var menuOuvertNiv2 = ""

//========================================//
var section_active_niveau1 = "";
var section_active_niveau2 = "";

var page = ""

var urlEntete = window.location
var p = String(urlEntete).split("?")
var strPageCourante


if(p.length > 1){
	var p2 = p[1].split("&")
	
	
	var param = new Array()
	for(ind in p2)
	{
		var p3= p2[ind].split("=")
		param[p3[0]] = p3[1]
		
		if(p3[0] == "page")
			page = p3[1]
	}	
	strPageCourante = page.split("_")
	section_active_niveau1 = strPageCourante[0]
	section_active_niveau2 = section_active_niveau1 + "_" + strPageCourante[1]
}else{
	section_active_niveau1 = "a0"
	section_active_niveau2 = "a0"
}





//========================================//

function td_out(pQuel)
{
	window.clearTimeout(timer)
	//pQuel.style.backgroundColor = '#18365B';

}
function td_over(pQuel)
{
	window.clearTimeout(timer)
	//pQuel.style.backgroundColor = '#c07a07';

}

//==========================================================//
//========== Fonction pour ouvrir les sous-menus ==========//
//========================================================//
function openSous(pQuel)
{		
	window.clearTimeout(timer)
	
	if (menuOuvertNiv2)
		document.getElementById(menuOuvertNiv2).style.visibility = 'hidden'

	if (menuOuvert)
		document.getElementById(menuOuvert).style.visibility = 'hidden'
	
	if (pQuel)
	{
		document.getElementById(pQuel).style.visibility = 'visible'
		menuOuvert = pQuel
		menuOuvertNiv2 = ""
	}
}

function openSousNiv2(pQuel, menu)
{
	
	//td_over(menu);
	
	window.clearTimeout(timer)

	if (menuOuvertNiv2)
	{
		document.getElementById(menuOuvertNiv2).style.visibility = 'hidden'
	}
	
	if (pQuel)
	{
		document.getElementById(pQuel).style.visibility = 'visible'
		menuOuvertNiv2 = pQuel
	}
}
function fermeSous()
{	
	fermeSousNiv2();
	
	if (menuOuvert)
		document.getElementById(menuOuvert).style.visibility = 'hidden'
	
	window.clearTimeout(timer)
}
function fermeSousNiv2()
{
	//td_out(menu);
	
	if (menuOuvertNiv2)
		document.getElementById(menuOuvertNiv2).style.visibility = 'hidden'
	
	window.clearTimeout(timer)
}
function timerSousMenu(statut)
{
	switch(statut)
	{
	case 1:
	case "1":
		timer = window.setTimeout('fermeSous();',200)
		break;
	case 0:
	case "0":
		window.clearTimeout(timer)
		break;
	}
}


//window.onload=func1; 

//==========================================//
//========== Creer les sous menu ==========//
//========================================//

function verifPos()
{
	
	//alert("allo")
	for(IndSous in menus_deroulants)
	{
		for(ind in menus_deroulants[IndSous])
		{
			var itemMenu = new ItemMenuDeroulant();
			itemMenu.sousItems = menus_deroulants[IndSous][ind].sousItems;
			var arySousItemsMenu = null;
			arySousItemsMenu = itemMenu.sousItems;

			if (arySousItemsMenu)
			{
				//createSousSousMenu(arySousItemsMenu,ind,IndSous);
				var id = ind;
				var pIndSous = IndSous
				//----------//
				var num = Number(id);
				var posTop = 0;
				var bt_name = 'btn_' + pIndSous + '_' + num;
				var element = document.getElementById(bt_name);
				
				if(element)
				{
					posTop = element.offsetTop;
					
					var mcMenu = "sous_" + IndSous + "_niv2_" + id;
					if(mcMenu)
					{
						document.getElementById(mcMenu).style.marginTop = posTop;
					}
				}
			}
		}
	}
}
window.onload=verifPos;
function createSous()
{
	for(IndSous in menus_deroulants)
	{	
 		if(menus_deroulants[IndSous].length>0)
		{
			document.write("<div id='sous_" + IndSous + "' class='g_sous sous_menu_" + IndSous + " color_" + IndSous + "' >")	
		}
		else
		{
			document.write("<div style='visibility:hidden;' id='sous_" + IndSous + "' class='g_sous sous_menu_" + IndSous + " color_" + IndSous + "' >")	
		}
		document.write("<div class='sous_float_" + IndSous + "' onmouseover='timerSousMenu(0)' onmouseout='timerSousMenu(1)'>")	
			document.write("<ul>");
				for(ind in menus_deroulants[IndSous])
				{
					var itemMenu = new ItemMenuDeroulant();
					itemMenu.texte = menus_deroulants[IndSous][ind].texte;
					itemMenu.lien = menus_deroulants[IndSous][ind].lien;
					itemMenu.estPageCourante = menus_deroulants[IndSous][ind].estPageCourante;
					itemMenu.sousItems = menus_deroulants[IndSous][ind].sousItems;
					
					//////////////////////////
					// Niveau2 (sous-items) //
					//////////////////////////
					var arySousItemsMenu = null;
					arySousItemsMenu = itemMenu.sousItems;

					//-------------//
					var myPage = "";
					var pagNiv1
					var url = itemMenu.lien;
					
					//////MODIF A FAIRE ICI !!!!!!!!!!!!
					
					var nameBtn = "";
					var mp = String(url).split("?");
					var mp2 = mp[1].split("&");
					
					if(mp.length > 1 && mp2.length > 1)
					{
						//alert('a');
						var mparam = new Array();
						for(ind2 in mp2)
						{
							var mp3= mp2[ind2].split("=");
							if(mp3.length > 1)
							{
								//alert('b');
								mparam[mp3[0]] = mp3[1];
								
								if(mp3[0] == "page")
								{
									//alert('c');
									myPage = mp3[1];
									pagNiv1 = myPage.split("_")
									if(pagNiv1.length > 1)
									{
										//alert('d');
										nameBtn = "btn_" + pagNiv1[0] + "_" + ind;
									}
								}
							}
						}
					}
					
					//-------------//
					//alert("myPage : " + myPage)
					if (arySousItemsMenu)
					{								
						if(itemMenu.estPageCourante || section_active_niveau2==myPage)
						{
							document.write("<li id='" + nameBtn + "'><a onmouseover='openSousNiv2(\"sous_" + IndSous + "_niv2_" + ind + "\", this)'  class='section_courante_niveau_2' href='" + itemMenu.lien + "'><span class='txt'>" + itemMenu.texte + "</span><span class='fleche'></span></a></li>");
						}
						else
						{
							document.write("<li id='" + nameBtn + "'><a  onmouseover='openSousNiv2(\"sous_" + IndSous + "_niv2_" + ind + "\", this)'  href='" + itemMenu.lien + "'><span class='txt'>" + itemMenu.texte + "</span><span class='fleche'></span></a></li>");
						}
					}
					else
					{
						if(itemMenu.estPageCourante)
						{
							document.write("<li id='" + nameBtn + "'><a onmouseover='openSousNiv2(null, this)' class='section_courante_niveau_2' href='" + itemMenu.lien + "'><span class='txt'>" + itemMenu.texte + "</span></a></li>");	
						}
						else
						{
							document.write("<li id='" + nameBtn + "'><a onmouseover='openSousNiv2(null, this)' href='" + itemMenu.lien + "'><span class='txt'>" + itemMenu.texte + "</span></a></li>");
						}
					}
					//////////////////
					// Fin niveau 2 //
					//////////////////
				}
				document.write("</ul>");
			document.write("</div>")
		document.write("</div>")
		
		//--------//
		for(ind in menus_deroulants[IndSous])
		{
			var itemMenu = new ItemMenuDeroulant();
			itemMenu.sousItems = menus_deroulants[IndSous][ind].sousItems;

			var arySousItemsMenu = null;
			arySousItemsMenu = itemMenu.sousItems;

			if (arySousItemsMenu)
			{
				createSousSousMenu(arySousItemsMenu,ind,IndSous);
			}
		}
		//----------//
	}
}
	
//------//
function createSousSousMenu(pArySousItemsMenu,pInd,pIndSous)
{
	var arySousItemsMenu = pArySousItemsMenu;
	var id = pInd;
	
	var numParent = pIndSous.split("mrc");
		
	//----- Position horizontale du 3eme niveau -----//
	var tbPosLeft = new Array();
	tbPosLeft["mairie0"] = 188;
	tbPosLeft["residants0"] = 293;
	tbPosLeft["citoyens0"] = 444;
	tbPosLeft["visiteurs0"] = 726;
	
	
	
	var posLeft = tbPosLeft[pIndSous]
	//--------//
	//----- Position verticale du 3eme niveau -----//
	var num = Number(id);
	var posTop = 0;
	var bt_name = 'btn_' + pIndSous + '_' + num;
	var element = document.getElementById(bt_name);
	
	if(element)
	{
		posTop = element.offsetTop -1;
	}
	//-------/	
	/*if(pIndSous=="mrc1")
	{
		var bt_name2 = 'btn_mrc1_3';
		var element2 = document.getElementById(bt_name2);
		alert(element2.offsetTop)
	}*/
	
		
	
	
	document.write("<div id='sous_" + pIndSous + "_niv2_" + id + "' class='sous_niv3  color_" + IndSous + "' style='margin-top:" + posTop + "px; margin-left:" + posLeft + "px;'>")	
		document.write("<div onmouseover='timerSousMenu(0)' onmouseout='timerSousMenu(1)'>")	
			document.write("<ul>");
					for(ind in arySousItemsMenu)
					{							
						var itemMenu = new ItemMenuDeroulant();
						itemMenu.texte = arySousItemsMenu[ind].texte;
						itemMenu.lien = arySousItemsMenu[ind].lien;
						itemMenu.estPageCourante = arySousItemsMenu[ind].estPageCourante;
						
						// Niveau3 (sous-items)
						if(itemMenu.estPageCourante)
						{
							document.write("<li><a class='section_courante_niveau_3' href='" + itemMenu.lien + "'><span class='txt'>" + itemMenu.texte + "</span></a></li>");	
						}
						else
						{
							document.write("<li><a href='" + itemMenu.lien + "'><span class='txt'>" + itemMenu.texte + "</span></a></li>");
						}
							
					}
					document.write("</ul>");
				
		document.write("</div>")
		document.write("</div>")
}

