// Chatroom Class
var Chatroom = {
   windows: {},

   create: function(name, imTitle) {
      var imLeft = Math.round(Math.random()*(Browser.width()-360))+'px';
      var imTop  = Math.round(Math.random()*(Browser.height()-400))+'px';
   
      var winId = name + '_chat';

	  imTitle = chatnhe_get_username(imTitle);

      this.windows[name] = new ChatWindow({id: winId, className: "dialog", width: 475, height: 340, top: imTop, left: imLeft,
                                                                   resizable: true, title: imTitle, draggable: true, detachable: false, minWidth: 475, 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="chatUserList" id="' + winId + '_userlist"><ul id="' + winId + '_ul" class="sortable box"><li style="display:none"></li></ul></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="Chatroom.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="Chatroom.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="Chatroom.windows[\'' + name + '\'].toggleUnderline();" onmousedown="return false;" alt="' + Languages.get('underline') + '" id="' + winId + '_underline" /></div>' +
                                                   ' <a href="#" class="setFontLink" id="' + winId + '_setFont" onclick="Chatroom.windows[\'' + name + '\'].toggleFontList();return false;" onselectstart="return false;">Tahoma</a>' +
                                                   ' <a href="#" class="setFontSizeLink" id="' + winId + '_setFontSize" onclick="Chatroom.windows[\'' + name + '\'].toggleFontSizeList();return false;" onselectstart="return false;">11</a>' +
                                                   ' <a href="#" class="setFontColorLink" id="' + winId + '_setFontColor" onclick="Chatroom.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="Chatroom.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);" onkeypress="return Chatroom.windows[\'' + name + '\'].keyHandler(event);"></textarea></div>';
      
      this.windows[name].setRoom(name);
      
      $(winId + '_userlist').setStyle({left:   (this.windows[name].getSize().width - 155) + 'px',
                                       height: (this.windows[name].getSize().height - 12) + 'px'});
      
      $(winId + '_rcvd').setStyle({marginTop: '5px',
                                   height:    (this.windows[name].getSize().height - 103) + 'px',
                                   width:     (this.windows[name].getSize().width - 170) + 'px'});
   
      $(winId + '_toolbar').setStyle({top:   (this.windows[name].getSize().height - 73) + 'px',
                                      width: (this.windows[name].getSize().width - 170) + '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'});
   
      var sendBox = $(winId + '_sendBox');
      sendBox.setStyle({top:            (this.windows[name].getSize().height - 45) + 'px',
                        left:           '2px',
                        width:          (this.windows[name].getSize().width - 175) + 'px',
                        fontWeight:     '400',
                        fontStyle:      'normal',
                        textDecoration: 'none'});
   
      this.windows[name].show();
      this.windows[name].toFront();
      Windows.focusedWindow = this.windows[name];
      setTimeout("$('"+winId+"_sendBox').focus();", 250);
   },
   
   handleResize: function(name) {
      var winId = this.windows[name].getId();

      $(winId + '_userlist').setStyle({left:   (this.windows[name].getSize().width - 155) + 'px',
                                       height: (this.windows[name].getSize().height - 12) + 'px'});
      
      $(winId + '_rcvd').setStyle({height: (this.windows[name].getSize().height - 103) + 'px',
                                   width:  (this.windows[name].getSize().width - 170) + 'px'});
   
      $(winId + '_toolbar').setStyle({top:   (this.windows[name].getSize().height - 73) + 'px',
                                      width: (this.windows[name].getSize().width - 170) + '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 - 175) + 'px'});
   },

   join: function(room_name) {
    	room_name = room_name.toLowerCase();
		nick_name = $('nicknameInRoom').value;
		
		if(joined_room[room_name+'_chat'])
		{
			$('newroom_error_msg').innerHTML = Languages.get('alreadyInRoom').replace('%1', room_name);
			return;
		}
		else
		{
			var xhConn = new XHConn();
			xhConn.connect(sv2, "POST", "call=joinroom&nick_name=" + nick_name + "&room_name=" + room_name + "&username=" + user,
				function(xh) 
			{});
		}
   },

   leave: function(room_name) {
		nick_name = joined_room[room_name+'_chat'];
		var xhConn = new XHConn();
		xhConn.connect(sv2, "POST", "call=exitroom&nick_name=" + nick_name + "&room_name=" + room_name + "&username=" + user,
		function(xh) 
		{
			joined_room[room_name+'_chat'] = '';
			Chatroom.windows[room_name].destroy();
		});
   }
};




// Chatroom Window Class
var ChatWindow = Class.create();
Object.extend(ChatWindow.prototype, IMWindow.prototype);

Object.extend(ChatWindow.prototype, {
   curSelected: '',

   setRoom: function(name) {
      this.room = name;
   },
   
   addUser: function(username, role, affiliation) {
	   var user_icon;
	   if(role=='moderator' && affiliation=='owner')
	   {
		   user_icon = ''+STATIC_ZICZAC+'/themes/icons/owner.png';
	   }
	   else
	   {
	 	   user_icon = ''+STATIC_ZICZAC+'/themes/icons/guess.png';
	   }
	   var room_name = this.room+'_chat';
      $(this.getId() + '_ul').innerHTML += '<li id="'+username+'_'+this.room+'_chatUser" class="buddy" onmousedown="Chatroom.windows[\'' + this.room + '\'].clickUser(\''+username+'\');return false;" onselectstart="return false;" onmouseover="Chatroom.windows[\'' + this.room + '\'].selectUser(this, \''+username+'\', true);" onmouseout="Chatroom.windows[\'' + this.room + '\'].selectUser(this, \''+username+'\', false);" '+(username!=joined_room[room_name] ? 'ondblclick="Chatroom.windows[\'' + this.room + '\'].onUserDblClick(\''+this.room+'\');"' : '')+' style="padding:0px;"><img src="' + user_icon + '" alt="" id="'+username+'_'+this.room+'_chatImg" />&nbsp;'+username+'</li>';
      $(username+'_'+this.room+'_chatUser').setStyle({listStyleType: 'none'});
   },
   
   deleteUser: function(username) {
      var toDelete = $(username + '_' + this.room + '_chatUser');
      if(typeof(toDelete) !== 'undefined')
         toDelete.parentNode.removeChild(toDelete);
   },
   
   selectUser: function(sel, username, selected) {
      if(selected === false) {
         if(this.curSelected != username) {
            try {
               Element.addClassName(sel, 'listNotSelected');
               Element.removeClassName(sel, 'listSelected');
               Element.removeClassName(sel, 'listHover');
            } catch(e) { }
         } else {
            Element.addClassName(sel, 'listSelected');
            Element.removeClassName(sel, 'listNotSelected');
            Element.removeClassName(sel, 'listHover');
         }
      } else {
         Element.addClassName(sel, 'listHover');
         Element.removeClassName(sel, 'listSelected');
         Element.removeClassName(sel, 'listNotSelected');
      }
   },

   clickUser: function(username) {
      if(this.curSelected.length > 0) {
         try {
            var el = $(this.curSelected + '_' + this.room + '_chatUser');
            Element.addClassName(el, 'listNotSelected');
            Element.removeClassName(el, 'listSelected');
            Element.removeClassName(el, 'listHover');
         } catch(e) { }
      }
      
      this.curSelected = username;
      
      var oel = $(this.curSelected + '_' + this.room + '_chatUser');
      Element.addClassName(oel, 'listSelected');
      Element.removeClassName(oel, 'listNotSelected');
      Element.removeClassName(oel, 'listHover');
   },

   onUserDblClick: function(room_name) {
      if(this.curSelected.length > 0) {
         if(typeof(IM.windows[this.curSelected]) == 'undefined') {
            IM.create(room_name+'/'+this.curSelected, this.curSelected, 'conference');
         } else {
            if(!IM.windows[this.curSelected].isVisible()) {
               IM.windows[this.curSelected].show();
               IM.windows[this.curSelected].toFront();
               setTimeout("scrollToBottom('" + IM.windows[this.curSelected].getId() + "_rcvd')", 125);
               setTimeout("$('" + IM.windows[this.curSelected].getId() + "_sendBox').focus();", 250);
            } else {
               IM.windows[this.curSelected].toFront();
               setTimeout("$('" + IM.windows[this.curSelected].getId() + "_sendBox').focus();", 250);
            }
         }
      }
   }
});