/**
* "render" Custom Event handler for a MenuModule instance.
* @private
* @param {String} p_sType The name of the event that was fired.
* @param {Array} p_aArgs Collection of arguments sent when the event
* was fired.
* @param {YAHOO.widget.MenuModule} p_oMenuModule The MenuModule instance that
* fired the event.
*/
YAHOO.widget.MenuModule.prototype._onRender =
    
    // FIXME
    // There is a bug here somewhere that sizes the width incorrectly. 
    // Seems to have something to to with the CSS "white-space" property.
    // Adding a few pixels to the width is a quick-fix.
    //
    function(p_sType, p_aArgs, p_oMenuModule) {
 
        if(this.cfg.getProperty("position") == "dynamic") {
    
            var sWidth = this.element.parentNode.tagName == "BODY" ?
                    this.element.offsetWidth : this._getOffsetWidth();
    
            this.cfg.setProperty("width", ((sWidth+24) + "px"));
    
        }
    
    };

function initMenuBar( event )
{
  var timeoutId;
  var portalMenu;
  
  // Hides submenus of the root Menubar instance
  function hideSubmenus() 
  {
    if (portalMenu.activeItem) {
      var submenu = portalMenu.activeItem.cfg.getProperty("submenu");
      if(submenu) { submenu.hide();  }
    }
  }

  // Cancels the call to "hideSubmenus"
  function cancelTimer()
  {
    if(timeoutId) {  window.clearTimeout(timeoutId);  }
  }
  // "mouseout" event handler for each submenu of the menubar
  function onSubmenuMouseOut( type, arguments, menuItem )
  {
    cancelTimer();
    timeoutId = window.setTimeout(hideSubmenus, 0);
  }
 
  // "mouseover" handler for each item in the menubar
  function onMenuBarItemMouseOver( type, arguments, menuItem )
  {
    var activeItem = this.parent.activeItem;
 
    // Hide any other submenus that might be visible
    if ( activeItem && this != activeItem ) {
      this.parent.clearActiveItem();
    }

    // Select and focus the current MenuItem instance
    this.cfg.setProperty("selected",true);
    this.focus();

    // Show the submenu for this instance
    var submenu = this.cfg.getProperty("submenu");
    if ( submenu ) { submenu.show();  }
   }
   
  // "mouseout" handler for each item in the menubar
  function onMenuBarItemMouseOut( type, arguments, menuItem )
  {
    this.cfg.setProperty("selected",false);
 
    var submenu = this.cfg.getProperty("submenu");
    if ( submenu ) {
      var event = arguments[0];
      var relatedTarget = YAHOO.util.Event.getRelatedTarget(event);

      if (false && !( relatedTarget == submenu.element || this._oDom.isAncestor(submenu.element,relatedTarget) )) {
        submenu.hide();
      }
 
    }
  }

  // "mouseover" handler for each item in the submenu
  function onSubmenuItemMouseOver( type, arguments, menuItem )
  {
    var activeItem = this.parent.activeItem;

    // Hide any other submenus that might be visible
    if ( activeItem && this != activeItem ) {
      this.parent.clearActiveItem();
    }

    // Select and focus the current MenuItem instance
    this.cfg.setProperty("selected",true);
    this.focus();
  }
  
  // "mouseout" handler for each item in the submenu
  function onSubmenuItemMouseOut( type, arguments, menuItem )
  {
    this.cfg.setProperty("selected",false);
  }
  

  // Instantiate and render the menubar and corresponding submenus
  portalMenu = new YAHOO.widget.MenuBar("sitenav")
  portalMenu.render();

  // Add a "mouseover" and "mouseout" event handler each item 
  // in the menu bar 
  var menuItems = portalMenu.getItemGroups()[0];
  var i = menuItems.length - 1;
  do {
    menuItems[i].mouseOverEvent.subscribe(onMenuBarItemMouseOver);
    menuItems[i].mouseOutEvent.subscribe(onMenuBarItemMouseOut);
  }
  while ( i-- )

  portalMenu.mouseOverEvent.subscribe(cancelTimer)
  
//  var conditionsMenu = portalMenu.getItem(1).cfg.getProperty("submenu")
//  conditionsMenu.mouseOverEvent.subscribe(cancelTimer);
//  conditionsMenu.mouseOutEvent.subscribe(onSubmenuMouseOut,conditionsMenu,true);
  
  YAHOO.util.Event.addListener(document,"click",hideSubmenus)
}

