
function IM(width, height, top, left) {
 this.popup_width=300;
 this.popup_height=200;
 
 this.width = width;
 this.height = height;
 this.top = top;
 this.left = left;

 this.is_on = true;
 this.open_when_init = false;
 this.sync_timeout = 3000;
 this.tmr = false;
 
 this.prefix = 'im_data';
 if (!IM.instances) IM.instances = new Array();
 this.index = IM.instances.length;
 IM.instances[this.index]=this;

 this.border='1px solid #999999';
 this.backgroundColor='#EEEEEE';
 this.windows= new Hash();
 this.users = new Hash();

 this.messages = new Hash();

 this.start_ts = 0;

 this.indicator_new_messages = new Hash();

 this.main_img_id = '';
 this.main_img_regular = '';
 this.main_img_new = '';
}

IM.agt = navigator.userAgent.toLowerCase();
IM.is_ie = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (IM.agt.indexOf("opera") == -1));
IM.is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
IM.is_mac = (navigator.userAgent.toLowerCase().indexOf("mac") != -1);
IM.is_mac_ie = (IM.is_ie && IM.is_mac);
IM.is_win_ie = (IM.is_ie && !IM.is_mac);
IM.is_gecko = (navigator.product == "Gecko");

IM.instances = new Array();


IM.cur_date_time = function() {
   var d = new Date();
   var d_str = d.getUTCDate().toPaddedString(2)+':'+(d.getUTCMonth() + 1).toPaddedString(2)+':'+d.getUTCFullYear();
   d_str = d_str+' '+ d.getUTCHours().toPaddedString(2) + ':' + d.getUTCMinutes().toPaddedString(2);
   return d_str;
}

IM.prototype.re_init = function(start_ts) {
  this.start_ts = start_ts;
  if (!this.data) {
    this.data = new Window({id: this.prefix+'_init', className: this.prefix+'_users', title: "Instant Messenger Beta", width: this.width, height: this.height, top: this.top, left: this.left, showEffectOptions: {duration:0}, draggable: false, zIndex: 99, resizable: false, wiredDrag: false});
    this.data.getContent().innerHTML='<div id="'+this.prefix+'_userslist" style="overflow:auto;width:100%;height:'+(this.height-25)+'px;">&nbsp;</div><div align="center" style="width:100%"><div id="'+this.prefix+'_on_off" class="'+this.prefix+((this.is_on)?'_off':'_on')+'" onClick="IM.instances['+this.index+'].on_off();">&nbsp;</div></div>';
  }

  var d = new Date();
  sajax_do_call('im_sync_time',Array(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), xaction));

  if (this.open_when_init) {
     this.data.show();
  }
  if (this.is_on) {
    sajax_do_call('im_on',Array(xaction));
  }else{
    sajax_do_call('im_off',Array(xaction));
  }
  this.getUsers();
  sajax_do_call('im_start',Array(this.start_ts, this.index, xaction));
}

IM.prototype.init = function() {
  if (!this.data) {
    this.data = new Window({id: this.prefix+'_init', className: this.prefix+'_users', title: "Instant Messenger Beta", width: this.width, height: this.height, top: this.top, left: this.left, showEffectOptions: {duration:0}, draggable: false, zIndex: 99, resizable: false, wiredDrag: false});
    this.data.getContent().innerHTML='<div id="'+this.prefix+'_userslist" style="overflow:auto;width:100%;height:'+(this.height-25)+'px;">&nbsp;</div><div align="center" style="width:100%"><div id="'+this.prefix+'_on_off" class="'+this.prefix+((this.is_on)?'_off':'_on')+'" onClick="IM.instances['+this.index+'].on_off();">&nbsp;</div></div>';
  }

  var d = new Date();
  sajax_do_call('im_sync_time',Array(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), xaction));

  if (this.open_when_init) {
    this.data.show();
  }
  if (this.is_on) {
    sajax_do_call('im_on',Array(xaction));
  }else{
    sajax_do_call('im_off',Array(xaction));
  }
  this.getUsers();
  this.ping();
}

