/**
 * Secondary Navigation Module
 */
(function($) {
    /**
    * Initializes secondary navigation modules.
    * Hides sub navigation elements that aren't relevant to the currently visited page.
    */
    Intel.mod.secondaryNavigation = function() {
        var self = this,
		secNavHeadings = $('.mod-secondary-navigation > a').not('.mod-secondary-navigation-inner a'),
		highlightFound = false;
        /**
        * Function to match a.href with the current location
        */
        this.linksMatch = function(currentHref) {
            var locMatch = new RegExp(currentHref.replace(/\?[\w\W]+/, '').replace(/\//g, '\\\/') + '$','i'),
			locString = location.protocol + '//' + location.host + '/' + location.pathname,
			locDefault = location.protocol + '//' + location.host + '/' + location.pathname + 'index.htm';
            
            if (locString.search(locMatch) !== -1 || locDefault.search(locMatch) !== -1) {
                return true;
            } else {
                return false;
            }
        };
        /**
        * Find all Level 1 links
        */
        secNavHeadings.each(function() {
            if (highlightFound) {
                $(this).css('border-top', 'none');
                highlightFound = false;
            }
            var hideLevel2Links = true,
            /**
            * Find all Level 2 links for the current Level 1 link
            */
			level2Links = $(this).next().find('a').not('li > a');
            level2Links.each(function() {
                var hideLevel3Links = true,
                /**
                * Find all Level 3 links for the current Level 2 link
                */
				level3Links = $(this).next().find('li > a').not('li > ul > li > a');
                level3Links.each(function() {
                    if (self.linksMatch($(this).attr('href'))) {
                        $(this).addClass('secondary-nav-focus');
                        $(this).addClass('secondary-nav-current');
                        $(this).bind('click', function(event) {
                            event.preventDefault();
                        });
                        hideLevel3Links = false;
                        highlightFound = true;
                    }
                });
                if (self.linksMatch($(this).attr('href')) || !hideLevel3Links) {
                    if (self.linksMatch($(this).attr('href'))) {
                        $(this).addClass('secondary-nav-current');
                        $(this).bind('click', function(event) {
                            event.preventDefault();
                        });
                    }
                    $(this).addClass('secondary-nav-focus');
                    hideLevel3Links = false;
                    hideLevel2Links = false;
                    highlightFound = true;
                }
                if (hideLevel3Links) {
                    if ($(this).next().is('ul')) {
                        $(this).next().hide();
                    }
                }
            });
            if (self.linksMatch($(this).attr('href')) || !hideLevel2Links) {
                if (self.linksMatch($(this).attr('href'))) {
                    $(this).addClass('secondary-nav-current');
                    $(this).bind('click', function(event) {
                        event.preventDefault();
                    });
                }
                $(this).addClass('secondary-nav-focus');
                highlightFound = true;
                hideLevel2Links = false;
            }
            if (hideLevel2Links) {
                if ($(this).next().is('div.mod-secondary-navigation-inner')) {
                    $(this).next().hide();
                }
                if ($(this).next().next().is('span.mod-secondary-navigation-inner-bottom')) {
                    $(this).next().next().hide();
                }
            }
        });
        $('.mod-secondary-navigation > a:first').not('.mod-secondary-navigation-inner a').css('border-top', 'none');
        $('.mod-secondary-navigation > a:last').not('.mod-secondary-navigation-inner a, a.secondary-nav-focus').css('padding-bottom', '0');
        $('.mod-secondary-navigation > a:last').not('.mod-secondary-navigation-inner a').css('border-bottom', 'none');
        $('.mod-secondary-navigation').css({ 'visibility': 'visible', 'background-repeat': 'no-repeat' });
    };
} (jQuery));
/**
 * Append to Global JS Initializations using jQuery
 */
jQuery(document).ready(function () {
	Intel.mod.secondaryNavigation();
});

