$(document).ready(
  function ()
  {
    $('#MoveAreaMap1').mousedown(
      function (event)
      {
        if (event.button == 1)
        {
          call('#header:move');
        }
      }
    );
    $('[tabindex]').each(
      function ()
      {
        $(this).keydown(
          function(event)
          {
            if (event.keyCode == 13) // on return 
            {
              $(this).click();
            }
          }
        );
      }
    );
  }
);

function call(string)
{
  /* Skip some calls on debug */
  if (debug)
  {
    if (
      (string == '#onload')
    )
    {
      return;
    }
    if (string.substr(0, 9) == '#tooltip:')
    {
      $('#debug').html(string);
      return;
    }
  }
  alert(string);
}

function releaseLock(id)
{
  if (id == 'subnav-help')
  {
    id = 'help';
  }
  if (id == 'subnav-settings')
  {
    id = 'settings';
  }
  $.LayerButton.hideLayer(document.getElementById(id), 'selected');
}

function addHoverPressedEvents(elements)
{
  elements.mousedown(
    function (e)
    {
      if (e.button == 1)
      {
        $(this).addClass('locked');
        $(this).find('.hover').stop(false, true).show();
        $(this).find('.pressed').stop(false, true).show();
      }
    }
  );

  elements.mouseenter(
    function ()
    {
      $(this).addClass('active');
      $(this).find('.hover').stop(false, true).show();
      if (!$(this).hasClass('locked'))
      {
        $(this).find('.pressed').stop(false, true).hide();
      }
    }
  );

  elements.mouseleave(
    function ()
    {
      $(this).removeClass('active');
      $(this).find('.hover').stop(false, true).hide();
      if (!$(this).hasClass('locked'))
      {
        $(this).find('.pressed').stop(false, true).hide();
      }
    }
  );
}

function slideLeft()
{
  $('#maincontent').animate({left: '-1000px'}, 750);
}

function activateTooltip(selector)
{
  $(selector).mouseenter(
    function ()
    {
      if (getTooltipId($(this)))
      {
        // Don't show tooltips if we are just launching a module
        if ($('#content').is('.launching')) return;
        
        call('#tooltip:1:' + getTooltipId($(this)));
      }
    }
  );
  $(selector).mouseleave(
    function ()
    {
      if (getTooltipId($(this)))
      {
        call('#tooltip:0:' + getTooltipId($(this)));
      }
    }
  );
}

function getTooltipId(element)
{
  if (element.attr('onclick'))
  {
    var onclick = String(element.attr('onclick'));
    var matches = onclick.match(/call\((.*)\)/);
    if (matches)
    {
      // strip quotes
      matches[1] = matches[1].substr(1, (matches[1].length - 2));
      // stip hash
      var tooltipId = matches[1].substr(1, (matches[1].length - 1));
      if (tooltipId.substr(0, 7) == 'launch:')
      {
         tooltipId = tooltipId.substr(7, (tooltipId.length - 7));
      }
      return tooltipId.replace(/:/g, '-');
    }
    else if (onclick.match(/turbomodeMap/))
    {
      return 'highspeed-' + (turbomodeStatus ? '0' : '1');
    }
    else if (onclick.match(/pillar2/))
    {
      return getTooltipId($('#pillar2 .pillarlinks a:first'));
    }
    else if (onclick.match(/pillar3/))
    {
      return getTooltipId($('#pillar3 .pillarlinks a:first'));
    }
    else if (onclick.match(/tab\-3/))
    {
      return 'gaindiskspace';
    }
  }
  else if (element.hasClass('tuindex'))
  {
    var id = element.attr('id');
    if (id.substr(0, 6) == 'helper')
    {
      id = id.substr(6);
    }
    return id;
  }
}