function MenuUtil(){};
MenuUtil.yuimenubaritemWidth = new Number(0);
/*
  The menus are build as children of a div which by default will fill the container space
  provided.  In order to center the the menu on the screen using css the true width must be 
  calculated and the width of the menu explicity set. Note this script is written assuming 
  we may have only ONE menu using this per page.
  menuId - the id of the menu.
 */
MenuUtil.condense = function( menuId ){	
	var menu = document.getElementById( menuId );	
	MenuUtil.yuimenubaritemWidth=new Number(0);
	MenuUtil._consolidateWidth(menu);	
	menu.style.width=MenuUtil.yuimenubaritemWidth+"px";
	menu.style.visibility="visible";
}

MenuUtil._consolidateWidth = function( node ){
	var i=0;
	var children = node.childNodes;
	if( children && children.length >0 ){
		while(i<children.length){
			var nodeObj = children[i];	var classNameStr = nodeObj.className;					
			if( classNameStr && classNameStr.search("yuimenubaritem") != -1 && nodeObj.nodeName == "LI" ){
				MenuUtil.yuimenubaritemWidth += new Number( nodeObj.offsetWidth );
			}
			if(nodeObj.hasChildNodes()){  MenuUtil._consolidateWidth( nodeObj);  }
			i++;
		}
	}	
}

/*
  Creates a menu capable of fading its sub menus in and out.  
  elemId - the id of the menu markup structure container
  hideMS - the number of milliseconds before the menu will disappear when mousing out
  fadeDuration - the number of seconds it takes to fade the menu ( decimal ok )
 */
function FadeMenu( elemId, hideMS, fadeDuration ){	
	YAHOO.util.Event.onContentReady(elemId, function(){
		var oMenuBar = 
			new	YAHOO.widget.MenuBar( elemId, { autosubmenudisplay:true,
																				  hidedelay:hideMS,
																				  lazyload:true,
																				  iframe:true,
																				  effect:{ effect: YAHOO.widget.ContainerEffect.FADE, duration:fadeDuration }
																				}
			);						
		oMenuBar.render();			
		MenuUtil.condense( elemId );											  																					  
	});	
}

/*
  Creates a menu capable of sliding its menus down when activated  
  elemId - the id of the menu markup structure container
  hideMS - the number of milliseconds before the menu will disappear when mousing out
 */
function SlideMenu( elemId, hideMS ){
  YAHOO.util.Event.onContentReady(elemId, function () {
    var ua = YAHOO.env.ua, oAnim; 
		function onSubmenuBeforeShow(p_sType, p_sArgs) {
      var oBody, oElement, oShadow, oUL;                
      if (this.parent) {
        oElement = this.element;
        oShadow = oElement.lastChild;
        oShadow.style.height = "0px";
        if (oAnim && oAnim.isAnimated()) {                        
           oAnim.stop();
           oAnim = null;                        
        }
        oBody = this.body;
        if (this.parent && !(this.parent instanceof YAHOO.widget.MenuBarItem)) {                        
           if (ua.gecko) { oBody.style.width = oBody.clientWidth + "px"; }                                                        
           if (ua.ie == 7) { oElement.style.width = oElement.clientWidth + "px"; }                        
        }   
        oBody.style.overflow = "hidden";
        oUL = oBody.getElementsByTagName("ul")[0];
        oUL.style.marginTop = ("-" + oUL.offsetHeight + "px");                    
      }
    };

    function onTween(p_sType, p_aArgs, p_oShadow) {
      if (this.cfg.getProperty("iframe")) { this.syncIframe(); }                
      if (p_oShadow) { p_oShadow.style.height = this.element.offsetHeight + "px"; }                
    };

    function onAnimationComplete(p_sType, p_aArgs, p_oShadow) {
     var oBody = this.body,
     oUL = oBody.getElementsByTagName("ul")[0];

     if (p_oShadow) { p_oShadow.style.height = this.element.offsetHeight + "px"; }
     oUL.style.marginTop = "";
     oBody.style.overflow = "";                    
     if (this.parent && !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
       if (ua.gecko) { oBody.style.width = ""; }                        
       if (ua.ie == 7) { this.element.style.width = ""; }                    
     }                    
   };

   function onSubmenuShow(p_sType, p_sArgs) {
     var oElement,oShadow,oUL;               
     if (this.parent) {
       oElement = this.element;
       oShadow = oElement.lastChild;
       oUL = this.body.getElementsByTagName("ul")[0];
       oAnim = new YAHOO.util.Anim(oUL, { marginTop: { to: 0 } },.5, YAHOO.util.Easing.easeOut);

       oAnim.onStart.subscribe( function(){ oShadow.style.height = "100%"; } );
       oAnim.animate();

       if (YAHOO.env.ua.ie) {                           
         oShadow.style.height = oElement.offsetHeight + "px";
         oAnim.onTween.subscribe(onTween, oShadow, this);    
       }    
       oAnim.onComplete.subscribe(onAnimationComplete, oShadow, this);                    
     }                
   };

   var oMenuBar = 
     new YAHOO.widget.MenuBar(elemId, {autosubmenudisplay: true, 
                                       hidedelay: hideMS, 
                                       lazyload: true });
                
     oMenuBar.subscribe("beforeShow", onSubmenuBeforeShow);
     oMenuBar.subscribe("show", onSubmenuShow);
     oMenuBar.render();          
     MenuUtil.condense( elemId );	
  });

}

