/** RSMenu constructor
 *
 * @param string menuBarId: the id of the menu bar element, 
 *                          and the id pattern for all other elements
 * @param function userDoSetupItem
 * @param function userDoShow
 * @param function userDoHide
 */
function RSMenu( menuBarId, userDoSetupItem, userDoShow, userDoHide ) 
{
  this.menuBarDiv = document.getElementById( menuBarId );
  if ( ! this.menuBarDiv ) 
  {
    throw "Menu Bar Element Not Found";
  }

  this.currentMenu = 0;
  this.menuBarItems = new Object();
  this.menuLists = new Object();

  this.userSetupItem = "function" == typeof userDoSetupItem ? userDoSetupItem : null;
  this.userDoShow = "function" == typeof userDoShow ? userDoShow : null;
  this.userDoHide = "function" == typeof userDoHide ? userDoHide : null;

  var self = this;

/** showMenu() - handler for onmouseover event
 *
 * user callback: userDoShow
 */
  this.showMenu = function( item ) 
  {
    if ( self.currentMenu && self.currentMenu != item ) 
    {
      /* call procedure 'hide menu' */
      self.doHide();
    }
    self.currentMenu = item;
    self.menuLists[item].style.display = "block";
    if ( self.userDoShow )
    {
      self.userDoShow( self );
    }
  } // showMenu()

/** hideMenu() - handler for onmouseout event
 *
 */
  this.hideMenu = function( event ) 
  {
    if ( ! self.currentMenu ) 
    {
      return 0;
    }
    var el = window.event ? window.event.toElement : event.relatedTarget;
    var id = self.currentMenu ;
    do { 
      if( id == el.id ) {
        return 0;
      }
      el = el.parentNode;
    } while ( el ) ;

    self.doHide();
  } // hideMenu()

/** doHide() - actually do the hiding of menu popup
 *
 * user callback: userDoHide
 */
  this.doHide = function()
  {
    self.menuLists[self.currentMenu].style.display = "none";
    if ( self.userDoHide )
    {
      self.userDoHide( self );
    }
    self.currentMenu = null;
  } // doHide()


/** setupMenuBarItem() - the menu bar items initialization function
 *
 */
  this.setupMenuBarItem = function( id, mbarItem, mList ) 
  {
    /* setup mouse events */
    mbarItem.onmouseover = function() { self.showMenu(id) } ;
    mbarItem.onmouseout = this.hideMenu;
    mList.onmouseout = this.hideMenu;

    this.menuBarItems[ id ] = mbarItem;
    this.menuLists[ id ] = mList;
    if ( this.userSetupItem )
    {
      this.userSetupItem( this, id, mbarItem, mList ) ;
    }
  } // setupMenuBarItem()

  for ( var i in this.menuBarDiv.childNodes ) 
  {
    var el = this.menuBarDiv.childNodes[i];
    if( ! el.id )
    {
      continue;
    }
    var menuItem, menuList;
    for( var j in el.childNodes )
    {
      var ch = el.childNodes[j];
      if ( ch.className ) 
      {
        if( ch.className.indexOf("menu-item") > -1 )
        {
          menuItem = ch;
        }
        else if( ch.className.indexOf("menu-list") > -1 )
        {
          menuList = ch;
        }
      }
    } // for
    if ( menuItem && menuList )
    {
      this.setupMenuBarItem( el.id, menuItem, menuList );
    }
  } // for

} // RS_Menu()

