function chat_init(cod,cod_user,max_chars,last_id,temp_dir){
	var chat=$("#chat"+cod).get(0);
	var $txt=$(chat).find("textarea[name=message]");
	var txt=$txt.get(0);
	chat.cod=cod;
	chat.cod_user=cod_user;
	chat.temp_dir=temp_dir;
	chat.txt=txt;
	chat.incoming=0;
	chat.ignore_next=0;
	window.hasFocus=true;
	chat.messages=$(chat).find("table.chat_messages").get(0);
	chat.users=$(chat).find("table.chat_users").get(0);
	chat.last_send=0;
	chat.last_id=last_id==null ? 0 : last_id;
	chat.last_id_in_file=0;
	chat.interval=chat.min_interval=1000;
	chat.last_date=0;
	chat.doc_title=document.title;
	chat.titleID=0;
	if(nav==1){
		var active_elem=document.activeElement;
		document.onfocusout=function(){
			if(document.activeElement==active_elem){
				window.hasFocus=false;
			}else{
				active_elem=document.activeElement
			}
		}
	}else{
		addEvent(window,'blur',function(){
			window.hasFocus=false;
		});
	}
	addEvent(window,'focus',function(){
		window.hasFocus=true;
		chat.interval=chat.min_interval;
		chat_alert(chat,0);
	});
	txt.uc=function(){
		setTimeout(function(){
			if(txt.value.replace(/\r\n/g,'\n').replace(/\n/g,'\r\n').length>max_chars){
				var caret=Math.min(getSelStart(txt),max_chars);
				var scroll=txt.scrollTop;
				txt.value=txt.value.replace(/\r\n/g,'\n').replace(/\n/g,'\r\n').substr(0,max_chars);
				setSelRange(txt,caret,caret);
				txt.scrollTop=scroll;
			}
		},1);
	}
	txt.tabString=null;
	txt.tabCount=0;
	txt.historyPos=-1;
	txt.history=[];
	txt[jQuery.browser.mozilla ? 'onkeypress' : 'onkeydown']=function(evt){
		var e=evt ? evt : event;
		if(!e.shiftKey && e.keyCode==13){
			this.history.splice(0,0,this.value);
			txt.historyPos=-1;
			chat_send(chat);
			return false;
		}else if(e.keyCode==9){
			var caret=getSelEnd(this);
			var str='',c='';
			for(var i=caret-1;i>=0;i--){
				c=this.value.charAt(i);
				if(c.match(/[\s,\r\n@]/)){
					break;
				}
				str=c+str;
			}
			if(txt.tabString==null){
				this.tabString=str;
			}
			var list=chat.users.rows;
			var preg=new RegExp('^'+this.tabString.regQuote(),'i');
			var arr=[],username='';
			for(var i=0;i<list.length;i++){
				username=$(list[i].cells[0].firstChild).text();
				if(username.match(preg)){
					arr.push(username);
				}
			}
			if(arr.length>0){
				username=arr[txt.tabCount%arr.length];
				this.value=this.value.substr(0,caret-str.length)+username+this.value.substr(caret);
				setSelRange(this,caret-str.length+username.length,caret-str.length+username.length);
				txt.tabCount++;
			}
			txt.historyPos=-1;
			return false;
		}else if(e.keyCode==38){
			if(this.history.length>this.historyPos+1 && !this.value.substr(0,getSelStart(this)).match(/[\r\n]/)){
				if(this.historyPos==-1){
					this.currentText=this.value;
				}
				this.value=this.history[++this.historyPos];
				return false;
			}
			return true;
		}else if(e.keyCode==40){
			if(this.historyPos>=0 && !this.value.substr(0,getSelEnd(this)).match(/[\r\n]/)){
				if(--this.historyPos>=0){
					this.value=this.history[this.historyPos];
				}else{
					this.value=this.currentText;
				}
				return false;
			}
			return true;
		}else if(e.keyCode==37 || e.keyCode==39){
			return true;
		}
		this.historyPos=-1;
		this.tabString=null;
		this.tabCount=0;
	}
	$txt.bind('input',function(){this.uc()}).bind('keydown',function(){this.uc()}).bind('paste',function(){this.uc()}).bind('focus',function(){chat.interval=chat.min_interval});
	$(chat).find("input[name=btn_send]").click(function(){
		chat_send(chat);
	})
	chat_sent_offline=0;
	$(window).bind('beforeunload',chat_offline).bind('unload',chat_offline);
	sendAndLoad('?chat_online=1');
	chat.count=0;
	chatintID=setInterval(function(){
		chat.count+=1000;
		if(chat.count>=chat.interval){
			chat.count=0;
			chat.interval=Math.min(window.hasFocus ? 20000 : 60000,chat.interval*1.2);
			sendAndLoad(temp_dir+cod,function(value){
				if(value!=chat.last_id){
					chat.last_id_in_file=value;
					chat_get_messages(chat,'');
				}
			})
			sendAndLoad(temp_dir+'u'+cod,function(value){
				if(value!=chat.last_uts){
					chat.last_uts=value;
					chat_get_users(chat);
				}
			},'HEAD','Last-Modified');
		}
	},1000);
	setInterval(function(){
		sendAndLoad('?chat_online=1');
	},60000);
	chat_get_messages(chat,'',1);
}

function chat_alert(chat,status){
	clearInterval(chat.titleID);
	if(status){
		chat.titleID=setInterval(function(){
			document.title=document.title=='--------------------' ? chat.doc_title : '--------------------';
		},500);
	}else{
		document.title=chat.doc_title;
	}
}

