YAHOO.util.Event.onContentReady("njgmenu", function () {

    var oMenuBar = new YAHOO.widget.MenuBar("njgmenu", { 
                                                autosubmenudisplay: true, 
                                                hidedelay: 750, 
                                                lazyload: true });

    var aSubmenuData = [
    
        {
            id: "mRegions", 
            itemdata: [ 
                { text: "Skylands", url: "/regions/skylands/nj-skylands-region.jsp" },
                { text: "Gateway", url: "/regions/gateway/nj-gateway-region.html" },
                { text: "Delaware River", url: "/regions/delaware-river/nj-delaware-river-region.html" },
                { text: "Shore", url: "/regions/shore/nj-shore-region.html" },
                { text: "Atlantic City", url: "/regions/atlantic-city/nj-atlantic-city-region.html" },
                { text: "Southern Shore", url: "/regions/southern-shore/nj-southern-shore-region.html" }
            ]
        },
        
        {
            id: "mAttractions", 
            itemdata: [
                { text: "Amusements", url: "/amusements.jsp" },
                { text: "Gardens", url: "/gardens.jsp" },
                { text: "Museums", url: "/museums.jsp" },
                { text: "Parks", url: "/parks.jsp" },
                { text: "Search", url: "/index.jsp" }
            ] 
        },
        {
            id: "mLodging", 
            itemdata: [
                { text: "Hotels", url: "/lodging/lodging.jsp" },
                { text: "Inns", url: "/lodging/inns.jsp" },
                { text: "Resorts", url: "/lodging/lodging.jsp" },
                { text: "Bed & Breakfast", url: "/lodging/lodging.jsp" }
            ] 
        },
        {
            id: "mSpecials", 
            itemdata: [
                { text: "Coupons", url: "/specials/coupons.html" },
                { text: "Discounts", url: "/specials/discounts.html" },
                { text: "Tips", url: "/specials/tips.html" },
                { text: "Ideas", url: "/specials/ideas.html" }
            ] 
        }
    ];


    var ua = YAHOO.env.ua, oAnim;  // Animation instance

    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)) {

            // Clear widths set by the "beforeshow" event handler
            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];
            
            /* Animate the <ul> element's "marginTop" style property to a value of 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);
        }
    }


    oMenuBar.subscribe("beforeRender", function () {
		var nSubmenus = aSubmenuData.length,i, j;
        if (this.getRoot() == this) {
			for (i = 2, j=0; j < nSubmenus; i++, j++) {
            	this.getItem(i).cfg.setProperty("submenu", aSubmenuData[j]);
			}
        }
    });


    oMenuBar.subscribe("beforeShow", onSubmenuBeforeShow);
    oMenuBar.subscribe("show", onSubmenuShow);
    oMenuBar.render();         

});

