/**
 * LiveChat
 *
 */

var LiveChat = {

	initalize: function(presets)
	{

		this.container = '#chatContainer';
		this.privacy = 0; // 0-мога да чатя с всички, 1-мога да чатя само на маса
		
		$('#chatEmosList').find('span').each(function(el){
	        $(this).click(function() {
	        	var emo = $(this).attr('rel');
				LiveChat.emoClick(emo);
	        });
			
	        $(this).bind('mouseover', function() {
	        	var emo = $(this).attr('emo');
	        	$(this).find('img').attr('src', EMO_URL+emo+'.gif');
	        });
			
	        $(this).bind('mouseout', function() {
	        	var emo = $(this).attr('emo');
	        	$(this).find('img').attr('src', EMO_URL+'static/'+emo+'.gif');
	        });
			
		});

		return this;
	},

	togglePrivacy: function()
		{
			if( $('.disabled-chat-info').length > 0 )
			{
				if( this.privacy == 0 )
				{
					var text = t('Enable chat');
					this.privacy = 1;
					$('.disabled-chat-info').css('display', 'block');

				}
				else
				{
					var text = t('Disable chat')
					this.privacy = 0;
					$('.disabled-chat-info').css('display', 'none');
				}
				$('#toggleChatPrivacy').html(text);
				flashCmd('[-65]');
			}

		},

	startChat: function(user) {
		flashCmd('[24, 0, '+user+']');
	},


	showBox: function() {
		if ($('.chat-box').css('display') == 'none') {
			$('.chat-box').css('display', 'block');
		}
	},

	hideBox: function() {
		$('.chat-box').css('display', 'none');
	},

	emos: function(id) {
		var coord = $('#chatId'+id).find('.chat-emots-btn').offset();
		var width = parseInt($('#chatEmosList').css('width'));
		var height = parseInt($('#chatEmosList').css('height'));

		if ((coord.left+width) > CACHED_WINDOW.w)
		{
			$('#chatEmosList').css('left', coord.left-width+35);
		}
		else
		{
			$('#chatEmosList').css('left', coord.left);
		}
		
		if ((coord.top+height) > CACHED_WINDOW.h)
		{
			$('#chatEmosList').css('top', (coord.top-height+25));
		}
		else
		{
			$('#chatEmosList').css('top', coord.top);
		}
		
		
		$('#chatEmosList').attr('rel', id);
		
		$('#chatEmosList').bind('mouseleave', function() {
			$('#chatEmosList').css('top', -1000);
			$('#chatEmosList').css('left', -1000);
		});
	},

	emoClick: function(emo) {
		var chatId = $('#chatEmosList').attr('rel');
		var cur = $('#input'+chatId).val();		
		$('#input'+chatId).val(cur+' '+emo+' ');
		$('#input'+chatId).focus();
		
		$('#chatEmosList').css('top', -1000);
		$('#chatEmosList').css('left', -1000);
	},

	openChat: function(id, users, msgs, title, canClose, haveColor) {

		this.showBox();


		var template = '';
		if( $('#chatId'+id).length == 0 )
		{

			template = '<div class="chit-chat"  id="chatId'+id+'" onClick="LiveChat.click(\''+id+'\');">';
			if (canClose == 1)
			{
				template = template + '<a class="i-delete" href="javascript:;" onclick="LiveChat.close(\''+id+'\');">&nbsp;</a>';
			}
			template = template + '<div class="cc-head">&nbsp;</div>'+
			'					<div class="cc-user">'+title+'</div>'+
			'					<div class="cc-content" id="msgs'+id+'">'+
			'					</div>'+
			'					<div class="cc-write-tools">'+
			'						<form name="s" action="" class="newChatMsgForm" method="POST" onSubmit="LiveChat.msg(\''+id+'\'); return false;">'+
			'							<input name="chat-field" id="input'+id+'" value="" class="chat-input"/>'+
			'							<span class="chat-emots-btn" onclick="LiveChat.emos('+id+');">&nbsp;</span>'+
			'						</form>'+
			'					</div>'+
			'					<div class="cc-tools">';
			if (canClose == 1)
			{
				template = template + '<a href="/extras/" class="i-chat-color">'+t('Change chat color',{})+'</a><br/>';
			}
			else
			{
				template = template + '<a href="javascript:;" id="toggleChatPrivacy" class="i-block-user" onClick="LiveChat.togglePrivacy();">'+t('Disable chat',{})+'</a><br/>';
			}

			template = template + '<div class="report-link"><a href="javascript:;" class="i-report-abuse" onClick="LiveChat.report('+id+');">'+t('Report abuse',{})+'</a></div>';

			if (canClose == 1)
			{
				template = template + '<a href="javascript:;" class="i-block-user" onClick="LiveChat.ignore('+id+')">'+t('Block user',{})+'</a><br/>';
			}
			
			template = template + '</div>'+
						'	<div class="cc-foot">&nbsp;</div>'+
						'</div>';
		}

		var cur = $(this.container).html();
		$(this.container).html(cur+template);

		if( msgs.length > 0 )
		{
			for( var k=0; k<msgs[0].length; k++)
			{
				LiveChat.newLine(id, msgs[0][k]);
			}
		}

		var openChats = $('.chit-chat');

		if ( openChats.length > 1 ) {
			$('.chat-body').find('div.chit-chat').each(function() {
		        $(this).removeClass('expand');
		    });
		}
		LiveChat.click(id);
	},


	newLine:function(id, msg)
			{

				if( $('#chatId'+id).length == 0 )
				{
					flashCmd('[27, '+id+']');
				}
				if( $('#msgId'+id+msg[3]).length == 0 )
				{
					var cur = $('#msgs'+id).html();

					var chatColor = '#666666';
					if( msg[4] )
					{
						chatColor = msg[4];
					}

					var tmp = '<div id="msgId'+id+msg[3]+'" style="color:'+chatColor+';"><em>'+msg[1]+'</em> <b>'+msg[0]+':</b> '+msg[2]+'</span>';
					$('#msgs'+id).append(tmp);

					
					$('#msgs'+id).scrollTop(document.getElementById('msgs'+id).scrollHeight);
					
					if( id != openedChat )
					{
						LiveChat.markUnRead(id);
					}
				}
			},

	msg: function(id) {
		if ($('#input'+id).val() != '') {
			var text = $('#input'+id).val();

			text = text.replace('//g', '');
			text = text.replace('"/g', '');
			text = text.replace("'/g", '');

			if( text != '' )
			{
				/*
					Първо включваме чата и после изпращаме съобщението
				*/
				if( this.privacy == 1 )
				{
					this.privacy = 0;
					flashCmd('[-65]');
				}

				flashCmd('[26, '+id+', \''+text+'\']');
				$('#input'+id).val('');
			}
		}
	},

	close: function(id)
		   {
				if( $('#chatId'+id).length == 0 )
				{
					return ;
				}
				$('#chatId'+id).remove();

				flashCmd('[28, '+id+']');

				if( $('.chit-chat').length == 0)
				{
					this.hideBox();
				}
		  },

	markUnRead: function(id) {

		if ($('#chatId'+id).length > 0) {
			$('#chatId'+id).addClass('chat-unread');
		}
	},

	markRead: function(id) {

		if ($('#chatId'+id).length > 0) {
			$('#chatId'+id).removeClass('chat-unread');
		}

	},

	ignore: function(id) {

		if (confirm(t('Are you sure ?')))
		{
			if ($('#chatId'+id).length > 0) {
				flashCmd('[-116, '+id+']');
				LiveChat.close(id);
			}
			return true;
		}
		else
		{
			return false;
		}

	},

	report: function(id) {

		if (confirm(t('Are you sure ?')))
		{
			if ($('#chatId'+id).length > 0) {

				$.post("/ajax/chat.php", {
							'do': 'insult',
							'content': $('#msgs'+id).html()
							},
							function(data){
								$('#chatId'+id).find('.report-link').css('display', 'none');
							},
				"json");
			}
		}

	},


	click: function(id) {

		if ($('#chatId'+id).length == 0) {
			return;
		}

		flashCmd('[25.1, '+id+']');


		$('.chat-body').find('div.chit-chat').each(function() {
	        $(this).removeClass('expand');
	    });


		$('#chatId'+id).addClass('expand');
		$('#input'+id).focus();


		$('#msgs'+id).scrollTop(document.getElementById('msgs'+id).scrollHeight);

		openedChat = id;
		
		LiveChat.markRead(id);
	}

};



var openedChat = 0;

$(document).ready(function(){
	LiveChat.initalize();

	$('.startChat').each(function() {
        $(this).click(function() {
			var cur = $(this).attr('rel');

			LiveChat.startChat(cur);

        });
    });

});