IM.prototype.show = function() {
  if (this.data) {
     this.data.show();
  }
}

IM.prototype.on_off = function() {
  if (this.is_on) {
   this.is_on=false;
   $(this.prefix+'_on_off').className=this.prefix+'_on';
   sajax_do_call('im_off',Array(xaction));
  }else{
   this.is_on=true;
   $(this.prefix+'_on_off').className=this.prefix+'_off';
   sajax_do_call('im_on',Array(xaction));
  }
}

IM.prototype.setMainImages = function(imgid, img_normal, img_new) {
 this.main_img_id = imgid;
 this.main_img_regular = img_normal;
 this.main_img_new = img_new;
}

IM.prototype.getUsers = function() {
   sajax_do_call('im_get_users',Array(this.index,xaction));
}

IM.prototype.fillUsers = function(in_ids, in_names) {
  if (!this.data) return false;
  this.users = new Hash();
  for (var i=0;i<in_ids.length;i++) {
     this.users.set(in_ids[i],in_names[i]);
  }
  this.updateUsers();
}

IM.prototype.updateUsers = function() {
  var html='';
  var keys = this.users.keys();
  for (var i=0;i<keys.length;i++) {
    html=html+'<div id="'+this.prefix+'_u_'+keys[i]+'" class="'+this.prefix+'_cls_user" onMouseOver="IM.instances['+this.index+'].remove_indicator_new_message('+keys[i]+'); this.className=\''+this.prefix+'_cls_user_over\';" onMouseOut="this.className=\''+this.prefix+'_cls_user\';" onClick="IM.instances['+this.index+'].open('+keys[i]+');">'+this.users.get(keys[i])+'</div>';
  }
  $(this.prefix+'_userslist').innerHTML=html;
}

IM.prototype.open = function(uid) {
  if (!this.windows.get(uid)) {
    this.addWindow(uid);
    this.windows.get(uid).showCenter();
    //Effect.ModalSlideDown(this.windows.get(uid));
  }else{
    this.windows.get(uid).showCenter();    
  }
}

IM.prototype.remove_indicator_new_message = function(uid) {
 if (this.indicator_new_messages.get(uid)) {
    this.indicator_new_messages.unset(uid);
 }
 if (this.indicator_new_messages.keys().length == 0) {
    // set main IM image to <regular>
    if (this.main_img_id) {
      document.images[this.main_img_id].src=this.main_img_regular;
    }
 }
}

IM.prototype.addWindow = function(uid) {
    if (!this.windows.get(uid)) {
      this.windows.set(uid, new Window({id: this.prefix+'_win_'+uid, className: this.prefix+'_win', title: this.users.get(uid), width:this.popup_width, height:this.popup_height, minWidth:this.popup_width, minHeight:this.popup_height, showEffectOptions: {duration:30}, draggable: true, zIndex: 100, resizable: true, wiredDrag: true, minimizable: false, maximizable: false}));
    }
    this.windows.get(uid).getContent().innerHTML='<div class="'+this.prefix+'_mes_container" id="'+this.prefix+'_container_'+uid+'">&nbsp;</div><div class="'+this.prefix+'_send"><form id="'+this.prefix+'_frm_'+uid+'"><textarea name="message" class="'+this.prefix+'_send_textarea"></textarea><input type="button" value="Send" class="'+this.prefix+'_send_button" onClick="IM.instances['+this.index+'].send('+uid+',document.forms[\''+this.prefix+'_frm_'+uid+'\'].message.value); document.forms[\''+this.prefix+'_frm_'+uid+'\'].message.value=\'\';" /></form></div>';
}

IM.prototype.ping = function() {
  if (this.tmr) clearTimeout(this.tmr);
  this.tmr = setTimeout('IM.instances['+this.index+'].ping();',this.sync_timeout);
  sajax_do_call('im_sync',Array(this.index, (this.is_on)?'1':'', xaction));
}

