YAHOO.namespace('signin.anim');

var signInLink = document.getElementById('signInLink');
var signInElem = document.getElementById('signIn');
var signInContents = document.getElementById('signInContents');

var attributesShowSignIn = {
  width: { to: 250 },
  height: { to: 87 }
};

var attributesHideSignIn = {
  width: { to: 250 },
  height: { to: 0 }
};

var animSignIn = new YAHOO.util.Anim(signInElem, attributesShowSignIn, .08);
var deanimSignIn = new YAHOO.util.Anim(signInElem, attributesHideSignIn, .08 );

// Submits the form when enter is pressed while in a form field, kind of a hack
// but its the only way to simulate submits for forms that are actually javascript funciton calls
function submitOnEnter(keyboardEvent)
{
  var keyPressed;
  if (keyboardEvent)
  {
    if (keyboardEvent.which)
    {
      // Get the key pressed for a non-ie browser
      keyPressed = keyboardEvent.which;
    }
    else
    {
      // Get the key pressed for an ie browser
      keyPressed = keyboardEvent.keyCode;
    }
  }

  // Submit the form if 'Enter' was pressed
  if (keyPressed == 13)
  {
    var pass = document.getElementById("popoutPassword");
    if (pass.value != '')
    {
      submitLayoutLoginForm();
    }
  }
}

function showSignInContents() {
  YAHOO.util.Dom.setStyle(signInContents, 'visibility','visible');
  YAHOO.util.Dom.setStyle(signInContents, 'display','block');
  positionSignInContents();

  YAHOO.util.Event.removeListener(signInLink, 'click', showSignIn);
  YAHOO.util.Event.addListener(signInLink, 'click', hideSignIn);

  // Put the cursor into the email field, we use setTimeout
  // as a hack for Firefox, if we don't then the browser windows
  // scrolls down a bit
  setTimeout("focusOnEmail()", 50);
}

function positionSignInContents() {
  var headerRegion = YAHOO.util.Dom.getRegion('header');

  YAHOO.util.Dom.setX(signInContents, headerRegion.left + 295);
  YAHOO.util.Dom.setY(signInContents, headerRegion.top + 20);
}

function focusOnEmail()
{
 document.getElementById('popoutEmail').focus();
}

function hideSignInContents() {
  YAHOO.util.Dom.setStyle(signInContents, 'visibility','hidden');
  YAHOO.util.Dom.setStyle(signInContents, 'display','none');

  YAHOO.util.Event.removeListener(signInLink, 'click', hideSignIn);
  YAHOO.util.Event.addListener(signInLink, 'click', showSignIn);
}

var showSignIn = function(e) {
  animSignIn.animate();
  setTimeout('showSignInContents()',100);
}

var hideSignIn = function(e) {
  hideSignInContents();
  deanimSignIn.animate();
}

// Load the signin animation on page load
YAHOO.signin.anim.init = function() {
  YAHOO.util.Event.addListener(signInLink, 'click', showSignIn);
}
YAHOO.util.Event.addListener(window, 'load', YAHOO.signin.anim.init);


var originalContent;
var passwordHelpPopoutDivContent = null;
function requestPasswordHelp()
{
  // if someone needs password help, we need to switch the content of the sign-in popout with the
  // content of the password-help popout.
  var passwordHelpPopoutDiv = document.getElementById("forgotten-password-div");

  if (passwordHelpPopoutDivContent == null)
  {
    passwordHelpPopoutDivContent = passwordHelpPopoutDiv.innerHTML;
  }

  // we have to set the imported passwordHelpPopoutDiv' innerHtml to empty so that the form
  // won't be on the page twice.
  passwordHelpPopoutDiv.innerHTML= "";

  var signInPopoutDiv = document.getElementById("signInContents");
  originalContent = signInContents.innerHTML;
  signInPopoutDiv.style.width = "280px";

  // now switch them
  signInPopoutDiv.innerHTML = passwordHelpPopoutDivContent;
}

closePasswordHelpPopout = function()
{
  // first switch the content back to what it was originally
  var popoutContentDiv = document.getElementById("signInContents");
  popoutContentDiv.innerHTML = originalContent;
  popoutContentDiv.style.width = "280px";

  // now hide the popout
  hideSignIn();
}


function submitLayoutLoginForm()
{
  setDestination();
  showLoginAnimation();
  document.loginForm.submit();
}

function setDestination()
{
  var url = document.location.pathname;
  var forward = "";
  if ((url.indexOf("/listing") != -1) ||
      (url.indexOf("/quickSearch") != -1) )
  {
    forward = "listing";
  }
  else if (url.indexOf("/storeHome") != -1)
  {
    forward = "storeHome";
  }
  else if ((url.indexOf("/ship") != -1) ||
           (url.indexOf("/payment") != -1) ||
           (url.indexOf("/confirm") != -1))
  {
    forward = "cart";
  }
  document.loginForm.forward.value=forward;
}

function showLoginAnimation()
{
  var animationText = "Your request is being processed... ";
  var message = "<br><img src=" + grayLoadingImg.src + " class='icon-mid'>" + animationText+"</td></tr></table>";
  showMessage(message, "dynamicLoginContent");
  
  hide(document.getElementById("login-errors"));
  hide(document.getElementById("signinTable"));
  show(document.getElementById("dynamicLoginContent"));

}


