﻿/* Website: www.metaswitch.com */
/* Script: metaswitch.js */
/* Purpose: Common functions */

function navigationUpdates()
{ 
  var strLang;
  
  // Kick off an asynchronous AJAX request to load the full menu hierarchy into the page
  jQuery(".footer").after('<div id="full-menu"></div>');
  
  if (location.href.indexOf('/es/') >= 0)
  {
    // Spanish site
    strLang = 'es';
  }
  else
  {
    // English site
    strLang = 'en';
  }
  jQuery("#full-menu").load('/master/full-site-map-menu.aspx?lang=' + strLang, '', getMenuSucceeded);
} 

// Add an onclick handler to any navigation LIs so that if the user
// clicks anywhere on the LI, not just on the hyperlink itself,
// the hyperlink is followed.
// Avoid doing this if hyperlink has a target, as it's hard to simulate clicking that link
function setupNavigationClickHandler()
{
  jQuery(".topNav li, .leftNav li").click(function(event) 
  {
    if (jQuery(event.target).is('li'))
    {
      // User clicked on the li outside the hyperlink
      var jqLinks = jQuery(this).find('> a');
      if (jqLinks.length == 1)
      {
        // This LI contains exactly one immediate child hyperlink
        var hypLink = jqLinks.get(0);
        if ((hypLink.target == '') && (hypLink.href != ''))
        {
          // This hyperlink doesn't have a target, so we can simulate navigation
          //jqLink.eq(0).click();
          location.href = hypLink.href;
        }
      }
    }
    
    // Don't bubble up to parent LIs
    event.stopPropagation();
  });

}

// We have successfully loaded the full site-map menu into a hidden DIV on the page
// Use this to dynamically create popup menus attached to the static menus  
function getMenuSucceeded(data, textStatus)
{

  // Iterate through all LI nodes in left navigation menu, and add
  // popup menus for children of level 4 nodes where appropriate  
  jQuery(".leftNav li li li").each(function() 
  {
    loadMenuChildren(jQuery(this));
  });

  jQuery(".leftNav li").each(function() 
  {
    highlightRollovers(jQuery(this));
  });

  // Add popup navigation to top navigation
  jQuery(".topNav li").each(function() 
  {
    loadMenuChildren(jQuery(this));
  });

  jQuery(".topNav li").each(function() 
  {
    highlightRollovers(jQuery(this));
  });
  
  if (jQuery.browser.msie)
  {
    // Work around Internet Explorer problems handling translucent background PNGs
    // when using jQuery animations
    jQuery('ul.popup').css('background-image', '').css('background-color', '#003D7E');
  }
  
  setupNavigationClickHandler();
}

// Called for a given node on the top or left navigation menu
// If the node currently has no children, but the full menu version does
// have children, copy the children across as a popup menu.
function loadMenuChildren(jqMenuNodeLi)
{
  var strNodePath;
  var iNumChildren;
  var jqChildrenUl;
  
  strNodePath = jqMenuNodeLi.attr('npath');
  iNumChildren = jqMenuNodeLi.children('ul').children('li').length;
  if ((strNodePath != '') && (iNumChildren == 0))
  {
    jqChildrenUl = findMenuNodeChildren(strNodePath);
    if (jqChildrenUl.length > 0)
    {
      jqChildrenUl = jqChildrenUl.clone();
      
      // Prune tree so we don't have more than 2 levels of popup
      jqChildrenUl.find('ul ul').remove();
      
      jqChildrenUl.addClass('popup');
      jqChildrenUl.find('ul').addClass('popup');
      jqMenuNodeLi.append(jqChildrenUl);
    }
  }
}

// Called for a given node on the top or left navigation menu
// If the node has a hidden popup menu below it, add CSS class to indicate this
function highlightRollovers(jqMenuNodeLi)
{
  var jqPopupChildUl;

  jqPopupChildUl = jqMenuNodeLi.children('ul.popup');
  if (jqPopupChildUl.length > 0)
  {
    // This is a menu LI that has a popup submenu UL below it
    jqMenuNodeLi.addClass('hasPopup');
    
    jqMenuNodeLi.hover(function() 
    {
      menuHover(jqMenuNodeLi, jqPopupChildUl);
    }, function() 
    {
      menuUnhover(jqMenuNodeLi, jqPopupChildUl);
    });
  }
}

// User has moused over a menu item that has a popup submenu
function menuHover(jqMenuNodeLi, jqPopupChildUl)
{
  jqPopupChildUl.css('display', '');
  jqPopupChildUl.css('visibility', 'visible');
  jqPopupChildUl.show();
}

// User has moused away from a menu item that has a popup submenu
function menuUnhover(jqMenuNodeLi, jqPopupChildUl)
{
  jqPopupChildUl.hide();
}

// Search for menu node in full site map, and return a jQuery object for the 
// UL containing its child nodes.
// Always returns a jQuery object, whose length will be zero if
// - full menu hasn't been loaded yet
// - node not found
// - node has no children.
function findMenuNodeChildren(strNodePath)
{
  var jqNode;
  
  jqNode = jQuery('#full-menu li[npath=' + strNodePath + '] > ul');
  
  return jqNode;
}

