// New Mind - MJJAMES - YUI Navigation

YAHOO.namespace("newmind.webplatform.navigation");

YAHOO.newmind.webplatform.navigation.onMenuBarReady = function(e, args, strID, bSubMenuDisplay, intHideDelay, bLazyLoad) {
		// Ensure optional parameters have values
		// strID: Default = "topNavigation"
		if ((typeof(strID) === "undefined") || (strID === null)) {
			strID = "topNavigation";
		}
		// bSubMenuDisplay: Default = 750
		if ((typeof(bSubMenuDisplay) === "undefined") || (bSubMenuDisplay !== false)) {
			bSubMenuDisplay = true;
		}
		// intHideDelay: Default = true
		if ((typeof(intHideDelay) === "undefined") || (isNaN(parseInt(intHideDelay,2)))) {
			intHideDelay = 750;
		}
		// bLazyLoad: Default = false
		if ((typeof(bLazyLoad) === "undefined") || (bLazyLoad !== true)) {
			bLazyLoad = false;
		}
			
    // Animation object
    var oAnim;
	
	//the strID we have isn't actually for the div of the navigation, it's for it's parent, lets find the correct div
	
	var parentNode = document.getElementById(strID);
	strID = parentNode.firstChild.id.toString();
    
    // Utility function used to setup animation for submenus
    function setupMenuAnimation(p_oMenu) {
    
        if (!p_oMenu.animationSetup) {
            var aItems = p_oMenu.getItemGroups();
            if (aItems && aItems[0]) {
                var i = aItems[0].length - 1;
                var oSubmenu;
                do {
                    oSubmenu = p_oMenu.getItem(i).cfg.getProperty("submenu");
                    if (oSubmenu) {
                        oSubmenu.beforeShowEvent.subscribe(onMenuBeforeShow, oSubmenu, true);
                        oSubmenu.showEvent.subscribe(onMenuShow, oSubmenu, true);
                    }
                }
                while (i--);
            }
            p_oMenu.animationSetup = true;
        }
    }
    
    // "beforeshow" event handler for each submenu of the menu bar
    function onMenuBeforeShow(p_sType, p_sArgs, p_oMenu){
        if (oAnim && oAnim.isAnimated()) {
            oAnim.stop();
            oAnim = null;
        }
        YAHOO.util.Dom.setStyle(this.element, "overflow", "hidden");
        YAHOO.util.Dom.setStyle(this.body, "marginTop", ("-" + this.body.offsetHeight + "px"));
    }
    
    // "show" event handler for each submenu of the menu bar
    function onMenuShow(p_sType, p_sArgs, p_oMenu){
        oAnim = new YAHOO.util.Anim(this.body, {
            marginTop: {
                to: 0
            }
        }, 0.5, YAHOO.util.Easing.easeOut);
        oAnim.animate();
        var me = this;
        function onTween(){
            me.cfg.refireEvent("iframe");
        }
        
        function onAnimationComplete(){
            YAHOO.util.Dom.setStyle(me.body, "marginTop", ("0px"));
            YAHOO.util.Dom.setStyle(me.element, "overflow", "visible");
            setupMenuAnimation(me);
        }
        
        /*
         Refire the event handler for the "iframe"
         configuration property with each tween so that the
         size and position of the iframe shim remain in sync
         with the menu.
         */
        if (this.cfg.getProperty("iframe") === true) {
            oAnim.onTween.subscribe(onTween);
        }
        oAnim.onComplete.subscribe(onAnimationComplete);
    }
    
    // "render" event handler for the menu bar
    function onMenuRender(p_sType, p_sArgs, p_oMenu) {
        setupMenuAnimation(p_oMenu);
    }
    
    // Instantiate and render the menu bar
    var oMenuBar = new YAHOO.widget.MenuBar(strID,
    	{
        autosubmenudisplay: bSubMenuDisplay,
        hidedelay: intHideDelay,
        lazyload: bLazyLoad,
        showdelay: 0
    	});

    // Subscribe to the "render" event
    oMenuBar.renderEvent.subscribe(onMenuRender, oMenuBar, true);
    oMenuBar.render();
};

YAHOO.newmind.webplatform.navigation.onMenuBarReadyWithArgs = function(strId, bSubMenuDisplay, intHideDelay, bLazyLoad){
	// Prepare a function call with specified parameters.
	// This will return a function that can be passed to the onDOMReady event,
	// but will pass on all arguments to the onMenuBarReady method.
	return function(e, args)
	       {
	       	YAHOO.newmind.webplatform.navigation.onMenuBarReady(
	       	  e,
	       	  args,
	       		strId,
	       		bSubMenuDisplay,
	       		intHideDelay,
	       		bLazyLoad);
	       };
};

// Initialize and render the menu bar when it is available in the DOM
//YAHOO.util.Event.onDOMReady(YAHOO.newmind.webplatform.navigation.onMenuBarReady);
//YAHOO.util.Event.onDOMReady(YAHOO.newmind.webplatform.navigation.onMenuBarReadyWithArgs("NavId"));

