var active_item = null;
var active_subitem = null;
var menuTimeout = 2000;
var menuTimer   = null;
var subTimer    = null;
var IE          = document.all ? true : false;
var mxy         = [0,0];

//
//

document.onmousemove = getMouseXY;

//
//

function OpenMenu(id)
{
  clearInterval(menuTimer);
  menuTimer = null;

  if (active_item != null)
    active_item.style.visibility = 'hidden';

  el = document.getElementById(id);
  if (el != null)
  {
    active_item = el;
    el.style.visibility = 'visible';
  }
}

function OpenSubMenu(id)
{
  clearInterval(menuTimer);
  menuTimer = null;

  if (active_subitem != null)
    active_subitem.style.visibility = 'hidden';

  el = document.getElementById(id);
  if (el != null)
  {
    active_subitem = el;
    el.style.visibility = 'visible';
  }
}

function CloseMenu()
{
  menuTimer = setInterval('CloseByTimeout()', menuTimeout);
}

function CloseByTimeout()
{
  if (active_item != null)
  {
    active_item.style.visibility = 'hidden';
  }
  if (active_subitem != null)
  {
    active_subitem.style.visibility = 'hidden';
  }
}


function getScrollXY()
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' )
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  }
  else if (document.body && ( document.body.scrollLeft || document.body.scrollTop))
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  }
  else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop))
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

  return [scrOfX, scrOfY];
}

function getMouseXY(e)
{
  tempX = 0;
  tempY = 0;

  if (IE)
  { // grab the x-y pos.s if browser is IE
    try
    {
      tempX = event.clientX + document.body.scrollLeft;
      tempY = event.clientY + document.body.scrollTop;
    }
    catch (e)
    {}
  }
  else
  {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX - window.pageXOffset;
    tempY = e.pageY - window.pageYOffset;
  }  

  // catch possible negative values in NS4
  if (tempX < 0)
    tempX = 0;
  if (tempY < 0)
    tempY = 0;

  mxy = [tempX, tempY];
}

function checkEmail(v)
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(v)) return true;

  return false;
}

function validateEmail()
{
  var status = checkEmail($('input[name=user]').val());
  if (!status)
  {
    $('.status').addClass('error').html('Sjekk at e-postadressen er skrevet riktig').fadeIn('slow'); 
  }

  return status;
}

$('document').ready(function()
{
  $('input[name=user]').focus(function()
  {
    var v = $(this).val(); 
    if (v == 'E-postadresse') $(this).val('');
    $('.status').fadeOut('slow', function(){$('.status').removeClass('error')})
  });

  if ($('.status').html()) $('.status').show();
});