function initPageTree()
{
  var pagenav = YAHOO.util.Dom.get('pagenav-main');
  if ( !pagenav ) return;
  var pagenav_tree = new YAHOO.widget.TreeView(pagenav);

  buildTextNodeTreeFromDlist(pagenav_tree.getRoot(),pagenav)
  
	pagenav_tree.setExpandAnim(YAHOO.widget.TVAnim.FADE_IN);
	pagenav_tree.setCollapseAnim(YAHOO.widget.TVAnim.FADE_OUT);
  pagenav_tree.draw();
}

function buildTextNodeTreeFromDlist( root_node, dlist )
{
  var term = new Array();
  var body_id = dlist.ownerDocument.body.id
  var body_id_match = body_id.match(/(main-)|(major-(\d+)-)(minor-(\d+)-)?item-(\d+)/)

  var child = dlist.childNodes
  for ( var n=0;  n<child.length;  ++n )
  {
    if ( 1 != child[n].nodeType ) { continue }
    
    if ( "DT" == child[n].nodeName ) {
      term[term.length] = { dt: child[n] }
      
      if ( "pagenav-"+body_id == child[n].id ) {
        term[term.length-1].open = true
        term[term.length-1].style = "ygtvlabel current"
      }
      else if ( body_id_match && "pagenav-main-item-"+body_id_match[3] == child[n].id ) {
        term[term.length-1].open = true
      }
      else if ( body_id_match && "pagenav-major-"+body_id_match[3]+"-item-"+body_id_match[5] == child[n].id ) {
        term[term.length-1].open = true        
      }
    }
    else if ( "DD" == child[n].nodeName ) {
      term[term.length-1].dd = child[n]
    }
  }


  for ( var n=0;  n<term.length;  ++n )
  {
    var anchor = YAHOO.util.Dom.getElementsByClassName("","a",term[n].dt)[0]
    var node = new YAHOO.widget.TextNode({label:anchor.firstChild.nodeValue,href:anchor.href,style:term[n].style},root_node,term[n].open)
    
    var child = term[n].dd.childNodes
    for ( var m=0;  m<child.length;  ++m )
    {
      if ( 1 != child[m].nodeType ) { continue }
      
      if ( "DL" == child[m].nodeName ) {
        buildTextNodeTreeFromDlist(node,child[m])
      }
      else if ( "UL" == child[m].nodeName ) {
        buildTestNodeTreeFromUlist(node,child[m])
      }
    }
  }
}

function buildTestNodeTreeFromUlist( root_node, ulist )
{
  var term = new Array()
  var body_id = ulist.ownerDocument.body.id
  
  var child = ulist.childNodes
  for ( var n=0;  n<child.length;  ++n )
  {
    if ( 1 != child[n].nodeType ) { continue }
    
    if ( "LI" == child[n].nodeName ) {
      var anchor = YAHOO.util.Dom.getElementsByClassName("","a",child[n])[0]
      var node = new YAHOO.widget.TextNode({label:anchor.firstChild.nodeValue,href:anchor.href,style:("pagenav-"+body_id == child[n].id ? "ygtvlabel current" : null)},root_node,false)
    }
  }
}



//Generic function to toggle popups DAL 
function toggle(divId)
{
var Id = divId;
if (document.layers)
  {
    if(document.layers[Id].visibility == "hide")
    { show(Id);
      return;
    }
    else if (document.layers[Id].visibility == "show")
    { hide(Id);
      return;
    }
  }
if (document.all)
  { if(document.all[Id].style.display == "none")
      { show(Id);
        return;
      }
    else if (document.all[Id].style.display == "inline")
      { hide(Id);
        return;
      }
  }
else if (document.getElementById)
  {
    if(document.getElementById(Id).style.display == "none")
      { show(Id);
        return;
      }
    else if (document.getElementById(Id).style.display == "inline")
      { hide(Id);
       return;
      }
  }
}

//hides div
function hide(divId) {
if (document.layers) document.layers[divId].visibility = 'hide';
else if (document.all) document.all[divId].style.display = 'none';
else if (document.getElementById) document.getElementById(divId).style.display = 'none';
}

//shows div
function show(divId) {
if (document.layers) document.layers[divId].visibility = 'show';
else if (document.all) document.all[divId].style.display = 'inline';
else if (document.getElementById) document.getElementById(divId).style.display = 'inline';
}


