var isIESix = 0;
var navJQ;

jQuery.fn.initNav = function() {
    return this.each(initMTNav);
}

jQuery.resetNav = function() {
    $(navJQ).log().find("ul").fadeOut('fast').end().find("> li.hovered").removeClass("hovered");
    return;
};

jQuery.fn.showNav = function() {
    return this.each(function() {
        // Reset siblings
        $.resetNav();
        // Set me
        $(this).addClass("hovered").find("> ul").slideDown();        
    });
};
jQuery.fn.log = function() {
    return this.each(function() {
        // console.log(this);
    });
};

function initMTNav() {

    // console.log('I am in the init function with '+this);

    // Cache navbar jQuery object
    navJQ = this;

    // Test for IE6.  Set global var and fix sublists
    if ($.browser.msie) { 
        if ( 6 == parseInt(jQuery.browser.version) ) {
            isIESix = 1;
            fixIENavSubListWidths(this);
        }
    }
    initMTNavHandlers();
    initActiveNav();
    return this;
}

function byHrefLength(a1, a2){
    var navhref1 = $("> a", a1).attr('href');
    var navhref2 = $("> a", a2).attr('href');
    
    if(navhref1.length < navhref2.length)
        return 1;
    if(navhref1.length > navhref2.length)
        return -1;
    if(navhref1.length == navhref2.length)
        return 0;
}

function initActiveNav() {
    var matchedHome = false;
    var activeItems = 0;
    var navItems = $("li", navJQ);
    var needActive = $(navItems).is('.active') ? 0 : 1;
    // console.log('Existing active states: '+needActive);

    // Highlight the active tab by comparing URLs
    // to the href attribute value of each nav link
    if ( needActive ) {

        navItems = navItems.sort(byHrefLength);
        $(navItems).each(function() {
            var navhref = $("> a", this).attr('href');
            var url = window.location.href;


            // console.log('Comparing '+navhref+' and '+url);
            navhref = navhref.replace( /([^A-Za-z0-9])/g , "\\$1" );
            if (url.match("^"+navhref)) {
                // console.log('LAST COMPARISON MATCHED');
                if ( $(this).hasClass('nav-top') ) {
                    if ($(this).attr('id') == "nav-home") matchedHome = true;
                    $(this).addClass('active');
                    activeItems = activeItems + 1;
                    needActive = 0;
                    return false;                    
                }
                else {
                    $(this).parents().each(function() {
                        if ($(this).hasClass('nav-top')) {
                            $(this).addClass('active');
                            activeItems = activeItems + 1;
                            needActive = 0;
                            return false;                    
                        }
                    });
                }
            }
        });
        if ( needActive ) $("#nav-archives").addClass('active');
        if ( activeItems > 1 && matchedHome === true)
            $("#nav-home").removeClass('active');
    }
}

function initMTNavHandlers() {

    // console.log('Initializing handlers with '+navJQ);

    // Hover event handler for top level nav buttons
    $("> li", navJQ).hover(
        function() {
            // Reset siblings
            $(this).showNav();
        },
        function() {
            if ( ! isIESix ) {
                $.resetNav();
            }
        }
    );

    // Hey look! Another way!
    $("#alpha, #beta, #gamma, #header").click(function () {
            $.resetNav();
        },
        function () {}
    );        
}

function fixIENavSubListWidths(root) {
    // Find the sublists in the nav and
    // even up all of their widths
    $("ul ul", root).show().each(function() {
        var longest = 0;
        $("a", root).each(function() {
            if ( $(root).width() > longest ) longest = $(root).width();
        })
        .width(longest)
    }).hide("fast");        
}

