var strmenu='leftmenu';
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 == strmenuopened) {
					// a[i].parentNode.style.backgroundColor = '#DCE9F5';
				}
		                  
				if(a[i].className || a[i].parentNode.className) {
					// only assign the event handler if it's actually a parent and not just an active link or we'll get an error
					if(a[i].className != 'smsubactive') {
						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 (well, 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 and make it look normal if it's an expanded parent      
       if( menus[i].className == strmenuon ) {
		 menus[i].className = strmenuoff;
		 // menus[i].style.backgroundColor = "#CCDFEF";
		 
		 // 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') {
			// changed this because all L1 have links as opposed to ICA.SE where some were only containers
			// colmenu(tg,ns);
			document.location.href = tg.href;
			
		}
		else {
			if( isMainMenu(tg) ) {
				closeMenus();
			}
			// expmenu(tg,ns);
			document.location.href = tg.href;
			
		} 
  }
  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 (expand called by the window init sub
  if( page_loaded == 0)
  {
    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);