var last_id=0;
var chatroom_id=0;
var chat_to=null;

$(document).ready(function() {	$("#show_av").click(function() {		var avs=$(this).attr("checked");
		$('#chat_box div img').each(function () {			$(this).css("display",avs?'inline':'none');		});	});

	$("#show_time").click(function() {
		var avs=$(this).attr("checked");
		$('#chat_box div span').each(function () {
			$(this).css("display",avs?'inline':'none');
		});
	});

	$("#show_mod").click(function() {
		var avs=$(this).attr("checked");
		$('#chat_box div input').each(function () {
			$(this).css("display",avs?'inline':'none');
		});
	});
});

function LoadChatRoom(roomid) {	$('#chat_box').html('<div class="ajax_imgloading">Идет загрузка чата. Подождите...</div>');
	$.getJSON('/ajax/chat.php',{'enter_room':roomid,'loadmsgfromid':0,'getroomlist':true,'getrooms':true},function(data) {		processChatData(data);
	});
}

function LoadChatMsgs() {
	$.getJSON('/ajax/chat.php',{'loadmsgfromid':last_id,'getrooms':true},function(data) {
		processChatData(data);
		chat_to=setTimeout('LoadChatMsgs()',3000);
	});
}

function processChatData(data) {
	if(data.roomdata) {		$("#chat_chid").val(data.roomdata.id);
		chatroom_id=data.roomdata.id;
		$("#chat_title").html("&quot;"+data.roomdata.title+"&quot;");
		$('#chat_box').empty();
		chat_to=setTimeout('LoadChatMsgs()',3000);
	}
	if(data.roomlist) {		var div='<h4>Сейчас в чате:</h4>';
		if(data.roomlistsize>0) {			div+="<ul>";
			$.each(data.roomlist, function(i,item){				div+='<li><a href="javascript:;" onclick="chat_reply(\''+item+'\')">'+item+'</a></li>';			});
			div+="</ul>";
		}
		$("#chat_box_who").html(div);	}
	if(data.rooms) {		var div='';
		$.each(data.rooms, function(i,item){			if(i==chatroom_id)
				div+=item.title+" ("+item.users+")<br/>";
			else
				div+='<a href="javascript:;" onclick="LoadChatRoom(\''+i+'\')">'+item.title+"</a> ("+item.users+")<br/>";		});
		$("#chat_box_chan").html(div);	}
	if(data.msg) {		var avs=$("#show_av").attr("checked");
		var tspans=$("#show_time").attr("checked");
		var mods=false;
		mods=$("#show_mod").attr("checked");		$.each(data.msg, function(i,item){			if(item.syscmd=="delmsg") {				if(!data.roomdata){
            		$("#msg_"+item.text).remove();
            	}			}
			else {				var div=FormChatDiv(i,item,avs,tspans,mods);				$("#chat_box").append(div);
			}
		});
		$("#chat_box div:hidden").each(function () {
			$(this).css("display","block");
			$(this).css("opacity",0);
			var hs=$(this).height();
			if(avs) {				ah=$(this).find("img").eq(0).height();
				if(ah>hs) hs=ah;			}
    	   	$("#chat_box").scrollTo('+='+hs+'px', 0 );
			$(this).fadeTo("fast",1);
		});	}
	if(data.lastid) last_id=data.lastid;
}

function FormChatDiv(i,item,a,t,m) {	var div='<div id="msg_'+i+'"';
	if(item.syscmd) div+=' class="chatsysmsg"';
	div+='>';
	if(item.ip) {
		div+='<input style="display:'+(m?'inline':'none')+'" type="button" value="x" onclick="chat_goDelete('+i+')" /><input style="display:'+(m?'inline':'none')+'" type="button" value="-" onclick="alert(\''+item.ip+'\')" />';
	}
	div+='<span style="display:'+(t?'inline':'none')+'">'+item.posted+'</span>';
	if(item.syscmd) {	}
	else {
		div+='<img style="display:'+(a?'inline':'none')+'" src="'+item.avatar+'" alt="'+item.nick+'" />';
		div+='<a href="/users/'+item.poster+'/" target="_blank">'+item.nick+'</a>: ';
	}
	div+=item.text;
	div+='</div>';
	return div;}

function chat_reply(nick) {
	$('#chat_text').val(nick+', '+$('#chat_text').val());
}

function chat_goDelete(id) {	MBConfirmBox('Вы действительно хотите удалить сообщение?',"chat_doDelete('"+id+"');");
}

function chat_doDelete(id) { 	$("#msg_"+id).remove();
	$.getJSON('/ajax/chat.php',{'delmsg':id},function(data) {
		processChatData(data);
	});}

function entsub(event,ourform) {	if (event && event.which == 13) SendShout();
	else return true;
}

function SendShout() {	$.getJSON('/ajax/chat.php',{'sendmsg':$('#chat_text').val()},function(data) {		$('#chat_text').val("");	});}


/*--------------*/
