/**
 * Authorization Status.
 *
 * Fires custom events in order to broadcast the user's authorization status.
 * Successful authorization fires an event containing the user's screen name
 * and other data (e.g., todo link). Use document.observe('check_auth:status',
 * function(event) {}); to monitor authorization status.
 *
 * Compare with 'auth:[start|failed|verify|etc]', which actually cause an authorization process to occur.
 *
 * @see: http://www.prototypejs.org/api/element/fire
 *
 * @event: check_auth:start
 * @event: check_auth:success
 * @event: check_auth:failure
 * @event: check_auth:exception
 */

var auth_status = Class.create({
  initialize: function() {
    this.page_node = $$('body')[0],
    this.login_status = false,
    this.payload = null;
    this.auth_type = 'start'
    this._get();
  },
  _get: function() {
    var randomnumber = Math.floor(Math.random() * 10000000001); /** To prevent caching. */
    if (User.is_logged_in()) { /** We only want true or false. */
      this.login_status = true;
    } else {
      this.login_status = false;
    }
    this._broadcast();
    if (this.login_status) {
      new Ajax.Request('/home/user_data?' + randomnumber, {
        method:'get',
        onException: function() {
          this.auth_type = 'exception';
          this.payload = {
            'error': 'Exception: Unable to retrieve user data.'
          };
          Object.toJSON(this.user_data);
          this._broadcast();
        }.bind(this),
        onFailure: function() {
          this.auth_type = 'failure';
          this.payload = {
            'error': 'Failure: Unable to retrieve user data.'
          };
          Object.toJSON(this.user_data);
          this._broadcast();
        }.bind(this),
        onSuccess: function(transport) {
          this.auth_type = 'success';
          this.payload = transport.responseText.evalJSON();
          this._broadcast();
        }.bind(this)
      });
    }
  },
  _broadcast: function() {
    switch (this.auth_type) {
      case 'start':
        this.page_node.fire('check_auth:start', {
          'logged_in': this.login_status
        });
        break;
      case 'success':
        this.page_node.fire('check_auth:success', {
          'logged_in': this.login_status,
          'payload': this.payload
        });
        break;
      case 'failure':
        this.page_node.fire('check_auth:failure', {
          'logged_in': this.login_status,
          'payload': this.payload
        });
        break;
      case 'exception':
        this.page_node.fire('check_auth:exception', {
          'logged_in': this.login_status,
          'payload': this.payload
        });
        break;
    }
  }
});

document.observe('dom:loaded', function() {
  new auth_status(); // NMK: I think this could be just a call to User.is_logged_in, and then fire event
});

document.observe('auth:succeed', function() {
  new auth_status(); 
});

document.observe('session:logged_out', function(){
  new auth_status();
});
