var Chat = {
  
  Initialised: 0,
  Executor: null,
  formName: 'frmSubmitChatMessage',
  MemberName: '',
  LastMessage: '',
  LastMessageModerator: '',
  RefreshRate: 10,

  initialise: function() {
    $('ChatSubmitButton').observe('click',Chat.submit);
    Chat.refreshChat();
  },

  refreshChat: function() {
    $('ChatLoading').show();
    if(typeof(Chat.Executor) !== 'undefined') {
      window.clearTimeout(Chat.Executor);
    }
    xajax_updateChat(Chat.LastMessage,Chat.LastMessageModerator);
  },

  resetSender: function() {
    $('ChatMemberName').value = Chat.MemberName;
  },
  
  resize: function() {
    $('ChatWindow').width = '48%';
    $('ModeratorWindow').width = '48%';
  },
  
  scheduleUpdate: function() {
    Chat.stopLoading();
    Chat.Executor = Chat.refreshChat.delay(Chat.RefreshRate);
  },
  
  submit: function() {
    Chat.stopChat();
    xajax_submitChatMessage(xajax.getFormValues(Chat.formName));
    $('ChatMessageBox').value='';
  },
  
  stopChat: function() {
      window.clearTimeout(Chat.Executor);
  },
  
  stopLoading: function() {
    $('ChatLoading').hide();
  },
  
  updateDisplay: function(WindowId) {
    var objDiv = $(WindowId);
    if($('AutoScroll').checked) {
      objDiv.scrollTop = objDiv.scrollHeight;
    }
  },

  updateWindow: function(WindowId, Content, LastMessage) {
    $(WindowId).insert({bottom: Content});
    Chat.LastMessage = LastMessage;
    Chat.LastMessageModerator = LastMessage;
    if($('AutoScroll').checked) {
      $(WindowId).scrollTop = $(WindowId).scrollHeight;
    }
  }
  
}
