//-----------------------------------------------------------------------------
// Code to clear submenus and highlighted items when mouse is outside the menus
//-----------------------------------------------------------------------------

if (browser.isIE)
  document.onmouseover = pageMouseover;
else
  document.addEventListener("mouseover", pageMouseover, true);
 
function pageMouseover(event) {

  var el;
  
  // Find the element that mouse is on.

  if (browser.isIE)
    el = window.event.srcElement;
  else
    el = (event.target.tagName ? event.target : event.target.parentNode);
       //clear any visible submenus when mouse is not on a menu
   
   if (getContainerWith(el, "DIV", "menu") == null) {
     closeSubMenu(document.getElementById('mainmenu')); 
	 closeSubMenu(document.getElementById('sectionhdr')); 
	   // clear any highlighted menu item when when mouse is not on a menu
	 
     return false;
  }  
}
//----------------------------------------------------------------------------
// Code to handle the menus and sub menus.
//----------------------------------------------------------------------------

function menuMouseover(event) {

  var menu;

    // Find the target menu element.
  if (browser.isIE){
    menu = getContainerWith(window.event.srcElement, "DIV", "menu");
  }else
    menu = event.currentTarget;																																									
	
    // Close any active sub menu.
	//alert(menu.activeItem);
  if (menu.activeItem != null)
    closeSubMenu(menu);
}

function menuItemMouseover(event, menuId) {
  var item, menu, x, y;

    // Find the target item element and its parent menu element.

  if (browser.isIE){
    item = getContainerWith(window.event.srcElement, "A", "menuItem");
  }else
    item = event.currentTarget;
  
  menu = getContainerWith(item, "DIV", "menu");
  
 
    // Close any active sub menu and mark this one as active
  if (menu.activeItem != null)
    closeSubMenu(menu);
    menu.activeItem = item;
  // alert(item.subMenu);
    // Initialize the sub menu, if not already done.
  if (item.subMenu == null) {
    item.subMenu = document.getElementById(menuId);
	//alert(item.subMenu.className);
	//alert(item.subMenu.isInitialised);
  if (item.subMenu.isInitialized == null)
    menuInit(item.subMenu);
  }
  
    // Get position for submenu based on the menu item.
  x = getPageOffsetLeft(item) + item.offsetWidth;
  y = getPageOffsetTop(item);
  
  // For IE, adjust position.

  if (browser.isIE) {
    //x += button.offsetParent.clientLeft;
    //y += button.offsetParent.clientTop;
	//x += 7;
	//y += 7;
  }
    // Adjust position to fit in view.
    // This is to deal with submenus of submenus cascading down 
    // and to the right and leaving the viewing area.  
 

  item.subMenu.style.left = x + "px";
  item.subMenu.style.top  = y + "px";
  item.subMenu.style.visibility = "visible";
 
	 
  // Highlight the item element.

  item.className += " menuItemHighlight";

   
    // Stop the event from bubbling.
  if (browser.isIE)
    window.event.cancelBubble = true;
  else
    event.stopPropagation(); 
}

function closeSubMenu(menu) {
  if (menu == null || menu.activeItem == null)
    return;
 
    // Recursively close any sub menus.
  if (menu.activeItem.subMenu != null) {
    closeSubMenu(menu.activeItem.subMenu);
    menu.activeItem.subMenu.style.visibility = "hidden";
    menu.activeItem.subMenu = null;
  }

  removeClassName(menu.activeItem, "menuItemHighlight");
  menu.activeItem = null;
}



//----------------------------------------------------------------------------
// Code to initialize menus.
//----------------------------------------------------------------------------

function initSample(mainMenuId) {
  menuInit(document.getElementById(mainMenuId));
}

function menuInit(menu) {

  var itemList, spanList;
  var textEl, arrowEl;
  var itemWidth;
  var w;
  var i, j;
  //alert (mainMenuId); 
    // get the width of the first menu item
  itemList = menu.getElementsByTagName("A");
  if (itemList.length > 0) 
    itemWidth = itemList[0].offsetWidth;
  else 
    return;
  
    // fix the anchor text width problem in IE
  if (browser.isIE) {
    itemList = menu.getElementsByTagName("A");
    w = getMaxItemWidth (itemList);
    for (i=0; i < itemList.length; i++) {
      itemList[i].style.width = w + "px";
	}
  }
  
    // Mark menu as initialized.
  menu.isInitialized = true;
}


function getMaxItemWidth (itemList) {
  var maxw=0;
  var w, dw;
  var i;

  for (i=0; i < itemList.length; i++) {
    w = itemList[i].offsetWidth;
    itemList[i].style.width = w + "px";
    dw = itemList[i].offsetWidth - w;
    w -= dw;
	if (maxw < w)
	  maxw = w;
  }
  return maxw;
}



//----------------------------------------------------------------------------
// General utility functions.
//----------------------------------------------------------------------------

function getContainerWith(node, tagName, className) {

  // Starting with the given node, find the nearest containing element
  // with the specified tag name and style class.

  while (node != null) {
    if (node.tagName != null && node.tagName == tagName && hasClassName(node, className))
      return node;
	  
    node = node.parentNode;
  }

  return node;
}


function hasClassName(el, name) {
  var i, list;

    // Return true if the given element currently has the given class name.
  list = el.className.split(" ");
  for (i = 0; i < list.length; i++){
    if (list[i] == name){
      return true;
    }
  }
  return false;
}


function removeClassName(el, name) {

  var i, curList, newList = "";

  if (el.className == null)
    return;

    // Remove the given class name from the element's className property.
  curList = el.className.split(" ");

  for (i = 0; i < curList.length; i++){
    if (curList[i] != name){
	 newList += curList[i];
	}
  }
 
  el.className = newList;
}

