/*
    Hide all menu items above the clicked menu item
    Show/hide those hidden items on "Full Menu" click event
*/

jQuery(function() {

    // when user clicks on the MainNav, the li does not contain the class "withChildren"
    // defaultPage --> loading menus for MainNav
    var defaultPage = jQuery("#subNav li.withChildren").length == 0;

    // determine if the Top Items currently displayed
    var isShowingTopItems = defaultPage;

    // first time when user clicks on a MainNav, don't hide any menus
    if (!defaultPage) {
        toggleMenuItemsAbove();
    }

    // Full Menu click 
    jQuery("#clickMe").click(function() {

        // if Top Items currently show then set to hide
        if (isShowingTopItems) {

            isShowingTopItems = false;
            toggleMenuItemsAbove();
            
        }
        else {
            isShowingTopItems = true;
            toggleMenuItemsAbove();
            
            // Hide this button if they expand the top Items
            jQuery(this).hide();
        }

    });

});


function toggleMenuItemsAbove() {
    var found = false;
    // element id = subNav, any li that has a class of withChildren
    jQuery.each(jQuery("#subNav li"), function() {

        // if "#subNav li" 
        if (jQuery(this).hasClass("withChildren")) {
            // this element has children
            found = true;
        }

        if (found) {
            return;
        }
        else {
            jQuery(this).toggle();
        }
    });

}

