window.addEvent('domready', function(){
				if (chat_url)
				{
					chatTimer=setInterval(function(){lc_chat.updateChatData();},5000);
					chatTimer=setInterval(function(){lc_chat.getOnliners();},50000);
				}
			});


lc_chat={
	last_id:0,
	request:false,
	limit:200,
	current_text:'',
	send_msg:function(form)
	{
		if (lc_chat.request) return false;
		if ($('msg_input').value=='') return false;
		data=form.toQueryString()+'&id='+lc_chat.last_id+'&action=post';
		lc_chat.current_text=$('msg_input').value;
		$('msg_input').value='';
		while(lc_chat.request){}
		lc_chat.request=true;
		new Request.JSON({
				url:chat_url,
				method: 'post',
				onSuccess:function(result){
						if (lc_chat.checkResponse(result))
						{
							lc_chat.setPosts(result.posts);
							$('msg_input').set('value', '').focus();
						}
					},
				onComplete:function(){lc_chat.request=false;}
				}).send(data);
		return false;
	},

	updateChatData:function()
	{
		if (lc_chat.request) return false;
		lc_chat.request=true;
		new Request.JSON({
				url:chat_url,
				method: 'post',
				onSuccess:function(result){
						if (lc_chat.checkResponse(result))
						{
							lc_chat.setPosts(result.posts);
						}
					},
				onComplete:function(){lc_chat.request=false;}
				}).send('id='+lc_chat.last_id+'&action=get&token='+token);
	},
	
	getOnliners:function()
	{
		new Request.JSON({
				url:chat_url,
				method: 'post',
				onSuccess:function(result){
						if (lc_chat.checkResponse(result))
						{
							lc_chat.setOnliners(result.onliners);
						}
					}
				}).send('action=getOnliners&token='+token);
	},

	checkResponse:function(result)
	{
		if (result.error)
		{
			alert(result.error);
			$('msg_input').value=lc_chat.current_text;
			return false;
		}
		return true;
	},
	
	setOnliners:function(users)
	{
		$('onliners').set('html', '');
		$each(users, function(value, key){
			new Element('a', {href:chat_user_link.replace(/_replace_/, value.user_id), html:value.login, target:'_blank'}).inject($('onliners'));
		});
	},

	setPosts:function(posts)
	{
		$each(posts, function(value, key){lc_chat.setPost(value)});
		lc_chat.removeOldMessages();
	},

	setPost:function(post)
	{
		user_login=post.login.replace(/\&/g,'&amp;').replace(/\</g,'&lt;').replace(/\>/g,'&gt;');
		login=new Element('a', {href:'#', html:user_login, events:{click: function(){lc_chat.setName(post.login);return false;}}});
		if (user_login==chat_user) login.set('class', 'own');
		//new Element('div', {html:'['+post.time+'] '+post.login+': '+post.text}).inject($('mainChat'))
		div=new Element('div');
		if (post.info!=1)
		{
			new Element('span', {html:'['+post.time+'] '}).inject(div);
			login.inject(div);
			new Element('span', {html:'&gt; '+post.text}).inject(div);
		}
		else
			new Element('span', {html:post.text}).inject(div);
			
		div.inject($('mainChat'));
		lc_chat.last_id=post.id;
		var myFx = new Fx.Scroll('mainChat', null).toBottom();
	},
	
	removeOldMessages:function()
	{
		items=$('mainChat').getChildren('div');
		if ((count=items.length)>lc_chat.limit)
		{
			for(i=0;i<(count-lc_chat.limit);i++)
			{
				items[i].destroy();
			}
		}
	},
	
	setName:function(login)
	{
		$('msg_input').set('value', login+': '+$('msg_input').value).focus();
	}
}