﻿var hoveredSubNav = null;

var animateBlockSpeed = 80;    // how fast the block moves under the nav items
var fadeSpeed = 200;            // how fast sub navs fade in and out
var hoverIntentWait = 300;      // how long the pause is between sub nav changes

var hoverConfig = { over: rootNavHoverOver, timeout: hoverIntentWait, out: rootNavHoverOut };
var hoverConfig2 = { over: navHoverOver, timeout: hoverIntentWait, out: navHoverOut };

var activeNav;

$(function() {
$("#menuSlider").hide();
    
    $("#topNav > li").hoverIntent(hoverConfig);
    $("#topNav").hoverIntent(hoverConfig2);
    
    var currentUrl = cleanUrl(window.location.toString());
    
    /*
    // find top level nav items
    $("#topNav > li > a").each(function(i) {
        var linkDestination = cleanUrl($(this).attr("href"));
        var trimmedCurrentUrl = currentUrl.substring(currentUrl.length - linkDestination.length, currentUrl.length);

        if (i > 0) {
            if (trimmedCurrentUrl == linkDestination) {
                activeNav = $(this).parent().find('ul');

                $("#topNav > li .subNav").hide();
                activeNav.show();

                var position = $(this).parent().position();

                $('#menuSlider').css('width', ($(this).width())).css('left', position.left);
                $('#menuSlider').show();
            }
        }
        else if (trimmedCurrentUrl == '/default.aspx') {
            position = $(this).parent().position();

            $('#menuSlider').css('width', ($(this).width())).css('left', position.left);
            $('#menuSlider').show();
        }
    });
    */

    // find lower leven nav items
    $("#topNav > li > ul > li > a").each(function() {
        var linkDestination = cleanUrl($(this).attr("href"));
        var trimmedCurrentUrl = currentUrl.substring(currentUrl.length - linkDestination.length, currentUrl.length);

        if (trimmedCurrentUrl == linkDestination) {
            activeNav = $(this).parent().parent();
            activeNav.show();
            $(this).addClass("subNavSelected");

            // show slider under parent li
            var topLevelLi = activeNav.parent();
            var position = topLevelLi.position();

            //$('#menuSlider').css('width', (topLevelLi.width())).css('left', position.left);
            //$('#menuSlider').show();
        }
    });
});

function cleanUrl(url) {
    if (url.indexOf('#') > -1) {
        url = url.substring(0, url.indexOf('#'));
    }
    if (url.indexOf('?') > -1) {
        url = url.substring(0, url.indexOf('?'));
    }
    if (url.indexOf('?') > -1) {
        url = url.substring(0, url.indexOf('?'));
    }
    
    if (url.substring(url.length - 1, url.length) == '/') {
        url = url + 'default.aspx';
    }

    return url;
}

function rootNavHoverOver() {   
    $("#topNav > li > ul").hide();
    $(this).find('ul').show();
    //$(this).addClass("temp-active");
    
    /*
    hoveredSubNav = $(this).find(".subNav");
    var position = $(this).position();
    $('#menuSlider').animate({ width: ($(this).width()), left: position.left }, animateBlockSpeed);
    $("#topNav > li .subNav").hide();
    hoveredSubNav.show(); */
}

function navHoverOver() {
    
}

function navHoverOut() {
    $("#topNav > li > ul").hide();
    $("#.active ul").show();
}

function rootNavHoverOut() {
    //$("#topNav > li").removeClass("temp-active");
}
