var input_time = new Date().getTime();
var newest_mess_id = 0;

function post(){
  if ($('context').value.length == 0){
    return false;
  }else{
    if (hasWhiteSpace($('context').value)){
      var form = $('chat-input-box');
    
    new Ajax.Request(form.action, {
        method: form.method,
        parameters: Form.serialize(form),
        asynchronous: true, 
        evalScripts: true,
        isCache: false,
        onLoading: delete_input,
        onSuccess: replace_html
    });
    return false;
    }else{
      return false;
    }
  }
}

function hasWhiteSpace(s){
   reWhiteSpace = new RegExp(/^\s+$/);
   if (reWhiteSpace.test(s)) {
        return false;
   }
  return true;
}

function replace_html(request){
  $('chat_box').scrollTop = $('chat_box').scrollHeight;
  $('context').focus();
}

function delete_input(){
  $('context').value = '';
}

function chat_box_checkEnterKey(e) {
  var input_time2 = new Date().getTime();
  if (e.keyCode == 13) {
    if ((input_time2 - input_time) > 2000){
      input_time = input_time2;
      post();
      return false;
    }else{
      return false;
    }
  } else {
    return true;
  }
}

function submit_check() {
  var input_time2 = new Date().getTime();
  if ((input_time2 - input_time) > 2000){
    input_time = input_time2;
    post();
    return false;
  }else{
    return false;
  }
}

function reload_chat_box(room_id){
  var message_id = $('last_message_id').innerHTML;
  reload_ajax = new Ajax.Request('/chat/' + room_id + '/refresh', {
    asynchronous: true, 
    evalScripts: true,
    parameters:{
      room_id: room_id,
      mess_id: message_id
    },
    onComplete: chat_box_loop
  });
}

function reload_users(room_id){
  reload_ajax_users = new Ajax.Request('/chat/' + room_id + '/refresh_users',{
    asynchronous: true,
    evalScripts: true,
    parameters:{
      room_id: room_id
    },
    onComplete: users_loop
  });
}

function reload_rooms(room_id){
  reload_ajax_users = new Ajax.Request('/chat/' + room_id + '/refresh_rooms',{
    asynchronous: true,
    evalScripts: true,
    parameters:{
      room_id: room_id
    },
    onComplete: rooms_loop
  });
}

function chat_box_loop(){
  timer = setTimeout("reload_chat_box('"+ refresh_room_id + "');", 5000);
}

function users_loop(){
  timer_user = setTimeout("reload_users('"+ refresh_room_id + "');", 60000);
}

function rooms_loop(){
  timer_rooms = setTimeout("reload_rooms('"+ refresh_room_id + "');", 180000);
}

function change_room(room_num, room_name){
  new Ajax.Request('/chat/' + room_num + '/room', {
    asynchronous: true, 
    evalScripts: true,
    parameters:{
      room: room_num,
      room_name: room_name
    },
    onLoading: kill_process,
    onLoaded: kill_process,
    onSuccess: stop_timer
  });
}

function stop_timer(){
  clearTimeout(timer);
  clearTimeout(timer_user);
  clearTimeout(timer_rooms);
  reload_ajax.transport.abort();
}

function kill_process(){
  reload_ajax.transport.abort();
}

function check_max_length(Object, MaxLen)
{
  if(Object.value.length > MaxLen)
  {
    Object.value = Object.value.substring(0, MaxLen);
  }
}

/* chat wall */
function paginate_chatwall(current_page, total_pages, username){
  new Ajax.Updater('box-center','/a/' + username + '/chatwall/page',{asynchronous: true, evalScripts: true, onLoaded: function(request){}, 
    onLoading: function(request){new Effect.Opacity(type, {duration:0.5, from: 1, to: 0.3 });show_loading();}, onComplete:function(request){show_share();new Effect.Opacity(type, {duration:0.5, from: 0.3, to: 1 });hide_loading();}, parameters: {current_page: current_page, total_pages: total_pages}});return false;
}

function post_chatwall(username){
  if ($('context').value.length == 0){
    return false;
  }else{
    if (hasWhiteSpace($('context').value)){
      var form = $('chat-input-box');
    
    new Ajax.Updater('box-center','/a/' + username + '/chatwall/post',{asynchronous: true, evalScripts: true,parameters: Form.serialize(form), onLoaded: function(request){}, 
    onLoading: function(request){$('context').disabled=true;new Effect.Opacity(type, {duration:0.5, from: 1, to: 0.3 });show_loading();}, onComplete:function(request){$('context').disabled=false;$('context').value="";show_share();new Effect.Opacity(type, {duration:0.5, from: 0.3, to: 1 });hide_loading();}});return false;
    }else{
      return false;
    }
  }
}

function chat_wall_checkEnterKey(e, username, logged_in, href) {
  var input_time2 = new Date().getTime();
  if (e.keyCode == 13) {
    if ((input_time2 - input_time) > 2000 && logged_in != ''){
      input_time = input_time2;
      post_chatwall(username);
      return false;
    }else{
      chatwall_popup(href);
      return false;
    }
  } else {
    return true;
  }
}

function chatwall_popup(href){
  var f = null;
  f = new Facebox();
  new Ajax.Updater('chatwall-popup', href,{asynchronous: true, evalScripts: true, onComplete:function(request){f.show_popup("login-popup");}});return false;
  
}

function delete_chatwall(message_id, username){
  new Ajax.Updater('box-center','/a/' + username + '/chatwall/delete',{asynchronous: true, evalScripts: true, onLoaded: function(request){}, 
    onLoading: function(request){new Effect.Opacity(type, {duration:0.5, from: 1, to: 0.3 });show_loading();}, onComplete:function(request){show_share();new Effect.Opacity(type, {duration:0.5, from: 0.3, to: 1 });hide_loading();}, parameters: {message_id: message_id}});$('facebox').hide();return false;
}