var strmenu='submenu';
var strmenuoff='smsuboff';
var strmenuon='smsubon';
var strmenuopened='smsubactive';
var page_loaded;

// initmenu
//
// run this when page is loaded to:
// * bind the togglemenu event handler to all anchors with classes (=parents) in the menu
// * highlight+open the active menu
function initmenu() {  
  page_loaded = 0;  
  
  if(supportDOM() && !ie501win() ) {
    if (document.getElementById(strmenu) ) {

      var o,a=document.getElementById(strmenu).getElementsByTagName('A');

      for(var i=0;i<a.length;i++){
        if(a[i].className || a[i].parentNode.className){          
          a[i].onclick=togglemenu;
	  
          o=a[i].parentNode.getElementsByTagName('UL').item(0)
          
          if(o&&a[i].className==strmenuopened) {
            page_loaded = 1;
            expmenu(a[i],o);
          }
          else if(o&&a[i].parentNode.className==strmenuopened) {
	    page_loaded = 1;
            expmenu(a[i],o);
          }
        }
      }
    }
  }
}


// closeMenus
//
// close open main menus (only one, most probably...)
function closeMenus() {

  if(!ie501win()&&supportDOM()) {
    var menus = document.getElementById(strmenu).getElementsByTagName('LI');
    // loop through the menu items
    for(var i=0; i < menus.length; i++) {	
       // close it if it's an expanded parent
       if( menus[i].className == strmenuon ) {
         menus[i].className = strmenuoff;
         
         // hide the submenu child
	 var submenu = document.getElementById(menus[i].id).getElementsByTagName('UL');
	 submenu[0].style.display = 'none';

	 // remove the highlight on the menu (restore the menu arrow)
	 var subanchor = document.getElementById(menus[i].id).getElementsByTagName('A');
	 subanchor[0].className = null;
       }
       // also remove the highlight on the anchor if it has one
       if( menus[i].firstChild.className == strmenuopened ) {
         menus[i].firstChild.className = null;
       }
    }   
    return false;
  }
}


// togglemenu
//
// event handler bound to the onClick events on anchors with children
// expands the menu if it's collapsed and vice versa
function togglemenu(e) {
  var tg;
  if(!e)
    var e=window.event;
  
  if(e.target)
    tg=e.target;
  else if(e.srcElement)
    tg=e.srcElement; 
  
  while(tg.nodeType!=1)
    tg=tg.parentNode; // Safari hack
  
  var ns=tg.nextSibling;
  	
  while(ns.nodeType!=1) {
    ns=ns.nextSibling;
    if(ns.style.display=='block') {
      colmenu(tg,ns);
    }
    else {
      if( isMainMenu(tg) ) {
        closeMenus();
      }   
      expmenu(tg,ns);      
    }
  }
  tg.blur();
  
  return false;
}


// isMainMenu
//
// goes through the main menu items and sees if targetNode is one of them
// this is so that level 2 submenus can be expanded without their parents
// getting closed first
function isMainMenu(targetNode) {
  var isMainMenuItem = false;

  var li=document.getElementById(strmenu).getElementsByTagName('LI');
  for(var i=0;i<li.length;i++) {

    // (1) if the id is numeric (only level 1 items have id's like "1", "10" et c)
    // (this is to save time by only checking with the main menu items)

    // (2) and the IDs match
    // = we've located targetNode as a main menu item   
    if( isNumeric(li[i].id) && li[i].id == targetNode.parentNode.id ) {
      isMainMenuItem = true;
    }
  }
  return isMainMenuItem;
}


// isNumeric
//
// determines if the parameter is numeric
function isNumeric(testString) {
  var validChars = "0123456789";
  var isNumber=true;
  var testChar;
 
  for (i=0; i<testString.length && isNumber == true; i++) { 
    testChar = testString.charAt(i); 

    if (validChars.indexOf(testChar) == -1) {
      isNumber = false;
    }
  }
  return isNumber;  
}


// expmenu
//
// expands the menu item connected to "tg" (target anchor of click) in togglemenu
function expmenu(tg,o) {
  // go to a new URL to reload the proper page + menu if
  // .) we just didn't load the page OR
  // .) we clicked on an un-expanded submenu and it has an URL(/startpage)  
  //    (otherwise the submenu URL ends with a # signifying no URL)
  
  if( (page_loaded == 0 || (!tg.className || tg.className != strmenuopened) ) && 
    	tg.href.charAt(tg.href.length-1) != "#" )  		
  {
    document.location.href = tg.href;  
  }
  // otherwise just do a normal expand
  else {
    o.style.display='block';
    if(tg.parentNode.className) {
      tg.parentNode.className=strmenuon;      
      tg.className=strmenuopened;
    }
    else {
      tg.className=strmenuon;
    }
  }
}


// colmenu
//
// collapses the menu item connected to "tg" (target anchor of click) in togglemenu
function colmenu(tg,o) {
  o.style.display='none';  
  if(tg.parentNode.className)
    tg.parentNode.className=strmenuoff;
  else
    tg.className=strmenuoff;
}


addEvent(window,'load',initmenu);
