// IM Class
var IM = {
   windows: {},
   sendBoxWithFocus: null,
   SEND_STATES: true,
  
/**
 * How often the state is updated if it differs from the previous, in seconds.
 */
STATE_INTERVAL: 15,
_currentState: null,
_previousState: null,
_stateTimer: null,


   create: function(name, imTitle, winType) {
	  if(typeof(IM.windows[name]) != 'undefined') {
	    Buddylist.activeIMWin(name);
		return;
	  }

      var imLeft = Math.round(Math.random()*(Browser.width()-360))+'px';
      var imTop  = Math.round(Math.random()*(Browser.height()-400))+'px';
   
      var winId = name + '_im';

      this.windows[name] = new IMWindow({id: winId, className: "dialog", width: 320, height: 335, top: imTop, left: imLeft,
                                                                   resizable: true, title: imTitle, draggable: true, detachable: imDetachable, minWidth: 320, minHeight: 150,
                                                                   showEffectOptions: {duration: 0}, hideEffectOptions: {duration: 0}});
      
      this.windows[name].setConstraint(true, {left: 0, right: 0, top: 0, bottom: 0});
      
      this.windows[name].getContent().innerHTML = '<div class="rcvdMessages" id="' + winId + '_rcvd"></div>' + "\n" +
                                                  '<div class="imToolbar" id="' + winId + '_toolbar" onmousemove="return false;" onselectstart="return false;"><img src="'+STATIC_ZICZAC+'/themes/'+theme+'/window/bold_off.png" onmouseover="buttonHover(this);" onmouseout="buttonNormal(this);" onclick="IM.windows[\'' + name + '\'].toggleBold();" onmousedown="return false;" alt="' + Languages.get('bold') + '" id="' + winId + '_bold" /> ' +
                                                  '<img src="'+STATIC_ZICZAC+'/themes/'+theme+'/window/italic_off.png" onmouseover="buttonHover(this);" onmouseout="buttonNormal(this);" onclick="IM.windows[\'' + name + '\'].toggleItalic();" onmousedown="return false;" alt="' + Languages.get('italic') + '" id="' + winId + '_italic" /> '+
                                                  '<img src="'+STATIC_ZICZAC+'/themes/'+theme+'/window/underline_off.png" onmouseover="buttonHover(this);" onmouseout="buttonNormal(this);" onclick="IM.windows[\'' + name + '\'].toggleUnderline();" onmousedown="return false;" alt="' + Languages.get('underline') + '" id="' + winId + '_underline" />' +
												  '<img src="'+STATIC_ZICZAC+'/themes/'+theme+'/window/buzz_off.png" onmouseover="buttonHover(this);" onmouseout="buttonNormal(this);" onclick="IM.windows[\'' + name + '\'].toggleBuzz();" onmousedown="return false;" alt="' + Languages.get('buzz') + '" id="' + winId + '_buzz" /></div>' +
                                                  ' <a href="#" class="setFontLink" id="' + winId + '_setFont" onclick="IM.windows[\'' + name + '\'].toggleFontList();return false;" onselectstart="return false;">Tahoma</a>' +
                                                  ' <a href="#" class="setFontSizeLink" id="' + winId + '_setFontSize" onclick="IM.windows[\'' + name + '\'].toggleFontSizeList();return false;" onselectstart="return false;">11</a>' +
                                                  ' <a href="#" class="setFontColorLink" id="' + winId + '_setFontColor" onclick="IM.windows[\'' + name + '\'].toggleFontColorList();return false;" onselectstart="return false;"><div id="' + winId + '_setFontColorColor" style="width:14px;height:14px;display:block;"></div></a>' +
                                                  ' <a href="#" class="insertEmoticonLink" id="' + winId + '_insertEmoticon" onclick="IM.windows[\'' + name + '\'].toggleEmoticonList();return false;" onselectstart="return false;"><img src="'+STATIC_ZICZAC+'/themes/' + theme + '/emoticons/mini_smile.gif" width="14" height="14" style="border:0;" /></a>' +
                                                  "\n" + '<div style="overflow:auto;"><textarea class="inputText" id="' + winId + '_sendBox" onfocus="blinkerOn(false);IM.sendBoxWithFocus=this;" onblur="IM.sendBoxWithFocus=null;" onkeypress="return IM.windows[\'' + name + '\'].keyHandler(event);"></textarea></div>' +
												   '<div class="imStatusbar" id="' + winId + '_statusbar"></div>';
      
      this.windows[name].setUsername(name);
      
      $(winId + '_rcvd').setStyle({height: (this.windows[name].getSize().height - 103) + 'px',
                                   width: (this.windows[name].getSize().width - 10) + 'px'});
   
      $(winId + '_toolbar').setStyle({top: (this.windows[name].getSize().height - 73) + 'px',
                                      width: (this.windows[name].getSize().width - 10) + 'px'});
      
      $(winId + '_setFont').setStyle({top: (this.windows[name].getSize().height - 65) + 'px'});
      
      $(winId + '_setFontSize').setStyle({top: (this.windows[name].getSize().height - 65) + 'px'});
      
      $(winId + '_setFontColor').setStyle({top: (this.windows[name].getSize().height - 65) + 'px'});
      
      $(winId + '_setFontColorColor').setStyle({backgroundColor: '#000000'});
      
      $(winId + '_insertEmoticon').setStyle({top: (this.windows[name].getSize().height - 65) + 'px'});
   
      $(winId + '_sendBox').setStyle({top: (this.windows[name].getSize().height - 45) + 'px',
                                      left: '2px',
                                      width: (this.windows[name].getSize().width - 16) + 'px',
                                      fontWeight: '400',
                                      fontStyle: 'normal',
                                      textDecoration: 'none'});
	$(winId + '_statusbar').setStyle({top: (this.windows[name].getSize().height + 15) + 'px',
                                      width: (this.windows[name].getSize().width - 10) + 'px'});

	  // Neu wintype la conference. Nghia la chat voi 1 nick ao trong room o cua so rieng thi se destroy cua so do sau khi close.

	  
      this.windows[name].show();
      this.windows[name].toFront();
      Windows.focusedWindow = this.windows[name];
      setTimeout("$('"+winId+"_sendBox').focus();", 250);

   },

   sendMessage: function(to, message, type, isBold, isItalic, isUnderline, fontName, fontSize, fontColor) {

		var CNChatCount 	= $("CNmsgCount");
		intChatCountOld 	= parseInt(CNChatCount.value);
		intChatCountNew 	= intChatCountOld+1;
		CNChatCount.value 	= intChatCountNew;
		
		if(intChatCountNew>3)
		{
			alert(Languages.get('sendtoofast'));
			return;
		}
		
		 // xu ly send message cho user zz va user ngoai
		if(to.search("@"+JSERVER)!=-1 || to.search(CONFERENCE_SERVER)!=-1) // neu gui cho user trong ziczac, normal chat or group chat
		{			
			message2 = '<font face="'+fontName+',sans-serif" color="'+fontColor+'"><span style="font-size: '+fontSize+'px">'+(isBold == 'true' ? '<b>' : '') + (isItalic == 'true' ? '<i>' : '') + (isUnderline == 'true' ? '<u>' : '') + message + (isUnderline == 'true' ? '</u>' : '') + (isItalic == 'true' ? '</i>' : '') + (isBold == 'true' ? '</b>' : '') +'</span></font>';
		}
		else
		{
			message2 = (isBold == 'true' ? '<b>' : '') + (isItalic == 'true' ? '<i>' : '') + (isUnderline == 'true' ? '<u>' : '') + message + (isUnderline == 'true' ? '</u>' : '') + (isItalic == 'true' ? '</i>' : '') + (isBold == 'true' ? '</b>' : '');
		}

		var arrTo = to.split('@');
		if(typeof(arrTo[2]) != 'undefined')
		{
			var tmpTo = to.replace('@',' ')
		}
		else
		{
			var tmpTo = to;	
		}

		objJS.sMs(tmpTo,message2,type,(init_conn_type == 'sk' ? '' : 'null'));

		if(type == 'groupchat')
		   Chatroom.windows[to].sendResult(message, isBold, isItalic, isUnderline, fontName, fontSize, fontColor);
		else
		   IM.windows[to].sendResult(message, isBold, isItalic, isUnderline, fontName, fontSize, fontColor);

      if(audioNotify == true) soundManager.play('msg_out');
   },

   sendBuzz: function(to, type) {

		var CNChatCount 	= $("CNmsgCount");
		intChatCountOld 	= parseInt(CNChatCount.value);
		intChatCountNew 	= intChatCountOld+1;
		CNChatCount.value 	= intChatCountNew;
		
		if(intChatCountNew>3)
		{
			alert(Languages.get('sendtoofast'));
			return;
		}
		
		var arrTo = to.split('@');
		if(typeof(arrTo[2]) != 'undefined')
		{
			var tmpTo = to.replace('@',' ')
		}
		else
		{
			var tmpTo = to;	
		}
		objJS.bzz(tmpTo,type);

		var message = 'BUZZ!!!';
		if(type == 'groupchat')
		   Chatroom.windows[to].sendResult(message, 'true', '', '', '', 11, '#FF0000');
		else
		   IM.windows[to].sendResult(message, 'true', '', '', '', 11, '#FF0000');

      if(audioNotify == true) soundManager.play('msg_buzz');
   },

   sendNotifyTyping: function(to, type, chatState) {

		var arrTo = to.split('@');
		if(typeof(arrTo[2]) != 'undefined')
		{
			var tmpTo = to.replace('@',' ')
		}
		else
		{
			var tmpTo = to;	
		}
		objJS.sNot(tmpTo,type,chatState);

   },
   
    onStateInterval: function(to){

		IM._currentState = "paused";		
		// TODO: Should check for the ordering according to the state chart
		if(typeof(IM.windows[to]) != 'undefined')
			IM.windows[to].sendState();
		//alert(IM._currentState + ":" + to);
	},
  
   emoteReplace: function(str, itemsList) {
      var r;
      for(var s in itemsList) {
         if(str.indexOf(s) > -1)
            str = str.replace(new RegExp(regExpEscape(s), 'g'), '<img src="'+STATIC_ZICZAC+'/images/emoticons/' + itemsList[s] + '" alt="' + itemsList[s] + '" title="' + s + '" />');
      }
      return str;
   },

   handleResize: function(eventName, win) {
      if(win.getId() == 'bl') {
         Buddylist.sizeBuddyList();
      } else if(win.getId().indexOf('_im') != -1) {
         var name = win.getId();
         var curIM = $(name + '_rcvd');
         
         curIM.setStyle({height: (win.getSize()['height'] - 103) + 'px',
                         width:  (win.getSize()['width'] - 10) + 'px'});
         
         $(name + '_toolbar').setStyle({top: (win.getSize()['height'] - 73) + 'px',
                                        width: (win.getSize()['width'] - 10) + 'px'});
         
         $(name + '_setFont').setStyle({top: (win.getSize()['height'] - 65) + 'px'});         
         
         $(name + '_setFontSize').setStyle({top: (win.getSize()['height'] - 65) + 'px'});
         
         $(name + '_setFontColor').setStyle({top: (win.getSize()['height'] - 65) + 'px'});
      
         $(name + '_insertEmoticon').setStyle({top: (win.getSize()['height'] - 65) + 'px'});
         
         $(name + '_sendBox').setStyle({top: (win.getSize()['height'] - 45) + 'px',
                                        width: (win.getSize()['width'] - 16) + 'px'});
         
         curIM.scrollTop = curIM.scrollHeight - curIM.clientHeight + 6;
      } else if(win.getId().indexOf('_chat') != -1) {
         Chatroom.handleResize(win.room);
      }
   },
   
   handleClose: function(eventName, win) {
      if(win.getId().indexOf('_im') == -1 && win.getId().indexOf('_chat') == -1) return;
   
      if(typeof(win.room) !== 'undefined') // dung cho conference
	  {
		  Chatroom.leave(win.room);
	  }
	  if(win.getId().indexOf('_chat') > -1)
	  {
			win.getContent().innerHTML='';
			return;
		  
	  }
           
      var rcvdBox = $(win.getId() + '_rcvd');
      if(typeof(win.room) == 'undefined' && imHistory == true) {
         rcvdBox.innerHTML = '<span class="imHistory">' +
                             rcvdBox.innerHTML.replace(new RegExp('\(' + Languages.get('autoreply') + ':\)/g'), Languages.get('autoreply') + ':').replace(/<(?![Bb][Rr] ?\/?)([^>]+)>/ig, '') +
                             "</span>\n";
      } else {
         rcvdBox.innerHTML = '';
      }
   },

   handleMinimize: function(eventName, win) {
      if(win.getId().indexOf('_im') == -1) return;
      
      var curIM = $(win.getId() + '_rcvd');
      curIM.scrollTop = curIM.scrollHeight - curIM.clientHeight + 6;
   },

   createTimestamp: function() {
      Stamp = new Date();
      var tH = String(Stamp.getHours()); var ti = String(Stamp.getMinutes()); var ts = String(Stamp.getSeconds());
      var th = tH > 12 ? tH - 12 : tH; var ta = tH > 12 ? 'pm' : 'am'; var tA = tH > 12 ? 'PM' : 'AM';
      var td = String(Stamp.getDate()); var tm = String(Stamp.getMonth() + 1);
      var tM = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Nov', 'Dec'][tm - 1];
      var tY = String(Stamp.getFullYear()); var ty = tY.substring(2);
      
      tu = tm; tx = td; tQ = tH;
      
      tH = (tH.length > 1) ? tH : "0"+tH; ti = (ti.length > 1) ? ti : "0"+ti;
      tq = (th.length > 1) ? th : "0"+th; ts = (ts.length > 1) ? ts : "0"+ts;
      td = (td.length > 1) ? td : "0"+td; tm = (tm.length > 1) ? tm : "0"+tm;
      return timestamp.replace(/H/, tH).replace(/h/, th).replace(/i/, ti).replace(/s/, ts)
                          .replace(/d/, td).replace(/Y/, tY).replace(/y/, ty).replace(/m/, tm)
                          .replace(/u/, tu).replace(/x/, tx).replace(/Q/, tQ).replace(/q/, tq)
                          .replace(/a/, ta).replace(/A/, tA).replace(/M/, tM);
   }
}


