﻿
function openUrl(sUrl)
{
  window.location.href = sUrl;
}

// menu stuff
var Menu =
{
  IdleTimer: null, // idle timer
  IdleDuration: 2500, // how long till the menu reverts back to idle state
  StaticItemId: null, // this is the menu item to stay on all the time (unless another is active)
  ActiveItemId: null, // this is the current active item
  ItemCollection: null, // all the primary items

  Init: function()
  {
    this.ItemCollection = $('.pageHeader .menu>li');

    // set up each primary item
    for (var i = 0; i < this.ItemCollection.length; i++)
    {
      // the current primary item
      var item = $(this.ItemCollection[i]);

      // check if its the static item and assign if it is
      if (this.StaticItemId == null && item.hasClass('on'))
      {
        this.StaticItemId = Menu.GetItemId(item);
        this.ShowMenu(this.StaticItemId);
      }

      // set up the mouseover functions
      item.mouseover(function()
      {
        Menu.MenuActive(true);
        Menu.ShowMenu(Menu.GetItemId(this));
      });

      // set up the mouseover functions for the children to get them to set active when browsing around the menu
      item.find('li').mouseover(function()
      {
        Menu.MenuActive(true);
      });
    }
  },

  // Sets the state of the entire menu
  MenuActive: function(isActive)
  {
    // always clear the timer on state change/update
    if (this.IdleTimer)
      clearTimeout(this.IdleTimer);

    // create a new timer
    if (isActive)
      this.IdleTimer = setTimeout("Menu.MenuActive(false)", this.IdleDuration);
    else
      Menu.ResetMenu();
  },

  // Gets a menu item by id
  GetItemById: function(id)
  {
    return $('.pageHeader .menu li[rel=' + id + ']');
  },

  // Returns the id for an item
  GetItemId: function(item)
  {
    if (item.attr != null)
      return item.attr('rel');
    else
      return $(item).attr('rel');
  },

  ShowMenu: function(id)
  {
    var item = this.GetItemById(id);
    var menu = item.children('ul');
    var doShowMenu = false;

    // if we haven't hovered a menu item before
    if (this.ActiveItemId == null)
    {
      this.ActiveItemId = id;
      doShowMenu = (menu.length > 0);
    }

    // if we have but it isn't the same one
    else if (id != this.ActiveItemId)
    {
      // set active menu
      this.ActiveItemId = id;
      // hide the rest
      this.ResetMenu(id);
      // trigger the show of menu
      doShowMenu = true;
    }

    if (doShowMenu)
      menu.fadeIn(300);
  },

  // Hides a particular menu by id
  HideMenu: function(id)
  {
    this.GetItemById(id).children('ul').hide();
  },

  // Resets the menu to a default state (including the static menu)
  ResetMenu: function(leaveVisibleId)
  {
    var isStaticItemActive = this.StaticItemId != null && this.StaticItemId == this.ActiveItemId;

    // we determine here what to close
    var closeItems;
    if (leaveVisibleId != null)
    {
      // if we need to leave one visible
      closeItems = this.ItemCollection.not('[rel=' + leaveVisibleId + ']');
    }
    else if (isStaticItemActive)
    {
      // if we have a static menu item and its active, then don't close it - it should remain open
      closeItems = this.ItemCollection.not('[rel=' + this.StaticItemId + ']');
    }
    else
    {
      // hide them all and reset the active to null
      this.ActiveItemId = null;
      closeItems = this.ItemCollection;
    }

    // perform close on all items we need to
    for (var i = 0; i < closeItems.length; i++)
      this.HideMenu(Menu.GetItemId(closeItems[i]));

    // if we aren't ressting the menu for a particular menu item and were not showing the static one
    if (leaveVisibleId == null && !isStaticItemActive)
      this.ShowMenu(this.StaticItemId);
  }
}

$(document).ready(function()
{
  Menu.Init();
});