function chat_offline(){
	if(!chat_sent_offline){
		chat_sent_offline=1;
		sendAndLoad('?chat_offline=1');
	}
}

function chat_send(chat){
	if(chat.last_send+500<(new Date()).getTime()){
		chat.interval=chat.min_interval;
		chat.last_send=(new Date()).getTime();
		chat_get_messages(chat,chat.txt.value.trim());
		chat.txt.value='';
	}
}

function chat_get_messages(chat,text,first){
	if(chat.incoming){
		chat.ignore_next=1;
	}
	chat.incoming=1;
	sendAndLoad("?chat_last_id="+chat.last_id+"?chat_message="+escape(text),function(value){
		if(value===false){
			chat_logout();
		}else{
			if(!chat.ignore_next){
				if(value.length>0){
					chat_set_messages(chat,value,first);
				}else{
					chat.last_id=chat.last_id_in_file;
				}
				chat.incoming=0;
			}
			chat.ignore_next=0;
		}
	},'POST',2);
}

function chat_get_users(chat,first){
	sendAndLoad(chat.temp_dir+'u'+chat.cod,function(value){
		if(value===false){
			chat_logout();
		}else{
			chat_set_users(chat,value,first);
		}
	},'GET',2);
}

function chat_set_messages(chat,data,first){
	var tbl=chat.messages;
	var last_user=tbl.rows.length>0 ? tbl.rows[tbl.rows.length-1].cod_user : 0;
	var exScrollTop=tbl.parentNode.scrollTop;
	tbl.parentNode.scrollTop=tbl.parentNode.scrollHeight;
	var tobescrolled=exScrollTop==tbl.parentNode.scrollTop;
	tbl.parentNode.scrollTop=exScrollTop;
	var new_messages=0;
	for(var i=0;i<data.length;i++){
		var d=data[i];
		if(d.cod>chat.last_id){
			new_messages=1;
			var row=tbl.insertRow(tbl.rows.length);
			row.cod_user=d.cod_user;
			var cell=row.insertCell(-1);
			if(last_user!=d.cod_user){
				row.hasText=true;
				cell.width='1%';
				cell.innerHTML=d.url;
				if(last_user!=0){
					row.previousSibling.className='chat_end';
				}
				cell.style.cursor='pointer';
				cell.onclick=function(){
					var r=this.parentNode;
					while(!r.hasText){
						r=r.previousSibling;
					}
					var caret=getSelStart(chat.txt),text=$(r.cells[0]).text()+' ';
					chat.txt.value=chat.txt.value.substr(0,caret)+text+chat.txt.value.substr(caret);
					setSelRange(chat.txt,caret+text.length,caret+text.length);
					chat.txt.focus();
				}
			}else{
				row.hasText=false;
				cell.innerHTML='&nbsp;';
			}
			var cell=row.insertCell(-1);
			cell.innerHTML=d.text;
			var cell=row.insertCell(-1);
			d.date=d.date.replace(/\:\d{2}$/,'');
			cell.innerHTML=chat.last_date==d.date ? '&nbsp;' : d.date;
			chat.last_date=d.date;
			cell.className='text8';
			cell.align='right';
			chat.last_id=d.cod;
			last_user=d.cod_user;
			if(!first){
				row.bgColor='#eeeeab';
				backgroundTrans(row,'#ffffff',3000,'bgColor',function(obj){obj.bgColor=''});
			}else{
				$(row).children().css('opacity',.5);
			}
		}
	}
	while(tbl.rows.length>500){
		tbl.deleteRow(0);
	}
	if(new_messages && !window.hasFocus){
		chat_alert(chat,1);
	}
	if(data.length && tobescrolled){
		tbl.parentNode.scrollTop=tbl.parentNode.scrollHeight;
	}
	if(first){
		chat_get_users(chat,1);
	}
}

function chat_set_users(chat,data,first){
	var tbl=chat.users;
	var exScrollTop=tbl.parentNode.scrollTop;
	tbl.parentNode.scrollTop=tbl.parentNode.scrollHeight;
	var tobescrolled=exScrollTop==tbl.parentNode.scrollTop;
	tbl.parentNode.scrollTop=exScrollTop;
	if(data[chat.cod_user]==null){
		sendAndLoad('?chat_online=1');
	}
	var total=0;
	for(var i in data){
		total++;
	}
	getE('chat_no_users'+chat.cod).innerHTML=total;
	for(var i=tbl.rows.length-1;i>=0;i--){
		if(data[tbl.rows[i].cod]==null){
			for(var j=0;j<chat.messages.rows.length;j++){
				if(chat.messages.rows[j].cod_user==tbl.rows[i].cod){
					$(chat.messages.rows[j]).children().css('opacity',.5);
				}
			}
			tbl.deleteRow(i);
		}else{
			tbl.rows[i].cells[0].innerHTML=data[tbl.rows[i].cod];
			delete data[tbl.rows[i].cod];
		}
	}
	for(var i in data){
		var row=tbl.insertRow(tbl.rows.length);
		row.cod=i;
		var cell=row.insertCell(-1);
		cell.innerHTML=data[i];
		for(var j=0;j<chat.messages.rows.length;j++){
			if(chat.messages.rows[j].cod_user==i){
				$(chat.messages.rows[j]).children().css('opacity',1);
			}
		}
		if(!first){
			cell.bgColor='#eeeeab';
			backgroundTrans(cell,'#ffffff',3000,'bgColor',function(obj){obj.bgColor=''});
		}
	}
	if(data.length && tobescrolled){
		tbl.parentNode.scrollTop=tbl.parentNode.scrollHeight;
	}
}

function chat_logout(){
	//document.location.href=document.location.href;
}