YAHOO.util.Event.onDOMReady(function () {

 var ua = YAHOO.env.ua, oAnim;
  var m = new YAHOO.widget.MenuBar('mainNavigation', { autosubmenudisplay: true, position: 'static', visible: true, lazyload: true });

 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 || ua.opera) {                            
                                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 = "";
                    //  Check if the menu is a submenu of a submenu.
                    if (this.parent && 
                        !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
                        if (ua.gecko || ua.opera) {
                            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];
                      	var par = YAHOO.util.Dom.getAncestorByClassName(oUL,"menuItem");
                      	 
                       var regMin = YAHOO.util.Dom.getRegion(par);                  
                       var reg = YAHOO.util.Dom.getRegion(oUL);
                       if( (reg.right-reg.left) < (regMin.right - regMin.left)) {
                       		oUL.style.width=(regMin.right - regMin.left -2) + "px";
                       	}
                        
                        var speed = (Math.abs(parseInt(oUL.style.marginTop)) )/200;  // our speed in px/s
                   
                        oAnim = new YAHOO.util.Anim(oUL, 
                            { marginTop: { to: 0 } },
                            speed, 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);
                    }
           }         

	  m.subscribe("beforeShow", onSubmenuBeforeShow);
	  m.subscribe("show", onSubmenuShow);
	 
	 m.render();
});

