/** 
 * Static class containing login functionality.
 *
 * TODO: if multiple callbacks are necessary by disparate functionality then
 * the callbacks will need to be invoked serially: first(), second(), third(),
 * etc. instead of overriding the callback method.
 *
 * Currently only Comments.class.js uses the callbacks.
 */
var Login =
{
  /**
   * Invokes the login ajax request.
   */
  submit: function(form)
    {
      form = $(form);

      var callback = function(data)
        {
          if ('success' == data.status)
          {
            $('#banner .welcome').html(data.userWelcomeBanner);
            User.setLoggedIn(true);
          }
          Login.hideWaiting();
          Login.callback(data);
        };

      Login.showWaiting();
      $.post(form.attr('action'),
             form.find(":input").serializeArray(),
             callback, 'json');

      return false;
    },

  showWaiting: function()
    {
      $('#userform :submit')
        .attr('value', 'Working...')
        .attr('disabled',true);
    },

  hideWaiting: function()
    {
      $('#userform :submit')
        .attr('value', 'Sign In')
        .attr('disabled',false);
    },

  /**
   * Invoked by the ajax response handler to perform page-specific actions for
   * post-login actions (such as login success or login error).
   */
  callback: function(data){},

  /**
   * Cancels the ajax login. This function has a page-specific implementation
   * and should be overridden.
   */
  cancel: function(){}
};