IM.prototype.sync = function(in_ids, in_names, in_delete, in_mids, in_mfrom, in_mdt, in_mmes) {
  var users_changed = false;
  for (var i=0; i<in_ids.length; i++) {    
    if (!this.users.get(in_ids[i])) {
      this.users.set(in_ids[i],in_names[i]);
      users_changed = true;
    }
  }

  for (var i=0; i<in_delete.length; i++) {
    if (this.users.get(in_delete[i])) {
      this.users.unset(in_delete[i]);
      users_changed = true;
    }
  }
  if (users_changed) {
    this.updateUsers(); 
  }

  for (var i=0; i<in_mids.length;i++) {
    this.indicator_new_messages.set(in_mfrom[i],true);
    // set main IM image to <new message>
    if (this.main_img_id) {
      document.images[this.main_img_id].src=this.main_img_new;
    }
    //go add messages to windows...
    if (!this.windows.get(in_mfrom[i])) {
      this.addWindow(in_mfrom[i]);
    }
    if (!this.messages.get(in_mids[i])) {
      $(this.prefix+'_u_'+in_mfrom[i]).className=this.prefix+'_cls_user_new';
      var html = $(this.prefix+'_container_'+in_mfrom[i]).innerHTML;
      if (html=='&nbsp;') html='';
      html=html+'<div class="'+this.prefix+'_mes"><div class="'+this.prefix+'_mes_date">'+in_mdt[i]+'</div><div class="'+this.prefix+'_mes_body">'+unescape(in_mmes[i])+'</div></div>';
      $(this.prefix+'_container_'+in_mfrom[i]).innerHTML = html;
      $(this.prefix+'_container_'+in_mfrom[i]).scrollTop = $(this.prefix+'_container_'+in_mfrom[i]).scrollHeight;
      this.messages.set(in_mids[i],'1');
    }
  }
}

IM.prototype.send = function(uid,mes) {
    if (!mes) return;
    if (!this.is_on) {
      alert('You can not send messages, while Your messanger is turned off.');
      return;
    }
    if (!this.windows.get(uid)) {
      this.addWindow(uid);
    }
    var html = $(this.prefix+'_container_'+uid).innerHTML;
    if (html=='&nbsp;') html='';
    mes = mes.replace(/\n/,'<br>');
    html=html+'<div class="'+this.prefix+'_mes"><div class="'+this.prefix+'_own_mes_date">'+IM.cur_date_time()+'</div><div class="'+this.prefix+'_own_mes_body">'+mes+'</div></div>';
    $(this.prefix+'_container_'+uid).innerHTML = html;
    $(this.prefix+'_container_'+uid).scrollTop = $(this.prefix+'_container_'+uid).scrollHeight;
    sajax_do_call('im_send',Array(uid, mes, xaction));
}

IM.prototype.pre_fill = function(in_mids, in_mmy_message, in_muser, in_mdt, in_mmes) {
  for (var i=0; i<in_mids.length;i++) {   
    //go add messages to windows...
    if (!this.windows.get(in_muser[i])) {
      this.addWindow(in_muser[i]);
    }
    if (!this.messages.get(in_mids[i])) {
      var html = $(this.prefix+'_container_'+in_muser[i]).innerHTML;
      if (html=='&nbsp;') html='';
      if (in_mmy_message[i]) {
         html=html+'<div class="'+this.prefix+'_mes"><div class="'+this.prefix+'_own_mes_date">'+in_mdt[i]+'</div><div class="'+this.prefix+'_own_mes_body">'+unescape(in_mmes[i])+'</div></div>';
      }else{
         html=html+'<div class="'+this.prefix+'_mes"><div class="'+this.prefix+'_mes_date">'+in_mdt[i]+'</div><div class="'+this.prefix+'_mes_body">'+unescape(in_mmes[i])+'</div></div>';
      }
      $(this.prefix+'_container_'+in_muser[i]).innerHTML = html;
      $(this.prefix+'_container_'+in_muser[i]).scrollTop = $(this.prefix+'_container_'+in_muser[i]).scrollHeight;
      this.messages.set(in_mids[i],'1');
    }
  }
  this.ping();
}