/*	Variables Declaration  */
var ajax = new Ajax();
var xmlData = null;

var navId = new Array();
var lang = jsReturnLanguage();

/*	Functions Declaration  */
function getNavXML() {
	ajax.doGet("/code/fonctions.asp?action=GenerateXMLMenu&lang=" + lang, manageXMLResult, "xml");
}

function manageXMLResult(resp) {
	xmlData = resp;
}

getNavXML();


/*--------------------------------------------*/


function getSubNav(idTag, lng) {

	var newNavId = idTag.split("-").slice(1);
	
	if(newNavId.length > navId.length) {
		openNav(newNavId);
		navId = newNavId;
	}else {
		closeNav(navId, newNavId.length);
		
		if(!compareArrRoot(newNavId, navId)) {
			openNav(newNavId);
			navId = newNavId;
		}else {
			//	Remove the last element if we only close the navigation item
			navId = newNavId;
			navId.pop();
		}
	}
}


/*
function getSubNav(idTag)
{
	var newNavId = idTag.split("-").slice(1);
	
	//	Now we have to check if we have to open an new level item
	//	Simply check if the newNavId lenght is higher then the navId, this way it means that we open a new sub nav
	//	or if the root node if different it means that we open a new level
	if(!compareArrRoot(newNavId, navId) || newNavId.length > navId.length) {
		openNav(newNavId);
	}
	
	//	If the length of the new array is equal or smaller, it means that we must close
	//	the current open nav. We have to close the nav at the same sub level that the new clicked item.
	//	To do so, we used the length value of the newNavId
	if(newNavId.length <= navId.length) {
		closeNav(navId, newNavId.length);
		
		//	Set the navId equal to the newNavId to get the same level
		//	Remove the last element because we are closing this level
		navId = newNavId;
		navId.pop();
	}else {
		navId = newNavId;
	}
}
*/


/*
 *	Even if the 2 array are not the same size, it compare only up to the last element
 *	of the smaller array. If the 2 array have the same item inside, it means that
 *	they are in the same tree of navigation
 */
function compareArrRoot(arrA, arrB)
{
	var max = Math.min(arrA.length, arrB.length);
	for(var i = 0; i < max; i++) {
		if(arrA[i] != arrB[i]) { return false; }
	}
	
	return true;
}



/*
 *	Retreive the sub nav element in the page and remove it and its child from the page
 */
function closeNav(temNavId, maxId)
{
	var newTagName = "submenu";
	for(var i = 0; i < maxId; i++) {
		newTagName += "-" + temNavId[i];
	}
	
	//	Remove item content if needed
	var pollDiv = document.getElementById(newTagName);
	while(pollDiv.firstChild) {
		pollDiv.removeChild(pollDiv.firstChild);
	}
}



/*
 *	Retreive the new sub nav element in the page and write the code that will display it
 */
function openNav(temNavId)
{
	var newTagName = "submenu";
	for(var i = 0; i < temNavId.length; i++) {
		newTagName += "-" + temNavId[i];
	}
	
	printSub(newTagName, temNavId);
}



/*
 *	The way to create all the navigation
 */
function printSub(idTag, idArray)
{
	//	Remove item content if needed
	var pollDiv = document.getElementById(idTag);
	
	//	Retreive the current node based on the navigation item ID
	var currNode = xmlData.documentElement;
	for(var i = 0; i < idArray.length; i++) {
		currNode = currNode.childNodes[parseInt(idArray[i], 10) + 1];
	}
	
	//	Pass throw all nodes
	var numOfItem = currNode.childNodes.length;
	for(var i = 1; i < numOfItem; i++) {
		//	Set the link Text
		var subText = document.createTextNode(currNode.childNodes[i].firstChild.nodeValue);
		
		//	Set the link
		var hrefAtt = currNode.childNodes[i].getAttribute("href");
		if(!hrefAtt) {
			//	If the link attributes is undefined or null we have to check if there is sub menu or we just set the # as a link
			if(currNode.childNodes[i].childNodes.length > 1) { hrefAtt = "javascript:getSubNav('"+ idTag + "-" + (i-1) +"');"; }
			else { hrefAtt = "#"; }
		}
		
		//	Set the link target
		var targetAtt = currNode.childNodes[i].getAttribute("target");
		if(!targetAtt) { targetAtt = "_self"; }
		
		var subLink = document.createElement("a");
		subLink.setAttribute("href", hrefAtt);
		subLink.setAttribute("target", targetAtt);
		subLink.appendChild(subText);
		
		
		// Create the div that will hold other sub items if there is
		var subItemDiv = document.createElement("div");
		subItemDiv.setAttribute("id", idTag + "-" + (i-1));


		// Create the div holder
		var itemDiv = document.createElement("div");
		itemDiv.className = currNode.childNodes[i].getAttribute("classId");
		itemDiv.appendChild(subLink);
		itemDiv.appendChild(subItemDiv);
		
		
		//	Put all inside the main div element of the nav
		pollDiv.appendChild(itemDiv);
	}
}

function jsReturnLanguage() {

	var url = new String(document.location);
	
	if (url.indexOf('/en/') != -1)
		return ('en');
	else
		return ('fr');
}

