﻿YAHOO.util.Event.onContentReady("navigation-menu", function() {

    var centerMenu = function() {
        // Find the width of the menu bar's container
        var nav = YAHOO.util.Dom.get("navigation");
        // set the padding to 0 so the document will reflow before we calculate the new padding
        nav.style.paddingLeft = 0;
        
        var navWidth = nav.offsetWidth;
                
        // Find the width of all the menubaritems
        var menuBarItems = YAHOO.util.Dom.getElementsByClassName("yuimenubaritem", "li", nav);
        var menuBarWidth = 0;
        var Y = YAHOO.util.Dom.getY(menuBarItems[0]);
        YAHOO.util.Dom.batch(menuBarItems, function(el) {
            // If this menuitem is on a new line, stop
            var thisY = YAHOO.util.Dom.getY(el);
            if (thisY != Y)
                return false;
            menuBarWidth += el.offsetWidth;
        });
        
        // apply padding to the left of the navigation div to center the menu
        var padding = (navWidth - menuBarWidth) / 2;
        nav.style.paddingLeft = padding + "px";
    };
    
    // Show the menu
    var oMenu = new YAHOO.widget.MenuBar("navigation-menu",
    {
        autosubmenudisplay: true,
        showdelay: 50
    });
    oMenu.render();
    oMenu.show();
    
    centerMenu();
    YAHOO.util.Event.addListener(window, "resize", centerMenu);
    
});
