(function ($) { // encapsulated $
    $(function () { // Dom Ready Shortcut

        var config = {
            sensitivity: 333, // number = sensitivity threshold (must be 1 or higher)    
            interval: 200,  // number = milliseconds for onMouseOver polling interval    
            over: doOpen,   // function = onMouseOver callback (REQUIRED)    
            timeout: 1,   // number = milliseconds delay before onMouseOut    
            out: doClose    // function = onMouseOut callback (REQUIRED)    
        };

        function doOpen() {

            $(this).find("a").addClass("active");
            $('div.sub', this).show();
        }

        function doClose() {
            if ($(this).find('div.sub').hasClass('active') == false) {
                $(this).find("a").removeClass("active");
                $('div.sub', this).hide();
            }
        }

        $("ul#subnavigation li.sub").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
        $("ul#subnavigation li").hoverIntent(config);

        $("ul#subnavigation .current").parents("div.sub").addClass("active").show();
        $("ul#subnavigation .current").parents("div.head").next('.sub').addClass("active").show();
        //$("ul#subnavigation div.current").next('.sub').addClass("active").show();
        
        // SET THE SELECTED PAGE COLOR ON THE SUB NAV
        jQuery('#subnavigation').find('li.current a:first span').css({ "color": "#519DD1" });
        jQuery('#subnavigation').find('a.headLink').hasClass('current').find('span').css({ "color": "#519DD1" });

        // THIS SECTION CONTROLS THE TABBED BODY LAYOUT
        var jQueryLiCount = jQuery('#tabbedLayoutContainer ul li').length;
        var jQueryLiWidth = Number((663 - jQueryLiCount) / jQueryLiCount).toFixed(0);
        var jQueryDifferenceMaker = ((jQueryLiWidth * jQueryLiCount) - (663 - jQueryLiCount));

        jQuery('#tabbedLayoutContainer ul li').css({ "width": (jQueryLiWidth) + "px" });

        if (jQueryDifferenceMaker >= 0) {
            jQuery('#tabbedLayoutContainer ul li').last().css({ "width": (jQueryLiWidth - jQueryDifferenceMaker) + "px" });
        }
        else {
            jQuery('#tabbedLayoutContainer ul li').last().css({ "width": (jQueryLiWidth + jQueryDifferenceMaker) + "px" });
        }

        jQuery('#tabbedLayoutContainer ul li.active').prev().css({ "border-right": "none" });
        jQuery('#tabbedLayoutContainer ul li.active').next().css({ "border-left": "none" });

    });
})(jQuery);                              // Passing it as the param 
