var ChatMutex = false;

var iChatPanelLastMessageId = 0;
var iActiveChatroom = 0;
var sChatPanelName = "";

var bChatUsersDirty = true;

var aChatrooms = new Array();
var aChatroomNames = new Array();

function ChatPanelKeyupEvent( id , e ) {
	if (e.keyCode!=13) return true;
	ChatSendMessage( id );
	return true;
}

function ChatPanelInitPost( postdata ) {
	if (iChatPanelLastMessageId) postdata.last_chat_message_id = iChatPanelLastMessageId;
	if (bChatUsersDirty) {
		// TODO post chatrooms
		postdata.get_chatroom_users = true;
		bChatUsersDirty = false;
	}
	return postdata;
}

function ChatPanelPostCallback( data ) {
	RefreshChatMessagesCallback( sChatPanelName , data );
}

function RefreshChatroomNames( id ) {
	var a = new Array();
	var chatrooms = $("#"+id).data( "chatrooms" );
	var i;
	for (i in chatrooms) a[ chatrooms[i].chatroom_id ] = chatrooms[i].chatroom_name;
	aChatroomNames = a;
}

function ChatroomUserPanelPostCallback( data ) {
	var chatroom_id;
	var h = "";
	for (chatroom_id in data) {
		var chatroom_members = data[chatroom_id];
		var members = chatroom_members.members;
		var user_id;
		for (user_id in members) {
			var member_name = members[user_id];
			h+=member_name + "<br/>";
		}
	}
	$("#memberlist").html( h );
}

function ChatHandleSystemMessage( id , message ) {
	switch (message.message_text) {
	case "refresh_users":
		var d = new Object;
		d.id = id;
		d.command = "RefreshUsers";
		ChatSendSystemCommand( id , d );	
		break;
	}
}

function ChatPrepareMessage( message ) {
	message.chat_message_id = parseInt( message.chat_message_id );
	message.chatroom_id = parseInt( message.chatroom_id );
	message.message_flags = parseInt( message.message_flags );
	message.is_system = (message.is_system=="t");
	return message;
}

function RefreshChatMessagesCallback( id , data ) {
	// if (ChatMutex) return false;ChatMutex = true;
	var o = $("#"+id);
	var chatrooms = o.data( "chatrooms" );
	iChatPanelLastMessageId = data.last_chat_message_id;
	var messages = data.messages;
	var i = 0;
	var wassystem = false;
	for (i in messages) {
		var message = ChatPrepareMessage( messages[i] );
		var tab = $("#chat_"+aChatroomNames[message.chatroom_id]);
		var ctstamp = message.ctstamp.substring( 11 , 19 );
		if (message.is_system) {
			ChatHandleSystemMessage( id , message );
		} else {
			// normal message
			var msg = "";
			if (message.message_flags&1) msg+="<b>";
			msg+=ctstamp + ": ";
			if ( message.poster_user_id ) {
				msg+=message.poster_user_name + ": <br/>&raquo;&nbsp;&nbsp;";
			}
			msg+=message.message_text;
			if (message.message_flags&1) msg+="</b>";
			msg+="<br/>";	
			tab.append( msg );
		}
		var tabdiv = tab.get(0);
		tabdiv.scrollTop = tabdiv.scrollHeight;
	}
	if (wassystem) bChatUsersDirty = true;
	ChatMutex = false;
}

function RenderChatroomUsers( id ) {
	var o = $("#"+id);
	var chatroom_users = o.data( "chatroom_users" );
	if (typeof chatroom_users[iActiveChatroom] == "undefined") return;
	var acu = chatroom_users[iActiveChatroom];
	var i;
	var ul = $("<ul></ul>");
	var num = 0;
	for (i in acu) {
		ul.append( $("<li></li>").addClass( (num%2) ? "odd" : "even" ).data( "user_id" , acu[i].user_id ).html( acu[i].user_name ) );
		num++;
	}
	$("#memberlist").html( ul );
}

function RefreshChatroomUsersCallback( id , chatroom_users ) {
	var o = $("#"+id);
	o.data( "chatroom_users" , chatroom_users );
	RenderChatroomUsers( id );
}

function ChatSendSystemCommand( id , datas ) {
	var o = $("#"+id);
	var req = datas;
	req.id = id;
	req.action = "chat";
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: req,
			url: "/chatservice/",
			success: function (ret) {
				if (typeof ret.to_eval != "undefined")
					eval( ret.to_eval );
			}
		}
	);
}

function ChatSendMessage( id ) {
	if ($("#"+id+"_msg").val().length==0) return;
	var o = $("#"+id);
	var chatrooms = o.data( "chatrooms" );
	var t = $("#"+id+" div.chatleft");
	var tabindex = t.tabs("option","selected" );
	var chatroom = chatrooms[tabindex];
	var req = new Object;
	req.action = "chat";
	req.command = "message";
	req.chatroom_id = iActiveChatroom;
	req.message = $("#"+id+"_msg").val();
	$.ajax(
		{
			type: "POST",
			cache: false,
			url: "/chatservice/",
			data: req,
			success: function (ret) {
				if (typeof ret.to_eval != "undefined") eval( ret.to_eval );
				$("#"+id+"_msg").val("");
			}
		}
	);
}


function InitChatPanel( id , datas ) {
	iChatPanelLastMessageId = datas.last_chat_message_id;
	var o = $("#"+id);
	sChatPanelName = id;
	o.data( "chatrooms" , datas.chatrooms );
	RefreshChatroomNames( id );

	// init tabs
	var t = $("#"+id+" div.chatleft");
	t.tabs();

	// set the onkeyup
	var txt = $("#"+id+"_msg");
	txt.keyup( function(e) { ChatPanelKeyupEvent( id , e ); } );

	ChatNewMessages( id );
	switch (datas.chatrooms.length) {
	case 0:
		// nincs chatroom
		return;
	case 1:
		// csak egy van, azonnali beleptetes
		var d = new Object;
		d.command = "Login";
		d.chatroom_id = datas.chatrooms[0].chatroom_id;
		iActiveChatroom = d.chatroom_id;
		aChatrooms[d.chatroom_id]=true;
		ChatSendSystemCommand( id , d );
	}
}

function ChatNewMessages( id ) {
	var d = new Object;
	d.id = id;
	d.command = "WaitMessages";
	d.last_chat_message_id = iChatPanelLastMessageId;
	ChatSendSystemCommand( id , d );
}
