var sitemap_filename = "/sitemap.xml";
var xns_sitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
var xns_menu = "http://www.miningexcellence.ca/schemas/sitemap_menu/0.1";

var menuTimer = null;
var menuDefault = "";
var submenu_data = Array();

function XPathEvalNS(aExpr, aNode)
{
	if(dom.evaluate) {
		var xpe = new XPathEvaluator();
		var nsr = xpe.createNSResolver(
			aNode.ownerDocument == null ?
    			aNode.documentElement :
			aNode.ownerDocument.documentElement);
// ... add to nsr
		return dom.evaluate(aExpr, aNode, nsr, 0/*type*/, null);
	} else if (dom.selectNodes) {
		var ns = new XmlNamespaceManager()
		return dom.selectNodes(aExpr, ns);
	}
	return null;
}

function loadXMLDoc(dname)
{
	var xmlDoc;
	if(window.XMLHttpRequest) {
		// W3C compliant
		xmlDoc = new window.XMLHttpRequest();
		xmlDoc.open("GET",dname,false);
		xmlDoc.send("");
		return xmlDoc.responseXML;
	} else if(ActiveXObject("Microsoft.XMLDOM")) {
		// IE 5 and IE 6, non-compliant
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load(dname);
		return xmlDoc;
	}
	alert("Browser does not support XMLHttpRequest");
	return null;
}

function uri_local(x) { // returns a full link (eg. http://www.miningexcellence.ca/about/mandate.html)
	s = 'http://www.miningexcellence.ca/'; 
	var i = x.indexOf(s); 
	return x.substring(i+s.length-1);

}

function submenu_string(x)
{
	var style, url, submenu = "";
////////////////////////////////////
	if(x.getAttributeNS) {
		style = x.getAttributeNS(xns_menu, "style");
	} else {
		style = x.getAttribute('m:style');
 	}
	if(style) submenu += '<div style="' + style + '">'

	if(x.getElementsByTagNameNS) {
		url = x.getElementsByTagNameNS(xns_sitemap, 'url');
	} else {
		url = x.getElementsByTagName('url');
	}
	for(j=0; j<url.length; j++)
	{
		var name, href;
		if(url[j].getAttributeNS) {
			name = url[j].getAttributeNS(xns_menu, "name");
			href = url[j].getElementsByTagNameNS(xns_sitemap, "loc")[0];
		} else {
			name = url[j].getAttribute("m:name");
			href = url[j].getElementsByTagName('loc')[0];
		}
		if(name) {
			if(j > 0) submenu += ' | ';
			href = uri_local(href.firstChild.nodeValue);
			submenu += '<a href="' + href + '">';
			submenu += name + '</a>';
		}
	}
	if(style) {
		submenu += '</div>'
	}
	return submenu;
}

function menu_html(xmlent_menu)
{
	var name, href, img = null, style, html = "";
	if(xmlent_menu.getAttributeNS) {
		var nsResolver
		// For systems that support XML Namespaces
		name = xmlent_menu.getAttributeNS(xns_menu, "name");
		img = xmlent_menu.getAttributeNS(xns_menu, "img");
		href = xmlent_menu.getAttributeNS(xns_menu, "href");
	} else {
		// For 'dumb' systems that don't do namespaces
		name = xmlent_menu.getAttribute("m:name");
		img = xmlent_menu.getAttribute("m:img");
		href = xmlent_menu.getAttribute('m:href');
	}
	if(href) { // Avoid null-pointers, hence no links.
		href = uri_local(href);
		html = '<a href="' + href +'">';
	}
	if(img != "" && img != null) { // Both checks necessary for all browsers to be compatible
		name = '<img src="' + img + '" width="16" height="16" alt="image">';
	}
	html += name;
	if(href) html += '</a>';
	return html;
}

function setMenuDefault(i)
{
  menuDefault = i;
}

function getMainPath(url) { // returns the main path (eg. about, team, news, careers, contacts, events...)
	var newUrl = "";
	urlPath = url.split( '/' );
	newUrl += "/";
	newUrl += urlPath[3]; // only pulls the first string of the path
	newUrl = newUrl.toLowerCase();
	newUrl += "/";
	return newUrl;
}

function getName(xmlent_menu) {
	var name;
	if(xmlent_menu.getAttributeNS) {
		var nsResolver
		// For systems that support XML Namespaces
		name = xmlent_menu.getAttributeNS(xns_menu, "name");
	} else {
		// For 'dumb' systems that don't do namespaces
		name = xmlent_menu.getAttribute("m:name");
	}
	return name;
}