var IMWindow = Class.create();
Object.extend(IMWindow.prototype, Window.prototype);

Object.extend(IMWindow.prototype, {
   setUsername: function(username) {
      this.username = username;
   },

   send: function() {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
   
      var isBold      = (sendBox.style.fontWeight == '400' ? 'false' : 'true');
      var isItalic    = (sendBox.style.fontStyle == 'normal' ? 'false' : 'true');
      var isUnderline = (sendBox.style.textDecoration == 'none' ? 'false' : 'true');
      var fontName    = $(winId + '_setFont').innerHTML;
      var fontSize    = $(winId + '_setFontSize').innerHTML;
      var fontColor   = $(winId + '_setFontColorColor').style.backgroundColor;
      var type    	  = (typeof(this.room) !== 'undefined' ? 'groupchat' : 'chat');

      if(trim(sendBox.value).length > 0) {
         var message = sendBox.value;
		if(message.length > 600)
		{
			message = message.substring(0,600);
		}
         sendBox.value = '';
		 //Truong test
		 IM.sendMessage((type!='chat' ? this.room : this.username), message.replace(/&/g, "&amp;").replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, "<br/>"), type, isBold, isItalic, isUnderline, fontName, fontSize, fontColor);
		 
      }
      
      scrollToBottom(winId + '_rcvd');
      sendBox.focus();
   },

   sendBuzz: function() {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
      var type    	  = (typeof(this.room) !== 'undefined' ? 'groupchat' : 'chat');

	  IM.sendBuzz((type!='chat' ? this.room : this.username), type);
      sendBox.focus();
	  new Effect.Shake(winId);
   },

   sendState: function() {
	  //var winId = this.getId();
     // var sendBox = $(winId + '_sendBox');
      var type    	  = (typeof(this.room) !== 'undefined' ? 'groupchat' : 'chat'); 
	  //Truong test
	//	alert(IM._currentState + ":" + IM._previousState);
		if (IM._currentState != IM._previousState)
		{
			IM._previousState = IM._currentState;	
			IM.sendNotifyTyping((type!='chat' ? this.room : this.username), type, IM._currentState);
			//sendBox.focus();
		}
  
   },
   
   sendResult: function(message, isBold, isItalic, isUnderline, fontName, fontSize, fontColor) {

      var winId = this.getId();

	  message = message.replace(/<br\/>/g, '\n').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/<([^>]+)>/ig, '').replace(/\n/g, '<br/>').replace(/(\s|\n|>|^)(\w+:\/\/[^<\s\n]+)/, '$1<a href="$2" target="_blank">$2</a>');
	  
      message = IM.emoteReplace(message, smilies);
	  
      var rcvdBox = $(winId + '_rcvd');
      Stamp = new Date(); var h = String(Stamp.getHours()); var m = String(Stamp.getMinutes()); var s = String(Stamp.getSeconds());
      h = (h.length > 1) ? h : "0"+h; m = (m.length > 1) ? m : "0"+m;

	  if(winId.search(CONFERENCE_SERVER)==-1) // chat binh thuong
	  {
			//display_name = init_displayname;
			display_name = "me";
	  }
	  else
	  {
		  	var tmpNick = winId.split('/');

			if(tmpNick[1] && tmpNick[1]!='undefined' )
			{
				display_name = joined_room[tmpNick[0]+'_chat'];
			}
			else
			{
				display_name = joined_room[winId];	
			}
	  }
		  
		  // co hien thi. thoi gian chat
		  //rcvdBox.innerHTML = rcvdBox.innerHTML + "<b class=\"userA\">" + IM.createTimestamp() + " " + display_name + ":</b> <span style=\"font-family:" + fontName + ",sans-serif;font-size:" + fontSize + "px;color:" + fontColor + ";\">" + (isBold == 'true' ? "<b>" : "") + (isItalic == 'true' ? "<i>" : "") + (isUnderline == 'true' ? "<u>" : "") + message + (isBold == 'true' ? "</b>" : "") + (isItalic == 'true' ? "</i>" : "") + (isUnderline == 'true' ? "</u>" : "") + "</span><br>\n";
		  
         rcvdBox.innerHTML = rcvdBox.innerHTML + "<span class=\"userA\">" + display_name + ":</span> <span style=\"font-family:" + fontName + ";font-size:" + fontSize + "px;color:" + fontColor + ";\">" + (isBold == 'true' ? "<b>" : "") + (isItalic == 'true' ? "<i>" : "") + (isUnderline == 'true' ? "<u>" : "") + message + (isBold == 'true' ? "</b>" : "") + (isItalic == 'true' ? "</i>" : "") + (isUnderline == 'true' ? "</u>" : "") + "</span><br>\n";

      scrollToBottom(winId + '_rcvd');
   },
   
   toggleBold: function() {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
         
      sendBox.hide(); // horrah weird Opera 9 input refresh!
      if(sendBox.style.fontWeight == '400') {
         $(winId + '_bold').src = ''+STATIC_ZICZAC+'/themes/' + theme + '/window/bold_on.png';
         sendBox.setStyle({fontWeight: '700'});
      } else {
         sendBox.setStyle({fontWeight: '400'});
         $(winId + '_bold').src = ''+STATIC_ZICZAC+'/themes/' + theme + '/window/bold_off.png';
      }
      sendBox.show(); // horrah weird Opera 9 input refresh!
      setTimeout("$('" + winId + "_sendBox').focus();", 125);
   },
      
   toggleItalic: function() {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
      
      sendBox.hide(); // horrah weird Opera 9 input refresh!
      if(sendBox.style.fontStyle == 'normal') {
         sendBox.setStyle({fontStyle: 'italic'});
         $(winId + '_italic').src = ''+STATIC_ZICZAC+'/themes/' + theme + '/window/italic_on.png';
      } else {
         sendBox.setStyle({fontStyle: 'normal'});
         $(winId + '_italic').src = ''+STATIC_ZICZAC+'/themes/' + theme + '/window/italic_off.png';
      }
      sendBox.show(); // horrah weird Opera 9 input refresh!
      setTimeout("$('" + winId + "_sendBox').focus();", 125);
   },
   
   toggleUnderline: function() {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
      
      sendBox.hide(); // horrah weird Opera 9 input refresh!
      if(sendBox.style.textDecoration == 'none') {
         sendBox.setStyle({textDecoration: 'underline'});
         $(winId + '_underline').src = ''+STATIC_ZICZAC+'/themes/' + theme + '/window/underline_on.png';
      } else {
         sendBox.setStyle({textDecoration: 'none'});
         $(winId + '_underline').src = ''+STATIC_ZICZAC+'/themes/' + theme + '/window/underline_off.png';
      }
      sendBox.show(); // horrah weird Opera 9 input refresh!
      setTimeout("$('" + winId + "_sendBox').focus();", 125);
   },

   toggleBuzz: function() {
      this.sendBuzz();
   },
  
   toggleFontList: function() {
      var fL = $('fontsList');
      var fLBtn = $(this.getId() + '_setFont');
      
      $('emoticonList', 'fontColorList', 'fontSizeList').invoke('hide');

      if($('fontsList').style.display == 'block') {
         fL.hide();
      } else {
         fL.setStyle({left:    Position.cumulativeOffset(fLBtn)[0] + 'px',
                      top:     (Position.cumulativeOffset(fLBtn)[1] + Element.getHeight(fLBtn) - 1) + 'px',
                      zIndex:  Windows.maxZIndex + 20,
                      display: 'block'});
                      
         IM.active = this;
      }
   },
   
   toggleFontSizeList: function() {
      var fsL = $('fontSizeList');
      var fsLBtn = $(this.getId() + '_setFontSize');
      
      $('emoticonList', 'fontsList', 'fontColorList').invoke('hide');

      if($('fontSizeList').style.display == 'block') {
         $('fontSizeList').setStyle({display: 'none'});
      } else {
         fsL.setStyle({left:    Position.cumulativeOffset(fsLBtn)[0] + 'px',
                       top:     (Position.cumulativeOffset(fsLBtn)[1] + Element.getHeight(fsLBtn) - 1) + 'px',
                       zIndex:  Windows.maxZIndex + 20,
                       display: 'block'});

       IM.active = this;
      }
   },
   
   toggleEmoticonList: function() {
      var eL = $('emoticonList');
      var eLBtn = $(this.getId() + '_insertEmoticon');
      
      $('fontsList', 'fontSizeList', 'fontColorList').invoke('hide');

      if($('emoticonList').style.display == 'block') {
         $('emoticonList').setStyle({display: 'none'});
      } else {
         eL.setStyle({left:    Position.cumulativeOffset(eLBtn)[0] + 'px',
                      top:     (Position.cumulativeOffset(eLBtn)[1] + Element.getHeight(eLBtn) - 1) + 'px',
                      zIndex:  Windows.maxZIndex + 20,
                      display: 'block'});

         IM.active = this;
      }
   },
   
   toggleFontColorList: function() {
      var fcL = $('fontColorList');
      var fcLBtn = $(this.getId() + '_setFontColor');
      
      $('fontsList', 'fontSizeList', 'emoticonList').invoke('hide');

      if($('fontColorList').style.display == 'block') {
         $('fontColorList').setStyle({display: 'none'});
      } else {
         fcL.setStyle({left:    Position.cumulativeOffset(fcLBtn)[0] + 'px',
                       top:     (Position.cumulativeOffset(fcLBtn)[1] + Element.getHeight(fcLBtn) - 1) + 'px',
                       zIndex:  Windows.maxZIndex + 20,
                       display: 'block'});

         IM.active = this;
      }
   },

   setFont: function(fontname) {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');

      sendBox.hide();
      sendBox.setStyle({fontFamily: fontname + ', sans-serif'});
      sendBox.show();
      
      $(winId + '_setFont').innerHTML = fontname;
      setTimeout("$('" + winId + "_sendBox').focus();", 125);
      this.toggleFontList('');
   },
   
   setFontSize: function(size) {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
      
      sendBox.hide();
      sendBox.setStyle({fontSize: size + 'px'});
      sendBox.show();
      
      $(winId + '_setFontSize').innerHTML = size;
      setTimeout("$('" + winId + "_sendBox').focus();", 125);
      this.toggleFontSizeList('');
   },
   
   setFontColor: function(color) {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
      
      sendBox.setStyle({color: color});
      
      $(winId + '_setFontColorColor').setStyle({backgroundColor: color});
      setTimeout("$('" + winId + "_sendBox').focus();", 125);
      this.toggleFontColorList('');
   },
   
   insertText: function(tti) {
      var winId = this.getId();
      var sendBox = $(winId + '_sendBox');
      
      sendBox.value += tti;
      setTimeout("$('" + winId + "_sendBox').focus();", 125);
      this.toggleEmoticonList();
      return false;
   },

   keyHandler: function(e) {
      var asc = document.all ? event.keyCode : e.which;
      var messageSent = false;
      if(asc == 13) {
         this.send();
		 messageSent = true;
      }
	  clearTimeout(IM._stateTimer);		
	  IM._stateTimer = setTimeout("IM.onStateInterval('" + this.username + "')", IM.STATE_INTERVAL*1000);
	  
		if (IM.SEND_STATES && !messageSent)
		{
			IM._currentState = "composing";
			this.sendState();
		}
		
		if(asc == 13) {
         return false;
      }
      
      return true;
   }
   
});
