﻿var hideCurrentSection = new function() {
    this.navItemId = '';
    this.navSectionId = '';
    this.hideIt = false;
};

function showGlobalNavSection(navItemId, navSectionId) {
    if (hideCurrentSection.navItemId != navItemId) {
        if (hideCurrentSection.navItemId != '') {
            reallyHideGlobalNavSection();
        }
        hideCurrentSection.navItemId = navItemId;
        hideCurrentSection.navSectionId = navSectionId;
    }
    else {
        hideCurrentSection.hideIt = false;
    }

    if ($("#" + navItemId).hasClass("selected") == false) {
        $("#" + navItemId).addClass("selected");
    }
    $("#" + navSectionId).css({ 'display': 'block' });
}

function hideGlobalNavSection(navItemId, navSectionId) {
    hideCurrentSection.navItemId = navItemId;
    hideCurrentSection.navSectionId = navSectionId;
    hideCurrentSection.hideIt = true;
}

function checkMenus() {
    if (hideCurrentSection.hideIt == true) {
        reallyHideGlobalNavSection();
    }
}

function reallyHideGlobalNavSection() {
    if (hideCurrentSection.hideIt == true) {
        $("#" + hideCurrentSection.navItemId).removeClass("selected");
        $("#" + hideCurrentSection.navSectionId).css({ 'display': 'none' });
        hideCurrentSection.hideIt = false;
    }
}

function positionGlobalNavSections() {
    $(".globalNavSection").each(function(i) {
        var liId = "nav" + this.id.substring("globalNavSection".length);
        var li = $("#" + liId);
        var top = li.offset().top + li.outerHeight();
        var liOffsetLeft = li.offset().left;
        //var liOuterWidth = li.outerWidth();
        //var currentGlobalNavWidth = $(this).outerWidth();
        //var left = liOffsetLeft + liOuterWidth - currentGlobalNavWidth;
        var left = liOffsetLeft;
        left++; // not sure why we need this - matt
        $(this).css({ 'top': top + 'px', 'left': left + 'px' });
    });
}

function wireUpGlobalNav() {
    $("#nav > li > a").each(function(i) {
        var parentId = $(this).parent().attr('id');
        var sectionName = parentId.substring("nav".length);
        $(this).mouseover(function() { showGlobalNavSection(parentId, 'globalNavSection' + sectionName); });
        $(this).mouseout(function() { hideGlobalNavSection(parentId, 'globalNavSection' + sectionName); });
    });

    $(".globalNavSection").each(function(i) {
        var currentId = $(this).attr('id');
        var sectionName = currentId.substring("globalNavSection".length);
        $(this).mouseover(function() { showGlobalNavSection('nav' + sectionName, currentId); });
        $(this).mouseout(function() { hideGlobalNavSection('nav' + sectionName, currentId); });
    });
}

$(document).ready(function() {
    wireUpGlobalNav();
    //positionGlobalNavSections();
});

$(window).resize(function() {
    //positionGlobalNavSections();
});

window.setInterval("checkMenus()", 500);