/*
  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. 
 */
function MenuUtil(menuId) {	
	this.minMenuWidth = 0;	
	this.menuId = menuId;		
}
MenuUtil.prototype = {
	addMenuBarItemId: function (elemId) {
		this.minMenuWidth += document.getElementById(elemId).offsetWidth;	
		document.getElementById(this.menuId).style.width = this.minMenuWidth + "px";
	}
};

/*
  Creates a menu object utilizing either a fade or sliding transition.
  elemId - the element id or object that represents the menu
  hideMS - the number of milliseconds to wait before hiding the submenuing
  animDuration - this is the duration of the animation
  transitionType - the type of effect for menu display, this may be fade or slide.
*/
function RichMenu(elemId, hideMS, animDuration, transitionType) {		
	this.elemId = elemId;
	this.hideMS = hideMS;
	this.fadeDuration = animDuration;
	this.transitionType = transitionType;
	this.menu = null;
	if (this.elemId !== 'undefined' && this.elemId !== null) {				
		this.renderMenu();
	}
}
RichMenu.prototype = {		
	renderMenu: function () {		
		try {			
			if (YAHOO !== 'undefined' && YAHOO !== null) { 								
				if (this.transitionType === 'fade') { this.renderFadeMenu(); }
				else if (this.transitionType === 'slide') { this.renderSliderMenu(); }
				else { this.renderBasicMenu(); }
			}			
		}catch (err) { 	}
	},
	renderBasicMenu: function () {
		this.menu = new	YAHOO.widget.MenuBar(this.elemId, { autosubmenudisplay: true, hidedelay: this.hideMS, lazyload: true, iframe: true });
		this.menu.render();	
		document.getElementById(this.elemId).style.visibility = "visible";	
	},
	renderFadeMenu: function () {
		this.menu = new	YAHOO.widget.MenuBar(this.elemId, { autosubmenudisplay: true, hidedelay: this.hideMS, lazyload: true, iframe: true, effect: { effect: YAHOO.widget.ContainerEffect.FADE, duration: this.fadeDuration } });
		this.menu.render();	
		document.getElementById(this.elemId).style.visibility = "visible";	
	},
	renderSliderMenu: 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 } }, 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);                    
     }                
    }
    
     this.menu = new YAHOO.widget.MenuBar(this.elemId, {autosubmenudisplay: true, hidedelay: this.hideMS, lazyload: true });                
     this.menu.subscribe("beforeShow", onSubmenuBeforeShow);
     this.menu.subscribe("show", onSubmenuShow);
     this.menu.render();          
     document.getElementById(this.elemId).style.visibility = "visible";
	}
};