function skipMatch(xmlent_menu) {
	var match;
	if(xmlent_menu.getAttributeNS) {
		var nsResolver
		// For systems that support XML Namespaces
		match = xmlent_menu.getAttributeNS(xns_menu, "match");
	} else {
		// For 'dumb' systems that don't do namespaces
		match = xmlent_menu.getAttribute("m:match");
	}
	return match.toLowerCase() == "false";
}

function getHref(xmlent_menu) {
	var href;
	if(xmlent_menu.getAttributeNS) {
		var nsResolver
		// For systems that support XML Namespaces
		href = xmlent_menu.getAttributeNS(xns_menu, "href");
	} else {
		// For 'dumb' systems that don't do namespaces
		href = xmlent_menu.getAttribute("m:href");
	}
	href = href.toLowerCase();
	return href;
}

function getMenu() {
	var xmlhttp = loadXMLDoc(sitemap_filename);
	if(null == xmlhttp)
		alert("Your browser does not support XMLHTTP!");
		
	var x;
	if(xmlhttp.getElementsByTagNameNS) {
		x = xmlhttp.getElementsByTagNameNS(xns_menu, "menu");
	} else {
		x = xmlhttp.getElementsByTagName("m:menu");
	}
	return x;
}

function determineSelection(x) {
	var menuName = "";
	var url = document.location.href; // current url
	url = getMainPath(url); 
	for(i=0; i<x.length; i++) { // Cycle through main menu elements' href to find current url
		
		// Skip where attribute m:match="false"
		if( skipMatch(x[i]) ) continue;
		
		href = getHref(x[i]);
		href = getMainPath(href);
		if(href == url) {
			menuName = getName(x[i]); // main element matched
		}
	}
	setMenuDefault(menuName);
	return menuName;
}

function initMenu() {
	var menu = document.getElementById("menu");
	var row = menu.rows[0];
	
	// Populate Menu
	var x = getMenu();
	while(row.cells.length > 0) row.deleteCell(0); // Clear cells ...
	for(i=0; i<x.length; i++) { // Add table-data.
		var cell = row.insertCell(-1);
		cell.onmouseover = doEvent;
		cell.innerHTML = menu_html(x[i]);
		submenu_data.push(submenu_string(x[i]));
	}

	// Determine Current Menu Selection
	currentMenuName = determineSelection(x);	
	setMenu(currentMenuName);
}

function setMenu(currentMenuName) { // sets the current element based on the element name from url
	var name,x;
	x = getMenu();
	var found = 0;
	var row = document.getElementById("menu").rows[0];
	
	for(index=0; index<x.length; index++) { // Cycle through main menu elements
		name = getName(x[index]); // Find the index of the one that matches the current element
		if (name == currentMenuName){
			found = 1;
			for(i=0; i< row.cells.length; i++) {
				var cell = row.cells[i];
				cell.id = (i == index) ? "menusel" : ""; // set the style of the current element
			}
			var submenu = document.getElementById("submenu");
			submenu.innerHTML = (index >= 0) ? submenu_data[index] : ""; // pull submenu of current element
		} 
	}	
	if(found!=1) { // none of the elements matched the url or it was the homepage
		for(i=0; i< row.cells.length; i++) {
			var cell = row.cells[i];
			cell.id = ""; // remove current formatting on main menu
		}
		var submenu = document.getElementById("submenu");
		submenu.innerHTML = ""; // remove current submenu
	}
}

function setMenuByIndex(index) { // sets the current element based on an index (mouseover only)
	var row = document.getElementById("menu").rows[0];
	for(i=0; i< row.cells.length; i++) {
		var cell = row.cells[i];
		cell.id = (i == index) ? "menusel" : ""; // set the style of the element at the index
	}
	var submenu = document.getElementById("submenu");
	submenu.innerHTML = (index >= 0) ? submenu_data[index] : ""; // pull submenu of element at the index
}

function getSrcObj(e) {
	if(!e) var e = window.event;
	var targ;
	if(e.target) targ=e.target;
	else if(e.srcElement) targ=e.srcElement;

	return targ;
}

function doEvent(e) {
	var src = getSrcObj(e)
	if(src.tagName == "TD") {
		setMenuByIndex(src.cellIndex); // on mouseover, set current element at the cell index
	}
}

function startTimer() {
  killTimer();
  menuTimer = setTimeout("resetMenu();", 750);
}

function killTimer() {
  if(menuTimer != null) {
    clearTimeout(menuTimer);
    menuTimer = null;
  }
}

function resetMenu() {
  setMenu(menuDefault); 
  menuTimer = null;
}

