var Invite = {

	initalize: function(presets) {
		
		this.container = '#gameInvitations .inv-body';

		return this;
	},

	
	send: function(game_domain, user_id) {
		flashCmd('[-60,'+game_domain+', '+user_id+']');
		jQuery(document).trigger('close.facebox');
	},
	
	showBox: function() {
		$('#gameInvitations').css('display', 'block');
	},
	
	hideBox: function() {
		var hide = true;
		
		$('#gameInvitations .inv-item').each(function() {
			if ($(this).css('display') != 'none')
			{
				hide = false;
			}
		});
		
		if (hide == true)
		{
			$('#gameInvitations').css('display', 'none');
		}
	},
	
	newInvitation: function(game_name, sender, invitation_id, time_left, i_send, points, domain_name, free_sits, players) {
		this.showBox();		
		
		/*
			Дали аз съм изпратил поканата, за да знаем кой темплейт да използваме
		*/
		if (i_send == 1) {
			var tmp = invitationFromMeTpl.replace(/{INVITATION_ID}/g, invitation_id);
			var tmp = tmp.replace(/{USERNAME}/g, sender);
			var tmp = tmp.replace(/{GAME_NAME}/g, game_name);
			var tmp = tmp.replace(/{TIME_LEFT}/g, time_left);
		}
		else {
			var tmp = invitationToMeTpl.replace(/{INVITATION_ID}/g, invitation_id);
			var tmp = tmp.replace(/{USERNAME}/g, sender);
			var tmp = tmp.replace(/{GAME_NAME}/g, game_name);
			var tmp = tmp.replace(/{TIME_LEFT}/g, time_left);
//			var tmp = tmp.replace(/{FREE_SIT}/g, free_sits);
			
			//	Лист с играчи на масата - START
			var playerList = '';
			if (players.length > 0) 
			{
				for (var i=0; i<players.length; i++) {
					playerList += '<b>'+players[i]+'</b> ';
				}
			}
			if (playerList != '')
			{
				var tmp = tmp.replace(/{PLAYERS_IN}/g, t('Players on table')+': '+playerList);
			}
			else
			{
				var tmp = tmp.replace(/{PLAYERS_IN}/g, '');
			}
			
			//	Лист с играчи на масата - END
		}
		
		if (points > 0)
		{
			var tmp = tmp.replace(/{POINTS_INFO}/g, ' ('+t('game with rating')+')');
		}
		else
		{
			var tmp = tmp.replace(/{POINTS_INFO}/g, ' ('+t('game without rating')+')');
		}
		
		var tmp = tmp.replace(/{DOMAIN}/g, domain_name);
		
		var cur = $(this.container).html();
		$(this.container).html(cur+tmp);
		
		Countdown.count('#invCount'+invitation_id, 0);
		blink('gameInvitation'+invitation_id);
	},

	accept: function(id) {

		$('#gameInvInfo'+id).html(t('Please wait...'));
		$('#gameInvitation').find('.inv-btns').css('display', 'none');

		flashCmd('[-61, '+id+', 1]');
	},
	
	deny: function(id) {
		flashCmd('[-61, '+id+', 0]');

		$('#gameInvitation'+id).css('display', 'none');
		
		Invite.hideBox();
	},
	
	timeOff: function(id) {

		$('#gameInvInfo'+id).html(t('Invitation time has expired'));
		setTimeout(function(){
			if ($('#gameInvitation'+id).length > 0)
			{
				$('#gameInvitation'+id).css('display', 'none');
			}
			Invite.hideBox();
		}, 5000);
	},
	
	opponentDeny: function(id) {
		$('#gameInvInfo'+id).html(t('Invitation refused'));
		$('#gameInvitation'+id).css('background-color', '#FFBFC8');
		setTimeout(function(){
			$('#gameInvitation'+id).css('display', 'none');
			Invite.hideBox();
		}, 7000);
	},
	
	opponentAccept: function(id) {
		$('#gameInvitation'+id).css('background-color', '#D8FFCC');
		$('#gameInvInfo'+id).html(t('Invitation accepted. Please wait...'));
	},
	
	clear: function() {
		$(this.container).html('');
		Invite.hideBox();
	}
	
};


Invite.initalize();
