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 = -1;
var submenu_data = Array();

// TODO: delete this function -- not using XPath?
function XPathEval(xpath, dom)
{
	if(dom.evaluate) {
		return dom.evaluate(xpath,dom,XPathResult.ANY_TYPE,null);
	} else if (dom.selectNodes) {
		return dom.selectNodes(xpath);
	}
	return null;
}

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) {
	// TODO: use regex to match. no cheating.
	// Check for scheme ?
	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, style, html = "";
	if(xmlent_menu.getAttributeNS) {
		var nsResolver
		// For systems that support XML Namespaces
		name = xmlent_menu.getAttributeNS(xns_menu, "name");
		href = xmlent_menu.getAttributeNS(xns_menu, "href");
	} else {
		// For 'dumb' systems that don't do namespaces
		name = xmlent_menu.getAttribute("m:name");
		href = xmlent_menu.getAttribute('m:href');
	}
	if(href) { // Avoid null-pointers, hence no links.
		href = uri_local(href);
		html = '<a href="' + href +'">';
	}
	html += name;
	if(href) html += '</a>';
	return html;
}

function setMenuDefault(i)
{
  menuDefault = i;
}

function initMenu() {
	var menu = document.getElementById("menu");
	var row = menu.rows[0];
	var xmlhttp = loadXMLDoc(sitemap_filename);
	if(null == xmlhttp)
		alert("Your browser does not support XMLHTTP!");

	// Get all menu-tags
	var x;
	if(xmlhttp.getElementsByTagNameNS) {
		x = xmlhttp.getElementsByTagNameNS(xns_menu, "menu");
	} else {
		x = xmlhttp.getElementsByTagName("m:menu");
	}

	// Populate Menu
	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]));
	}

	setMenu(menuDefault);
}

function setMenu(index) {
	var row = document.getElementById("menu").rows[0];
	for(i=0; i< row.cells.length; i++) {
		var cell = row.cells[i];
		cell.className = (i == index) ? "menusel" : "";
	}
	var submenu = document.getElementById("submenu");
	submenu.innerHTML = (index >= 0) ? submenu_data[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") {
		setMenu(src.cellIndex);
	}
}

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

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

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

