/*

	Auteurs : 

		Dominic Gauthier 	: 6924

		Martin Rancourt 	: 7526

		

	Dernière modification :

		Martin Rancourt 	: 2005-07-07	Optimiser le code

		Martin Rancourt		: 2005-07-19	Ajouter le system d'exeption pour le hightlight

*/


var domaine = "http://"+location.host;

var pathURL = location.pathname.toString().toLowerCase();

if (pathURL.indexOf("regions") != -1) pathURL = "/nouvelles" + pathURL.replace("/regions", "/regional");

var arrayPathURL = pathURL.split("/");

function construireMenu(objUl) {

	//loop dans les childs du UL

	for(var i=0;i<objUl.childNodes.length;i++) {

		//valide si le node est un LI

		if (objUl.childNodes[i].tagName == "LI") {

			//loop dans les childs du LI

			for(j=0;j<objUl.childNodes[i].childNodes.length;j++) {

				//reset la valeur de la varible qui indique si on doit sélectionné ou ouvrir l'item

				needProcess = false;

				//valide si le node est un A

				if(objUl.childNodes[i].childNodes[j].tagName == "A") {

					//Regarde si il y a un class sur le A 

					//on gère par exeption l'ouverture et la selection de l'item

					if (objUl.childNodes[i].childNodes[j].getAttribute('class') != null) {

						//Si le contenu du class se trouve dans l'url de la page

						if ( (document.location.toString().toLowerCase()).indexOf(objUl.childNodes[i].childNodes[j].getAttribute('class').toLowerCase()) != -1 ) {

							//On doit faire l'ouverture ou la selection de l'item

							needProcess = true;

						}

						

					// on gère automatiquement l'ouverture et la selection de l'item

					} else {

						//garde le path du lien trouvé

						var path = objUl.childNodes[i].childNodes[j].getAttribute('href').toString().toLowerCase();

						//enlève le domaine si il le contien

						if(path.indexOf(domaine) != -1){

							path = path.slice(domaine.length);

						}

						//Créé un array du path

						var arrayPath = path.split("/");

						//Garde le dernier folder du path

						if(arrayPath[(arrayPath.length-1)].indexOf(".") != -1 || arrayPath[(arrayPath.length-1)] == "") {

							var position = (arrayPath.length-2);

						} else {

							var position = (arrayPath.length-1);

						}

						//Si dernier folder de la page = le dernier folder du lien

						if(arrayPathURL[position] == arrayPath[position]) {

							needProcess = true;

						}

					}

					//Si on doit ouvrir ou sélectionner l'item

					if (needProcess) {

						//loop dans les childs du LI

						for(k=0;k<objUl.childNodes[i].childNodes.length;k++) {

							//valide si c'est un élément A

							if (objUl.childNodes[i].childNodes[k].tagName == "A") {

								// Allume le menu -> mets un class "On" au A

								objUl.childNodes[i].childNodes[k].setAttribute("className", "On");

								objUl.childNodes[i].childNodes[k].setAttribute("class", "On");

							}

							//valide si il y a un UL

							if(objUl.childNodes[i].childNodes[k].tagName == "UL") {

								//Ouvre le sous menu

								objUl.childNodes[i].childNodes[k].style.display = "block";

								//Recall la fonction pour appliquer le processus au child

								construireMenu(objUl.childNodes[i].childNodes[k]);			

								break;					

							}

						}

					}

				}

			}

		}

	}

}