var d=document,r=null,rbusy=false,z=0,e=20000,timeout=120000,cb_req_cnt=new Array(),
pending=new Object(),sent=new Object(),cbs=new Object(),nextid=0,sw=false,sto=null,nst=0;
function loadXMLDoc(url, post) {
    if(window.XMLHttpRequest)try{r=new XMLHttpRequest();}catch(e){}
    if(!r && window.ActiveXObject)try{r=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
    if(!r && window.ActiveXObject)try {r=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
	if(r){
		rbusy=true;r.onreadystatechange=doup;
		r.open("POST",url,true);
		r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
		r.send(post);
}}
function b(p){
	var f=d.getElementById('chatbox_'+p),m=f.scrollHeight,n=f.clientHeight;
	if(m-(f.scrollTop+n)>100)f.scrollTop=99999;
	else f.scrollTop+=2;if(f.scrollTop+n<m)setTimeout('b('+p+')', 20)
}
function x(u){	
	var j=u.indexOf('>'),content=u.substring(j+1),
		hist=(u.match(/hist\s*=\s*["'](.*?)["']/)[1]=='true'),
		high=u.match(/high\s*=\s*["'](.*?)["']/)[1],
		p=u.match(/pageid\s*=\s*["'](.*?)["']/)[1],
		f=d.getElementById('chatbox_'+p),l=d.getElementById('chatbox_offline_'+p),
		n=(f.scrollTop+f.clientHeight>=f.scrollHeight);
	cb_req_cnt[p]--;
	f.style.display='block';l.style.display='none';
	if(hist){f.innerHTML=content+f.innerHTML;d.getElementById('chatbox_hist_'+p).style.display='none'}
	else{f.fromid=high;f.innerHTML+=content}if(n)b(p);
	if (cb_req_cnt[p]>0)return;
	send(-1, "chatbox", "pageid='"+p+"' cbid='"+f.cbid+"' fromid='"+f.fromid+"'", x);
	cb_req_cnt[p]++;
	if (e>=timeout){f.style.display='none';l.style.display='block';}
}
function update_ol(u) {
	var c=u.indexOf('>'),ct=u.substring(c+1),k=d.getElementById('ol');
	if(k)k.innerHTML=ct;
	send(-2,"whos_online","",update_ol);
}
function doup(){
    if (!r.readyState||r.readyState!=4)return;
    if (r.status!=200){
		for (var i in sent)pending[i]=sent[i];
		sent=null;rbusy=false;dosend();
	}
	var ud=r.responseText.split("</ud>");
	for (var i=0;i<ud.length;i++){
		if (!(u=ud[i]))continue;
		var j=u.indexOf('>'),param=u.substring(0,j),
			mod=param.match(/m\s*=\s*["'](.*?)["']/)
		if (mod)mod=mod[1];
		var cb=cbs['p'+mod];
		if (cb)cb.call(window, u);
		rbusy=false;
		if (sw)dosend();
	}
	rbusy=false;
}
function g(post){loadXMLDoc(updatephp,post);}
function dosend(){
	if (sto){clearTimeout(sto);sto=null;nst=0;}
	e+=e/10;if(e>timeout)e=86400000;
	nst=0;
	var pend=false;for(var i in pending){pend=true; break;}
	if (!pend)return;
	if (rbusy){sw=true;return;}
	sw=false;
	var msg="msg=";
	for(var i in pending)msg+=pending[i]+"\r\n";
	sent=pending;pending=new Object();
	g(msg);
}
function send(sc, component, params, cb, body){
	var id=++nextid,
		r=escape("<ud id='"+id+"' m='"+component+"' "+params+">"+body+"</ud>");
	pending["p"+id]=r;
	if(cb)cbs["p"+id]=cb;
	if(sc==-1)sc=e;
	if(sc==-2)sc=86400000;
	if(sc<=100)e=5000;
	if(!sc)dosend(); 
	else{
		var now=new Date().getTime(); 
		if (sto){
			if (nst==0||nst>(now+sc))
			{nst=now+sc;clearTimeout(sto);sto=setTimeout('dosend()',sc);}
		}else{nst=now+sc;sto=setTimeout('dosend()',sc);}
	}
	return id;
}
