//Function to add hover rules to the main site navigation
$(document).ready(
    function addNavigationListeners(){
        $(".listMainNavigationItem").hover(
                //hover over actions
                function(){
                    /* Calculate out the left positions */
                    var navOffset = $(this).offset();
                    var navLeft = navOffset.left;
                    var navWidth = $(this).width();
                    var navId = $(this).attr("id");
                    var subnavWidth = 0;
                    $(this).find(".listSubNavigationColumn149").each(function() {
                        subnavWidth = subnavWidth + 8 + 149;
                    });
                    $(this).find(".listSubNavigationColumn164").each(function() {
                        subnavWidth = subnavWidth + 8 + 164;
                    });
                    $(this).find(".listSubNavigationColumn174").each(function() {
                        subnavWidth = subnavWidth + 8 + 174;
                    });
                    $(this).find(".listSubNavigationColumn199").each(function() {
                        subnavWidth = subnavWidth + 8 + 199;
                    });
                    $(this).find(".listSubNavigationColumn254").each(function() {
                        subnavWidth = subnavWidth + 8 + 254;
                    });
                    $(this).find(".listSubNavigationColumn304").each(function() {
                        subnavWidth = subnavWidth + 8 + 304;
                    });
                    $(this).find(".listSubNavigationColumn329").each(function() {
                        subnavWidth = subnavWidth + 8 + 329;
                    });
                    subnavWidth = subnavWidth + 22;
                    if (navId == "navTraining" || navId == "navPressRoom" || navId == "navContacts" || navId == "navTrueBlue")
                    {
                        navLeft = navLeft + navWidth - subnavWidth;                     //Right Aligned Subnavigation will have their left positions set to the parent's right position minus the subnavigations width
                    }
                    else if (navId == "navResources")
                    {
                        navLeft = navLeft + navWidth - subnavWidth + 271;               //Centered Subnavigation will have their left positions set to the parents right position minus the subnavigations width, then removing both sides, all padding and margins and the padding of the text in the top image (ie 199 + 22 + 24 + 12 + 14)
                    }
                    if( jQuery.browser.msie && (jQuery.browser.version >= 8) )
                    {
                        subnavWidth = subnavWidth + 20;
                    }
                    var navFinalLeft = navLeft.toString() + "px";
                    /* Done calculating out the left position */
                    $(this).find(".navImage").hide();                                   //Hide the non-hover image
                    $(this).find(".navImageHover").show();                              //Show the hover image
                    $(this).find(".listSubNavigation").css({"left" : navFinalLeft});    //Place the Subnavigation at the correct position
                    $(this).find(".listSubNavigation").css({"width" : subnavWidth});    //Ensure that the width is correct
                    $(this).find(".listSubNavigation").show();                          //Show the sub navigation menu
                },
                //hover out actions
                function() {
                    $(this).find(".listSubNavigation").hide();                          //Hide the sub navigation menu
                    $(this).find(".navImageHover").hide();                              //Hide the hover image
                    $(this).find(".navImage").show();                                   //Show the non-hover image
                }
        );
    }
);