var session = {
  /**
   * Renders the form for logging in
   * 
   * @param {String} redirect_to The url to redirect to after the session is created
   */
  create: function(redirect_to) {
    // Store the redirect url in the dialog
    if (redirect_to) {
      $('#session-new-dialog').data('redirect_to', redirect_to);
    }
    
    // Show the dialog, resetting the height in case it needs to be re-calculated
    $('#session-new-dialog').height('').dialog({
      title: 'Login',
     
      buttons: { 
        'Login': function(event) {
          var form = $('#session-new');
          var dialog = $(this);
          var dialogContainer = dialog.parents('.ui-dialog').spinner();
          
          // Post the new session
          $.ajax({
            type: 'POST',
            url: form.attr('action'),
            data: form.serialize(),
            success: function(html, status) {
              // User logged in: Close the dialog and load the homepage
              dialogContainer.spinner('destroy');
              dialog.dialog('close');
              
              window.location.href = dialog.data('redirect_to');
            },
            error: function(xhr) {
              $('#session-new .errors').hide();
              
              // Display new errors
              if (xhr.status == 422) {
                form.replaceWith(xhr.responseText);
              } else {
                form.prepend(errors.build(['There was a problem logging in. Please try again.']).css('color', '#E45858'));
              }
              
              dialogContainer.spinner('destroy');
              dialog.dialog('resize');
            }
          });
          
          return false;
        },
        'Cancel': function() {
          $(this).dialog('close');
          return false;
        }
      
      },
      
      close: function() {
        $('#session-new .errors').hide();
      }
    }).show().dialog('open');
    
    return false;
  }
};

// Add event hooks when the document is finished loading
$(document).ready(function() {
  $('#session-new-dialog')
    // Submits a new session
    .delegate('click', {'#session-new-login': function(event) {
      var form = $('#session-new');
      var dialog = $(this);
      var dialogContainer = dialog.parents('.ui-dialog').spinner();
      
      // Post the new session
      $.ajax({
        type: 'POST',
        url: form.attr('action'),
        data: form.serialize(),
        success: function(html, status) {
          // User logged in: Close the dialog and load the homepage
          dialogContainer.spinner('destroy');
          dialog.dialog('close');
          
          window.location.href = dialog.data('redirect_to');
        },
        error: function(xhr) {
          $('#session-new .errors').hide();
          
          // Display new errors
          if (xhr.status == 422) {
            form.replaceWith(xhr.responseText);
          } else {
            form.prepend(errors.build(['There was a problem logging in. Please try again.']));
          }
          
          dialogContainer.spinner('destroy');
          dialog.dialog('resize');
        }
      });
      
      return false;
    }})
    
    // Cancels the session dialog
    .delegate('click', {'#session-new-cancel': function() {
      $(this).dialog('close');
      return false;
    }});
});
