var login_state = 'open';

function toggleLogin() {

  if(login_state=='closed')
  {
      $('#home_login').animate({
        height: '+=85'
      }, 800, function() {
        // Animation complete.
      });
      login_state = 'open';
  }
  else
  {
      $('#home_login').animate({
        height: '-=85'
      }, 800, function() {
        // Animation complete.
      });
      login_state = 'closed';
  }

}


$(document).ready(function() {
  var email_val = $('#user_email').val();
  if (email_val != '')
  {
    $('#Email_title').hide();
  }
  
  $('#login_toggle').click(function (e) {
      toggleLogin();
      e.preventDefault();
  });
  $('#login_form').children().click(function (e) {
      if(login_state=='closed')
      {
          $('#home_login').animate({
            height: '+=85'
          }, 800, function() {
            // Animation complete.
          });
          login_state = 'open';
      }
  });
  $('#login_form').click(function (e) {
      if(login_state=='closed')
      {
          $('#home_login').animate({
            height: '+=85'
          }, 800, function() {
            // Animation complete.
          });
          login_state = 'open';
      }
  });
  $('#Email_title').click(function (e) {
      $('#loginform_error_message').hide();
      if(login_state=='closed')
      {
          $('#home_login').animate({
            height: '+=85'
          }, 800, function() {
            // Animation complete.
          });
          login_state = 'open';
      }
      $(this).fadeOut();
      $('#user_email').focus();
  });
  
  $('#login_form input').focus(function (e) {
      $('#loginform_error_message').hide();
      if(login_state=='closed')
      {
          $('#home_login').animate({
            height: '+=85'
          }, 800, function() {
            // Animation complete.
          });
          login_state = 'open';
      }
      $(this).parents("fieldset").children('label').fadeOut();
  });
  
  
  
  $("#loginform").validate({
		rules: {
		  user_email: {
		    required: true,
		    email: true
		  },
		  user_password: {
		    required: true
		  }
		},
		submitHandler: function (form)
		{
		  if($("#user_password").attr("changed") === "true")
		  {
		    pw = calcMD5($("#user_password").attr("value"));
		    $("#user_password").get(0).value = pw;
		  }
		  form.submit();
		}
  });
  
  
  
});
