YAHOO.util.Event.onContentReady("mainMenu", function () {

	var userAgent = YAHOO.env.ua,
  animation;  // Animation instance
  
  function onSubmenuBeforeShow(p_sType, p_sArgs) {

  	var menuBody,
    currentElement,
    shadow,
    currentList;
                
    if (this.parent) {

        currentElement = this.element;

        shadow = currentElement.lastChild;
        shadow.style.height = "0px";
    
        if (animation && animation.isAnimated()) {
        
            animation.stop();
            animation = null;
        
        }

        menuBody = this.body;

        if (this.parent && 
            !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
      

            if (userAgent.gecko || userAgent.opera) {
            
                menuBody.style.width = menuBody.clientWidth + "px";
            
            }
            
            if (userAgent.ie == 7) {

                currentElement.style.width = currentElement.clientWidth + "px";

            }
        
        }
    
        menuBody.style.overflow = "hidden";

        currentList = menuBody.getElementsByTagName("ul")[0];

        currentList.style.marginTop = ("-" + currentList.offsetHeight + "px");
    
    }

	}

  function onTween(p_sType, p_aArgs, p_shadow) {

      if (this.cfg.getProperty("iframe")) {
      
          this.syncIframe();
  
      }
  
      if (p_shadow) {
  
          p_shadow.style.height = this.element.offsetHeight + "px";
      
      }
  
  }

  function onAnimationComplete(p_sType, p_aArgs, p_shadow) {

      var menuBody = this.body,
          currentList = menuBody.getElementsByTagName("ul")[0];

      if (p_shadow) {
      
          p_shadow.style.height = this.element.offsetHeight + "px";
      
      }
      
      currentList.style.marginTop = "";
      menuBody.style.overflow = "";

      if (this.parent && 
          !(this.parent instanceof YAHOO.widget.MenuBarItem)) {


          if (userAgent.gecko || userAgent.opera) {
          
              menuBody.style.width = "";
          
          }
          
          if (userAgent.ie == 7) {

              this.element.style.width = "";

          }
      
      }
      
  }

  function onSubmenuShow(p_sType, p_sArgs) {

      var currentElement,
          shadow,
          currentList;
  
      if (this.parent) {

          currentElement = this.element;
          shadow = currentElement.lastChild;
          currentList = this.body.getElementsByTagName("ul")[0];

          animation = new YAHOO.util.Anim(currentList, 
              { marginTop: { to: 0 } },
              .5, YAHOO.util.Easing.easeOut);


          animation.onStart.subscribe(function () {

              shadow.style.height = "100%";
          
          });


          animation.animate();

          if (YAHOO.env.userAgent.ie) {
              
              shadow.style.height = currentElement.offsetHeight + "px";

              animation.onTween.subscribe(onTween, shadow, this);

          }

          animation.onComplete.subscribe(onAnimationComplete, shadow, this);
      
      }
  
  }

  var navBar = new YAHOO.widget.MenuBar("mainMenu", { 
                                              autosubmenudisplay: true, 
                                              hidedelay: 150, 
                                              lazyload: true,
                                              zindex: 999 });
  
  navBar.subscribe("beforeShow", onSubmenuBeforeShow);
  navBar.subscribe("show", onSubmenuShow); 

  navBar.render();          

});