///////////////////////////////////
//        ajaximrpg 4.00         //
//    AJAX Instant Messenger     //
//      Copyright (c) 2010       //
//   Do not remove this notice   //
///////////////////////////////////

/**
 * User status.
 **/
var Status = {
  awayMessage: '',           // away message
  wasSetAutoAway: false,     // set as 'away' because no activity
  lastIM: null,              // timestamp of the last IM you sent
  state: 1,                  // current status
  offline: 0,                // 'offline' constant
  online: 1,                 // 'online' constant
  away: 2,                   // 'away' constant
  invisible: 3,              // 'invisible' constant
  friends: 4,                // 'friends' constant

  /**
   * Change your status.
   *
   * @arguments
   *   status - status [0=online, 1=away, 49=invisible, 99=friends only]
   *   away_msg - away message to use
   *
   * @author Joshua Gross
   **/
  set: function(status, message) {
    var curStatus = $('curStatus');
    if (status == this.away) {
      this.state = this.away;
      this.awayMessage = message;
      if (curStatus != null) {
        $('curStatus').innerHTML = this.limitString(this.awayMessage, 30);
      }
    } else { // back
      this.lastIM = new Date().getTime();
      this.state = status;
      this.awayMessage = '';
      if (curStatus != null) {
        $('curStatus').innerHTML = message;
      }
    }
      
    $('statusList').hide();
  },

  /**
   * Display entry box to allow the user to enter
   * a custom away message.
   *
   * @author Joshua Gross
   **/
  customAway: function() {
    $('curStatus').hide();
    $('customStatus').show().focus();
  },

  /**
   * Handle keyboard entires on customStatus.
   *
   * @arguments
   *   event - sent by browser
   *
   * @author Joshua Gross
   **/
  processCustomAway: function(event) {
    event = event || event.window;
    var asc = document.all? event.keyCode: event.which;
    if (asc == 13) {
      this.awayMessage = $('customStatus').value;
      $('curStatus').innerHTML = this.limitString(this.awayMessage, 30);
      $('curStatus').show();
      $('customStatus').hide();
      Status.set(1, awayMessage);
    }
    return asc != 13;
  },
  
  /**
   * Display/hide the status drop down list.
   *
   * @author Joshua Gross
   **/
  toggleStatusList: function() {
    var obj = $('statusList');
    if (obj.style.display == 'block') {
      obj.hide();
      if (obj.style.zIndex > Windows.maxZIndex) {
        Windows.maxZIndex = obj.style.zIndex;
      }
    } else {
      Element.setStyle(obj, {left: parseInt(Buddylist.buddyListWin.getLocation()['left']) + $('statusSettings').offsetLeft + $('blTopToolbar').offsetLeft + 'px', top: parseInt(Buddylist.buddyListWin.getLocation()['top']) + $('statusSettings').offsetTop + $('blTopToolbar').offsetTop + $('statusSettings').offsetHeight + 'px', zIndex:  Windows.maxZIndex + 20, display: 'block'});
    }
  },
  
  limitString: function(s, len) {
    return s.substring(0, len) + ((s.length > len)? '...': '');
  }
};
