  var openChatWindows = new Hashtable();
  function createChatWindow(id, addpartner, divtitle, status) {
    //try {
    var divId = "chat_"+id;

    if (!document.getElementById(divId)) {
      if (openChatWindows.getSize()<3) {
      var maindiv = document.createElement('div');
      maindiv.setAttribute('id', divId);
      maindiv.className = 'chatdiv';
      maindiv.style.right = (openChatWindows.getSize()*235+5)+"px";
      //maindiv.style.visibility = "visible";
      document.getElementById("chatbar").appendChild(maindiv);
      openChatWindows.put(id, divId);
      
      var headerdiv = document.createElement('div');
      headerdiv.className = 'chatheader';
      maindiv.appendChild(headerdiv);

      var titlediv = document.createElement('div');
      titlediv.className = 'chattitle';
      titlediv.setAttribute('id', divId+'_title');
      titlediv.innerHTML = divtitle;
      var closediv = document.createElement('div');
      if (closediv.attachEvent) {
        closediv.attachEvent('onclick', function(event){removeChatPartner(id, event)}, false);
      } else {
        closediv.addEventListener('click', function(event){removeChatPartner(id, event)}, false);
      }
      closediv.className = 'chatclose';

      headerdiv.appendChild(titlediv);
      headerdiv.appendChild(closediv);

      var newdiv = document.createElement('div');
      newdiv.className = 'chatcontent';
      maindiv.appendChild(newdiv);
      
      var statuspanel = document.createElement('div');
      statuspanel.setAttribute('id', divId+'_status');
      statuspanel.className = 'chatstatus';
      statuspanel.innerHTML = '';
      newdiv.appendChild(statuspanel);
      var infopanel = document.createElement('div');
      infopanel.setAttribute('id', divId+'_info');
      infopanel.className = 'chatinfo';
      infopanel.innerHTML = '';
      newdiv.appendChild(infopanel);
      
      var newpanel = document.createElement('div');
      newpanel.setAttribute('id', divId+'_panel');
      newpanel.className = 'chatlist';
      newpanel.innerHTML = '';
      newdiv.appendChild(newpanel);
  
      var newinput = document.createElement('input');
      newinput.setAttribute('id', divId+'_msg');
      newinput.setAttribute('type', 'text');
      newinput.setAttribute('value', '');
      newinput.className = 'chatinput';
      if (newinput.attachEvent) {
        newinput.attachEvent('onkeypress', function(event){stopAnimeTitle(divId);onEnter(id, event)}, false);
        newinput.attachEvent('onclick', function(){stopAnimeTitle(divId)}, false);
      } else {
        newinput.addEventListener('keypress', function(event){stopAnimeTitle(divId);onEnter(id, event)}, false);
        newinput.addEventListener('click', function(){stopAnimeTitle(divId)}, false);
      }
      newdiv.appendChild(newinput);
  
        if (addpartner) { 
          addChatPartner(id, divtitle);
          newinput.focus();
        }
      }
    } else {
      document.getElementById(divId+'_msg').focus();
    }
    
    //chatWindowsArray[chatWindowsArray.length] = divId;
    //} catch(e) {alert(e)}
  }

  function createMessageLine(divid, msg, username, ctime) {
    var father = document.getElementById(divid+'_panel');
    var lineDiv = document.createElement('div');
    lineDiv.className = 'chatline';
    father.appendChild(lineDiv);
    
	  if (username) {
      var usernameDiv = document.createElement('div');
      usernameDiv.className = 'username';
      usernameDiv.innerHTML = username;
      lineDiv.appendChild(usernameDiv);
      var ctimeDiv = document.createElement('div');
      ctimeDiv.className = 'ctime';
      ctimeDiv.innerHTML = ctime;
      lineDiv.appendChild(ctimeDiv);
	  }
	  
    var msgDiv = document.createElement('div');
    msgDiv.className = 'msg';
    msgDiv.innerHTML = msg;
    lineDiv.appendChild(msgDiv);
    return;
  }
  
  function onEnter(pId, evt) {
    if (evt.keyCode == 13) {
      addChatMessage(pId);
    }
  }
  
  function addChatPartner(partnerId, partnerName) {
    if (xmlhttp(8)!=null) {
      cancelRefreshChat();
      xmlhttp(8).onreadystatechange=addChatPartnerDone
      xmlhttp(8).open("GET",'ajaxChatWindows.do?partnerId='+partnerId+'&partnerName='+partnerName+'&nokess='+Math.ceil(100000*Math.random())+'&useraction=', true);
      xmlhttp(8).send(null);
    } else {
    }
    
    return;
  }
  function removeChatPartner(id) {
    openChatWindows.remove(id);
    document.getElementById("chatbar").removeChild(document.getElementById("chat_"+id));
    if (xmlhttp(8)!=null) {
      xmlhttp(8).onreadystatechange=removeChatPartnerDone
      xmlhttp(8).open("GET",'ajaxChatWindows.do?partnerId='+id+'&remove=true&nokess='+Math.ceil(100000*Math.random())+'&useraction=', true);
      xmlhttp(8).send(null);
    } else {
    }
   
    var winIds = openChatWindows.getKeys();
    var winDiv = null;
    for (var k=0; k<winIds.length; k++) {
      winDiv = document.getElementById("chat_"+winIds[k]);
      winDiv.style.right = (k*235+5)+"px";
    }
    return;
  }

  function addChatMessageDone() {
    if(checkReadyState(8)) {
      resetState(8);
      try {
        refreshChatMessages();
      } catch(e) {
      }
    }
  }
  function addChatPartnerDone() {
    if(checkReadyState(8)) {
      resetState(8);
    
      try {
        refreshChatMessages('new');
      } catch(e) {
        debug(e)
      }
    }
  }

  function removeChatPartnerDone() {
    if(checkReadyState(8)) {
      resetState(8);
      try {
        
      } catch(e) {
      }
     }
  }
  function addChatMessage(pId) {
    if (xmlhttp(8)!=null) {
      cancelRefreshChat();
      var msg = document.getElementById("chat_"+pId+"_msg");
      var params = 'partnerId='+pId+'&msg='+encodeURIComponent(msg.value)+'&nokess='+Math.ceil(100000*Math.random())+'&useraction=';
      xmlhttp(8).onreadystatechange=addChatMessageDone
      xmlhttp(8).open("POST","ajaxAddChatMessage.do",true);
      xmlhttp(8).setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xmlhttp(8).setRequestHeader("Content-length", params.length);
      xmlhttp(8).setRequestHeader("Connection", "close");

      xmlhttp(8).send(params);
    } else {
      //a checkframe-el meg kell oldani ha nincs ajax.
    }
    msg.value = '';
    return;
  }
  
  var continueRefreshChat = true;
  function loadChatMessages(option) {
    if (xmlhttp(9)!=null) {
      var params = (option?'option='+option+'&':'')+'nokess='+Math.ceil(100000*Math.random());
      xmlhttp(9).onreadystatechange=setChatMessages
      xmlhttp(9).open("POST","ajaxChatMessages.do",true);

      xmlhttp(9).setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xmlhttp(9).setRequestHeader("Content-length", params.length);
      xmlhttp(9).setRequestHeader("Connection", "close");

      xmlhttp(9).send(params);
    } else {
      //a checkframe-el meg kell oldani ha nincs ajax.
    }
    return;
  }

  function setChatMessages() {
    if(checkReadyState(9)) {
      try {
        var chatlist = xmlhttp(9).responseXML.documentElement.getElementsByTagName("chatlist")[0];
        if (chatlist) {
	        var chatwindows = chatlist.getElementsByTagName('chatwindow');
	        if (chatwindows) {
	          for (var j=0; j < chatwindows.length; j++) {
	            var partnerid = getNodeValue(chatwindows[j],'partnerid');
	            var divid = 'chat_'+partnerid;
              var partnername = getNodeValue(chatwindows[j],'partnername');
              var newmessage = getNodeValue(chatwindows[j],'newmessage');
              
              var partnerstatus = getNodeValue(chatwindows[j],'partnerstatus');
              var partnerinfo = getNodeValue(chatwindows[j],'partnerinfo');
	            var list = chatwindows[j].getElementsByTagName('chatmessage');
	            if (list&&list.length>0) {
	              if (!document.getElementById(divid)) {
	                createChatWindow(partnerid, false, partnername);
	              }
	              if (partnerid&&document.getElementById(divid)) {
                  
		              if (partnerstatus) {
                    document.getElementById(divid+'_status').innerHTML = '<img style="position:relative;top:2px;left:2px;" src="img/'+partnerstatus+'.gif" border="0" >';
                    document.getElementById(divid+'_info').innerHTML = ''+partnerinfo;
		              }
		              for (var i=0; i < list.length; i++) {
		                var ctime = getNodeValue(list[i],'ctime');
		                var username = getNodeValue(list[i],'username');
		                var msg = getNodeValue(list[i],'msg');
		                createMessageLine(divid, msg, username, ctime);
		                
		              }
		            }
	              document.getElementById(divid+'_panel').scrollTop = document.getElementById(divid+'_panel').scrollHeight;
	            } else {
	              if (partnerid&&document.getElementById(divid)) {
                  
	                if (partnerstatus) {
                    document.getElementById(divid+'_status').innerHTML = '<img style="position:relative;top:2px;left:2px;" src="img/'+partnerstatus+'.gif" border="0" >';
                    document.getElementById(divid+'_info').innerHTML = ''+partnerinfo;
	                }
	              }
	            }
	            if (newmessage) {
                startAnimeTitle(divid);
              }           
	          }
	        }
        }
        resetState(9);
      } catch(e) {
        debug("c "+e)
        continueRefreshChat = false;
      }
    }
    
  }
  var refreshChat = null;
  function refreshChatMessages(option) {
    loadChatMessages(option);
    if (continueRefreshChat) refreshChat = setTimeout('refreshChatMessages()', 5000);
  }  
  window.onload = function(){refreshChatMessages('all')};

  function cancelRefreshChat() {
    if (refreshChat) {
      window.clearTimeout(refreshChat);
      refreshChat = null;
    }
  }
  
  var titleAnimation = new Hashtable();
  var at;
  function startAnimeTitle(divid) {
    if (at) window.clearTimeout(at);
    titleAnimation.put(divid, document.getElementById(divid+'_title').innerHTML);
    document.getElementById(divid+'_title').innerHTML = "* "+document.getElementById(divid+'_title').innerHTML;
    animeTitle();
    return;
  }
  function animeTitle() {
    if (chatTitle==document.title) {
      setTitle(titleAnimation.getValues()[0]+' chat üzenetet küldött...');
      for (var j=1; j < titleAnimation.getSize(); j++) {
        setTimeout('eval(setTitle(\''+titleAnimation.getValues()[j]+' chat üzenetet küldött...\'))', 1000);
      }
    } else {
      setTitle(chatTitle);
    }
    at = setTimeout('animeTitle()', 2000);
  }
  function stopAnimeTitle(divid) {
    if (at) {
      var ttl = titleAnimation.get(divid);
      document.getElementById(divid+'_title').innerHTML = ttl;
      titleAnimation.remove(divid);
      if (titleAnimation.isEmpty()) {
	      clearTimeout(at);
        setTitle(chatTitle);
	      at = null;
      }
    }
  }
  function isTitleAnimated() {
    return at!=null;
  }
  
