//v.12022011KB
jQuery(document).ready(function ($) {

    // MEGA MENU
    var focused = document.activeElement,
        $topnav = $("#topnav"),
        topnav_anchors = "#topnav > li > h3 > a, #topnav > li > span > a",
        $topnav_anchors = $(topnav_anchors); //target top tab and 'more' anchors

    var megaDDShow = function () {

        var $tab = $(this),
            $tab_sub = $tab.find(".sub:first"),
            tab_pos = $tab.offset(),
            topnav_pos = $("#topnav").offset();

        $topnav.addClass("bborder");
        $tab.addClass("active");

        if ($tab[0].id === "moretab") {
            var $more_tab = $("li#moretab"),
                $more_sub = $more_tab.find(".sub:first");
            $more_tab.find(".sub").css({
                "left": $more_tab.width() - $more_sub.width() - 3,
                "top": $topnav.height()
            });
        } else {
            // reposition dd    
            $tab_sub.css({
                "left": topnav_pos.left - tab_pos.left,
                "top": $topnav.height()
            });
        }

        if ($tab_sub.not(":visible")) {
            var $tab_siblings = $tab.siblings();
            $tab_sub
                .stop()
                .fadeTo("fast", 1, function () {
                    $(this).show(); //show dd
                })
                .find("a:first")
                .focus(); //focus first anchor
            $tab_siblings
                .find(".sub:first")
                .stop()
                .fadeTo("slow", 0, function () {
                    $(this).hide();
                })
                .end()
                .removeClass("active"); //hide other dd's
        }
    };

    var megaDDHide = function () {
        //reset tab focus
        var $tab = $(this);
        $topnav.removeClass("bborder");
        $tab
            .find(".sub")
            .stop()
            .fadeTo("slow", 0, function () {
                $(this).hide();
            })
            .end()
            .removeClass("active");
    };

    var config = {
        sensitivity: 2,
        interval: 200,
        timeout: 500,
        over: megaDDShow,
        out: megaDDHide
    };

    $("ul#topnav > li").hoverIntent(config);

    var megaDDVisible = function () {
        return $("#topnav .sub").is(":visible");
    };

    var megaDDHideAll = function () {
        $visible_sub = $topnav.find(".sub:visible").first();
        $visible_sub.each(function () {
            var $t = $(this);
            $topnav.removeClass("bborder");
            $t.fadeTo("slow", 0, function () {
                $t.hide();
            });
        });
    };

    var tabsInactive = function () {
        $topnav.each(function () {
            $(this).find("> li").removeClass("active");
        });
    };

    $(document).delegate("form,a", "focus", function (e) {
        if (!$(this).parents("#topnav").length) {
            // handles tabbing out of primary nav
            tabsInactive();
            if (megaDDVisible()) {
                megaDDHideAll();
            }
        }
    });


    // KEYBOARD NAV
    $topnav.delegate(topnav_anchors, "focus blur keydown", function (e) { //primary nav
        var $t = $(this),
            $menu = $t.closest(".menu");
        if (e.type === "focusin") {
            tabsInactive();
            // megaDDShow.call($menu); //toggles the menu to show automattically
            $menu.addClass("active");
        }
        if (e.type === "keydown" && e.keyCode === 40) { //down arrow is pressed left for keyboard-only users
            // e.preventDefault(); IE + JAWS Issue (wreaks havoc)
            megaDDShow.call($menu);
        }
    }).delegate(".kb_expando a", "focus click", function (e) { //expando
        var $t = $(this),
            $menu = $t.closest(".menu");
        if (e.type === "focusin") {
            tabsInactive();
            $menu.addClass("active");
            if (megaDDVisible()) {
                megaDDHideAll();
            }
        }
        if (e.type === "click") { //enterdown triggers "click" event, screen reader necessary
            e.preventDefault();
            megaDDShow.call($menu);
        }
    }).delegate(".sub a", "keydown", function (e) { //megadd subnav
        var $t = $(this),
            focused = document.activeElement;
        switch (e.keyCode) {
        case 38: //up arrow
        case 27: //esc
            if (megaDDVisible()) {
                var $ddVisible = $t.parents(".menu:first"),
                    $activeTab = $ddVisible.find("a.tab:first");
                $activeTab.focus();
                megaDDHide.call($ddVisible);
                e.preventDefault();
            }
            break;
        }
    });
    // MEGA MENU END

}); // noconflict end
