//
// Simple-minded, two level menu system.
// Author: Bjørn Hell Larsen, blarsen@nonews.net, 2005-03-03
//
// This work is licensed under the Creative Commons Attributions License,
// see http://creativecommons.org/licenses/by/2.0/.
//
// You are free:
//
//    * to copy, distribute, display, and perform the work
//    * to make derivative works
//    * to make commercial use of the work
//
// Under the following conditions:
//    * Attribution. You must give the original author credit.
//    * For any reuse or distribution, you must make clear to others the license terms of this work.
//    * Any of these conditions can be waived if you get permission from the copyright holder.
//
// Your fair use and other rights are in no way affected by the above.
//
// 


// display submenu, or hide it
function toggleSubMenu(id) {
   elem = document.getElementById(id);
   if (!elem) {
      return;
   }
   if (elem.className == "submenu") {
	 // hide all other menus
     for (i=1; i < 99; i++) {
         xelem = document.getElementById("submenu"+i);
         if (xelem) {
           xelem.className = "submenu";
         }
     }
     // show this menu
     elem.className = "submenuvisible";
   } else {
     // hide this menu
     elem.className = "submenu";
   }
}

// select an item in the menu. called from body onLoad event handler.
// expand submenus as needed.
function selectMenu() {
   href = location.href;
   re = new RegExp('http...www[^/]+/');
   path = "/" + href.replace(re, '');

   selectedelem = 0;
   selectedmainindex = 0;

   for (i=1; i < 99; i++) {
     xelem = document.getElementById("mmain"+i);
     if (xelem) {
	   if (xelem.childNodes[0].nodeType == 1) { // element
       ehref = xelem.childNodes[0].getAttribute("href");
	     if (ehref == path || ehref == href) { // does this entry point to this page? (double, browser-independent check. :-)
	       selectedelem = xelem;
           selectedmainindex = i;
	     }
       }
     }
     for (ii=1; ii < 99; ii++) {
       xelem = document.getElementById("msub"+i+"_"+ii);
       if (xelem) {
	     if (xelem.childNodes[0].nodeType == 1) {
	       ehref = xelem.childNodes[0].getAttribute("href");
           if (ehref == path || ehref == href) {
	         selectedelem = xelem;
 	       }
         }
       }
     }
   }
	
   elem = selectedelem;
   if (!elem) {
      return;
   }
   elem.className = elem.className + " selected";

   // expand containing submenu if needed
   if (elem.parentNode.className == "submenu") {
      elem.parentNode.className = "submenuvisible";
   }

   // expand submenu if the main menu entry was selected
   if (selectedmainindex) {
     toggleSubMenu("submenu"+selectedmainindex);
   }
}
