var menuItems = [
   ["RebAnderson.org",              "index.html"],
   ["Teaching<br>Schedule",         "schedule.php"],
   ["Biography",                    "bio.html"],
   ["No Abode<br>Hermitage",        " http://noabode.org"], // the leading whitespace is to force opening in the same window
   ["San Francisco<br>Zen Center",  "http://www.sfzc.org"], // leading 'http' will cause opening in a new window
   ["Koan Class",                   "KoanClass.html"],
   ["Dharma Talks",                 "talks.html"],
   ["Mailing List",                 "mlistsignup.html"],
   ["Contact",                      "contact.html"]
];

function drawMenu() {
	var menuHTML = "<span id='menu' name='menu'>\n<br>";
   for (i = 0; i < menuItems.length; i++) {
      var targetHTML = "";
      if (menuItems[i][1].match(/^http/)) targetHTML = " target='_blank'";
      menuHTML += "<a href=" + menuItems[i][1] + targetHTML + ">" + 
                  "<FONT COLOR='#FFFFFF' FACE='Arial,Helvetica' SIZE=-1>" +
                  "<b>" + menuItems[i][0] + "</b></font></a><br><br>\n";
   }
   menuHTML += '</span>\n';
	document.write(menuHTML);
   paintMenuItems();
}

function paintMenuItems() {
	var pageURI = document.location;
	var pagePath = getPath(pageURI);
	var menuElem = document.getElementById("menu");
	//document.write('<br>menuElem: ' + menuElem.nodeName);
	//var elCount = menuElem.childElementCount;	
	//document.write('<br>' + elCount);
	//document.write('<br>pageUri: ' + pageURI);
	//document.write('<br>pagePath: ' + pagePath);

	var menuItemElem = menuElem.firstElementChild;
	if (!menuItemElem) menuItemElem = menuElem.firstChild;
	//document.write('<br>menuItemElem: ' + menuItemElem.nodeName);

	var tmp;
	
	//document.write('<p>Children of menu:</p><ol>');
	while (menuItemElem) {
		//document.write('<li>' + menuItemElem.nodeName + '</li>');
		if (menuItemElem.nodeName == 'A') {
			//var cPath = menuItemElem.getAttribute('href').replace('#http://[^/]/#', '');
			var hrf = menuItemElem.getAttribute('href');
			var cPath = getPath(menuItemElem.getAttribute('href'));
			var cPathRE = new RegExp(cPath + '$');
			//document.write('<br>hrf: ' + hrf);
			//document.write('<br>cPath: ' + cPath);
			//document.write('<br>cPathRE: ' + cPathRE.toString());
			if (pagePath != '' && pagePath == cPath || pagePath == '' && cPath == 'index.html') {
			//if (pagePath != '' && pagePath.match(cPathRE) || pagePath == '' && cPath == 'index.html') {
				var fontElem = menuItemElem.firstElementChild;
				if (!fontElem) fontElem = menuItemElem.firstChild;
				fontElem.setAttribute('color', 'cyan');
				//document.write(' ***');
			}
		}
		//menuItemElem = menuItemElem.nextElementSibling;
		tmp = menuItemElem.nextElementSibling;
		if (tmp) menuItemElem = tmp;
		else menuItemElem = menuItemElem.nextSibling;
  	}	
	//document.write('</ol>');
}

function getPath(uri) {
	var path = new String(uri);
	//document.write('<br>' + path);
	//path = path.replace(/(http[s]?:\/\/)?(www.)?[^/]*[/]?/,'');
	path = path.replace(/(http[s]?:\/\/)(www.)?[^/]*[/]?/,'');
	//document.write('<br>' + path);

	//document.write('<br>' + uri);
	return path;
}


