
var menuCloseList = new Array();
var menuCloseTimer = null;


function menuObj() {

   this.root = null;
   this.curSubmenu = 0;

   this.init = function( root ) {
      var openerURL;
      var children;

      this.root = root;
      children = root.childNodes;

      for( i = 0; i < children.length; i++ ) {
         if( children[i].tagName == 'LI' ) {

            childLinks = children[i].getElementsByTagName('A');
            if( childLinks.length < 1 )
               continue;

            openerURL = childLinks[0];

            childMenus = children[i].getElementsByTagName('UL');

            // if there are submenus, we only want the first one
            // (only 1 submenu per nav)
            if( childMenus.length > 0 ) {
   
               this.curSubmenu++;
               submenu = childMenus[0];
               submenu.id = 'menu'+ this.curSubmenu;
               submenu.onmouseover = function() {
                  if( typeof menuObj == 'function' )
                     menuObj.cancelClose();
               }
               submenu.onmouseout = function() {
                  if( typeof menuObj == 'function' )
                     menuObj.scheduleClose();
               }


               childLinks = children[i].getElementsByTagName('A');
               if( childLinks.length < 1 )
                  continue;

               openerURL.rel = submenu.id;
               openerURL.onmouseover = function() {
                  if( typeof menuObj == 'function' ) {
                     menuObj.open( this.rel );
                  }
               }

               openerURL.onmouseout = function() {
                  if( typeof menuObj == 'function' )
                     menuObj.scheduleClose();
               }
            } else {
               openerURL.onmouseover = function() {
                  if( typeof menuObj == 'function' ) {
                     menuObj.closeAll();
                  }
               }
            }
         }
      }
   }
}


menuObj.open = function( menuID ) {
   menuObj.closeAll();
   menu = document.getElementById( menuID );
   menu.style.display = 'block';

   menuCloseList.unshift( menuID );
}

menuObj.scheduleClose = function() {
   menuObj.cancelClose();
   menuCloseTimer = setTimeout( "menuObj.close()", 500 );
}

menuObj.cancelClose = function() {
   if( menuCloseTimer != null ) {
      clearTimeout( menuCloseTimer );
      menuCloseTimer = null;
   }
}

menuObj.close = function() {
   if( menuCloseList.length < 1 )
      return;

   menuObj.cancelClose();

   var menuID = menuCloseList.shift();

   menu = document.getElementById( menuID );
   menu.style.display = 'none';
}

menuObj.closeAll = function() {
   for( i = 0; i < menuCloseList.length; i++ ) {
      menuObj.close();
   }
}

