// ----------------------------------
// App startup login
// ----------------------------------
// at app startup, check if a user has already logged in, and a valid session
// exists. if so, don't show the login sreen, go directly to the home screen.
// requesting the login action with no email and password supplied will
// return success: true if the user has already logged in. in that case,
// hide the startup indeterminate animation and background.

function attemptInitialLogin() {
  new Ajax.Request("api/accounts/login", {parameters: {series: seriesID, course: courseID}, onSuccess: initialLoginSuccessful, onFailure: initialLoginFailed});
}

function initialLoginSuccessful(transport) {
  if(transport.responseJSON.success){
    loadUI(transport.responseJSON);
  } else {
    initialLoginFailed(transport);
  }
}

function initialLoginFailed(transport) {
  $('startup_indeterminate').fade();
  $('login_panel').appear({duration: 0.5});
}


// ----------------------------------
// UI
// ----------------------------------
// attempt a log in using the email and password. the ajax call will set
// a session cookie that the django app uses to identify the user
function tryLogin() {
  $('login_email').disabled = true;
  $('login_email').style.color = '#aaa';
  $('login_password').disabled = true;
  $('login_password').style.color = '#aaa';
  $('login_indeterminate').appear({duration: 0.3});
  new Ajax.Request("api/accounts/login", {parameters: {email: $('login_email').value, password: $('login_password').value, series: seriesID, course: courseID}, onSuccess: loginSuccessful, onFailure: loginFailed});
}

// when clicking the continue button, or pressing enter in the email
// or password fields, attempt a login
$('login_continue').observe('click', function(event) {
  event.stop();
  tryLogin();
});

$('login_email').observe('keypress', function(event) {
  if(event.keyCode == 13 || event.keyCode == 10)
    tryLogin();
});

$('login_password').observe('keypress', function(event) {
  if(event.keyCode == 13 || event.keyCode == 10)
    tryLogin();
});

function loadUI(json) {
  // update the current course
  courseID = json.course.id;

  // initialise UI
  loadTabs(json.tabs, json.is_index, json.ncourses, json.course.series.name);
  document.title = json.course.series.full_name;
  $('series_logo').src = 'images/' + json.course.series.logo;
  if (json.is_index) {
    $('series_name').innerHTML = json.course.series.full_name;
  }
  else {
    $('series_name').innerHTML = json.course.series.full_name;
    if (json.course.year != 0)
      $('series_name').innerHTML += ' &ndash; ' + json.course.year;
    var w = $('welcome_message');
    w.update();
    var a = new Element('a', {href: json.account_url});
    a.insert(json.user.first_name + ' ' + json.user.last_name);
    w.appendChild(a);
  }

  // remove the screen blocker and fade in the UI
  $('startup').fade();
  $('login_panel').fade();
  $('sidebar_tabs').appear();
  $('series_name').appear();
  $('series_logo').appear();
}


// ----------------------------------
// Response Handling
// ----------------------------------
function loginSuccessful(transport) {
  if (transport.responseJSON.success) {
    loadUI(transport.responseJSON);
  } else {
    if(transport.responseJSON.reason)
      failLoginWithReason(transport.responseJSON.reason);
    else
      failLoginWithReason("An unknown error occurred when logging in. Please try again later.");
  }
}

function loginFailed() {
  failLoginWithReason("The server encountered an error when trying to log you in. Please try again later.");
}

function failLoginWithReason(reason) {
  alert(reason);
  $('login_indeterminate').fade({duration: 0.3});
  $('login_email').disabled = false;
  $('login_email').style.color = '#333';
  $('login_password').disabled = false;
  $('login_password').style.color = '#333';
}
