var _CurrentlySelectedNavAnchor = null;

//
// Initialize the navigation. Called on document.ready.
//
function InitializeNavigation() {

  _CurrentlySelectedNavAnchor = $("div#mainNav a.active");

  //Attach hover events to all top level navigation items
  $("li.mainNavItem").each(function (index) {
    var navItem = $(this);
    $(this).hover(function (e) { ShowNavItem(navItem) }, function (e) { HideNavItem(navItem) })
  });

  $("li.mainNavItem td").each(function (index) {
    $(this).hover(function (e) { $(this).css("background-color", "#ffffff"); }, function (e) { $(this).css("background-color", "#EEEFE1"); })
  });
}

function ShowNavItem(navItem) {
  navItem.find("a:first-child").addClass("active");
  navItem.addClass("hover");
}

function HideNavItem(navItem) {
  if (!IsCurrentlyActiveNav(navItem) || (_CurrentlySelectedNavAnchor != null && _CurrentlySelectedNavAnchor.legnth == 0)) {
    navItem.find("a:first-child").removeClass("active");
  }
  navItem.removeClass("hover");
}

//returns true if navi Item is the current page being viewed
function IsCurrentlyActiveNav(navItem){
  return (_CurrentlySelectedNavAnchor != null && _CurrentlySelectedNavAnchor.length == 1) && (_CurrentlySelectedNavAnchor.attr("href") == navItem.find("a:first-child").attr("href"));
}
