// is the current user logged in?
function logged_in() {
  return _member.logged_in;
}

// wrap functions in this to get the login form first (if required)
function halt_for_login(do_after) {
  if (logged_in()) {
    do_after();
  } else {
    request_login(do_after);
  }
}

function request_login(do_after) {
  // hide the failed notice (if this is their second attempt)
  $('login-failed-notice').hide();
  $('forgot_password_container').hide();
  $('reset_password_container').hide();

  pop_over($('login-form-container'));

  var frm = $('login_form');
	frm.onsubmit = function(e) {
    $('login-failed-notice').hide();
    $('login-failed-notice').update('Login failed');
    
		// is everything ok with the fields
		if (form_presubmit_ok(frm, e)) {
      // we can drop straight into the submit now
      var params = Form.serialize(frm);

      // do the async submit
      var request = new Ajax.Request(frm.action,
      {
        asynchronous  : true, 
        parameters    : params, 
        onSuccess     : function(t) { login_form_onSuccess(t, do_after); },
        onFailure     : login_form_onFailure
      });
		}
		
		return false;
	};
	
	var frm2 = $('forgot_password');
	frm2.onsubmit = function(e){
		// is everything ok with the fields
		if (form_presubmit_ok(frm2, e)) {
      // we can drop straight into the submit now
      var params = Form.serialize(frm2);

      // do the async submit
      var request = new Ajax.Request(frm2.action,
      {
        asynchronous  : true, 
        parameters    : params, 
        onSuccess     : function(t) { forgot_password_onSuccess(t, do_after); },
        onFailure     : forgot_password_onFailure
      });
		}
		
		return false;
	};
	
	var frm3 = $('reset_password');
	frm3.onsubmit = function(e){
		// is everything ok with the fields
		if (form_presubmit_ok(frm3, e)) {
      // we can drop straight into the submit now
      var params = Form.serialize(frm3);

      // do the async submit
      var request = new Ajax.Request(frm3.action,
      {
        asynchronous  : true, 
        parameters    : params, 
        onSuccess     : function(t) { reset_password_onSuccess(t, do_after); },
        onFailure     : reset_password_onFailure
      });
		}
		
		return false;
	};
}

function login_form_onSuccess(t, do_after) {
  var result = t.responseJSON;

  // check if form submitted with empty form fields
 	if(result == null){
 		login_form_onFailure(t); 
 	}

  if (result.success) {
    pop_over_close();

    // fire off the logged in custom event
    Event.fire(document, 'session:logged_in', result);

    // need to set the original event running again
    do_after();
  } else {

    // let the handlers decide what to do about it
  	Event.fire(document, 'session:logged_in_failed', result );
  }
}
function login_form_onFailure(t) {
  $('login-failed-notice').show();
}

function forgot_password_onSuccess(t, do_after) {
  var result = t.responseJSON;
  
  /*
    If email address was found show the code form
    else display error
  */
  if(result.success)
  {
    $('reset_email').value = '';
    Effect.SlideUp('forgot_password_container', {duration: .3});
    Effect.SlideDown('reset_password_container', { queue: 'end', duration: .3 });
    do_after();
  }
  else
  {
    $('forgot-failed-notice').show();
  }
}

function forgot_password_onFailure(t) {
  //Handle failure
}

function reset_password_onSuccess(t, do_after) {
  var result = t.responseJSON;
  
  //
  if(result.success)
  {
    $('password_code').value = '';
    $('new_password').value = '';
    $('login-failed-notice').update('Password changed successfully'); 
    $('login-failed-notice').show();
    Effect.SlideUp('reset_password_container', {duration:.3});
    do_after();
  }
  else
  {
    switch(result.error_code)
    {
      case 'code_not_found':
        $('reset-failed-notice').update('Code could not be found'); 
        break;
        
      case 'no_password':
        $('reset-failed-notice').update('No password was set, password is required'); 
        break;
        
      default:
        $('reset-failed-notice').update('Error');
        break; 
    }
    
    $('reset-failed-notice').show();
  }
}

function reset_password_onFailure(t) {
  //Handle failure
}
