﻿var chatroom =
{
	users: [],
	proxy: null,
	topic: null,

	test2Topics: function()
	{
		presence.connect('TOKEN1');
		getIframe('iframepresence').chatroom.joinTopic(1151);
		getIframe('iframepresence').chatroom.chat(1010, encodeURIComponent('test'), chatroom.topic);
		chatroom.leaveTopic();
		getIframe('iframepresence').chatroom.joinTopic(1322);
		getIframe('iframepresence').chatroom.chat(1010, encodeURIComponent('test'), chatroom.topic);
		chatroom.leaveTopic();
		presence.disconnect();
	},
	joinTopic: function()
	{
		chatroom.initEvents();
		chatroom.topic = resources.get('VAR_GameChat_CometGameID');
		getIframe('iframepresence').chatroom.joinTopic(chatroom.topic);
	},
	onJoinTopic: function(response)
	{
		chatroom.log('Topic joined');
		$('#tdJoinChatRoom').hide();
		$('#tdLeaveChatRoom').show();
		$('#chatinput').removeAttr('disabled');
		$('#chatinput').focus();
	},
	leaveTopic: function()
	{
		getIframe('iframepresence').chatroom.leaveTopic();
	},
	onLeaveTopic: function(response)
	{
		chatroom.log('Topic left');
		$('#tdLeaveChatRoom').hide();
		$('#tdJoinChatRoom').show();
	},
	onChat: function(response)
	{
		response.msg = response.msg.replace(/\+/g, '&#32;');
		response.msg = decodeURIComponent(response.msg);
		response.msg = response.msg.replace(/<.[^>]*>/gi, '');
		response.msg = response.msg.slice(0, 150);
		//response.msg = chatroom.replaceSmileys(response.msg);
		chatroom.write(response.idFrom + ': ' + response.msg);
	},
	onPlayerUpdate: function(response)
	{
		if (response.remove)
			chatroom.log(response.player.id + ' left the topic');
		else
			chatroom.log(response.player.id + ' joined the topic');
	},
	onError: function(response)
	{
		chatroom.log('Error: err = ' + response.err + ', status = ' + response.status);
	},

	log: function(s)
	{
		$('#chatcontent').append($('<label>').attr('class', 'Bold Red').html(s));
		$('#chatcontent').append($('<br>'));
		$('#chatcontent').scrollTop($('#chatcontent').attr('scrollHeight'));
	},
	write: function(s)
	{
		$('#chatcontent').append($('<label>').html(s));
		$('#chatcontent').append($('<br>'));
		$('#chatcontent').scrollTop($('#chatcontent').attr('scrollHeight'));
	},
	replaceSmileys: function(s)
	{
		s = s.replace(/\>\:\-\)/g, '<img class="imgsmiley" src="' + resources.get('SmileyDiabolic') + '" />');
		s = s.replace(/\:\-D/g, '<img class="imgsmiley" src="' + resources.get('SmileyHappy') + '" />');
		s = s.replace(/\:\-\)/g, '<img class="imgsmiley" src="' + resources.get('SmileySmile') + '" />');
		s = s.replace(/\:\-\//g, '<img class="imgsmiley" src="' + resources.get('SmileyConfused') + '" />');
		s = s.replace(/\:\-\(/g, '<img class="imgsmiley" src="' + resources.get('SmileyCry') + '" />');
		return s;
	},
	initEvents: function()
	{
		$('#chatsend').click(function()
		{
			var data = $('#chatinput').attr('value');
			data = data.replace(/\s/g, '');
			if (data.length > 0)
			{
				data = data.slice(0, 150);
				data = data.replace(/\r|\n|\r\n/g, ' ');
				getIframe('iframepresence').chatroom.chat(presence.user.id, encodeURIComponent(data), chatroom.topic);
			}
			$('#chatinput').attr('value', '');
			$('#chatinput').focus();
		});
		$('#chatinput').keydown(function(e)
		{
			if (events.keyCode(e) == 13 && $('#chatinput').attr('value') == '')
			{
				e.preventDefault();
				return true;
			}
			else if (events.keyCode(e) == 13 && $('#chatinput').attr('value') != '')
			{
				$('#chatsend').click();
				return false;
			}
		});
	}
}
