var lobby_data;

function viewRoomStats(game_id, room_id, width, height)
{
	window.open('/multiplayer/room_stats.php?game_id=' + game_id + '&room_id=' + room_id, 'RoomStats', 'dependent=yes,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=10,left=10,width=' + width + ',height=' + height);
}

function launch(player_data, game_id, room_id, room_options, room_password, is_creator)
{
	showLoadingMessage();
	
	$('#lobby_content').fadeOut('def', function()
	{
		document.getElementById('lobby').innerHTML = 'Lobby SWF';

		$.ajax(
		{
			type: 'POST',
			dataType: 'json',
			url: '/multiplayer/game_data.php',
			data: 'game_id=' + game_id + '&player_data=' + player_data,
			success: function (game)
			{
				var game_box_width = game.width + 6;
			
				document.getElementById('game_content').style.width = game_box_width + 'px';
			
				var so = new SWFObject(game.swf_location + '?' + game.swf_last_modified_time, 'game_swf', game.width, game.height, game.min_flash_version, '#000000');

				so.addVariable('server', 1);
				so.addVariable('local', 0);
				so.addVariable('player_data', player_data);
				so.addVariable('game_id', game_id);
				so.addVariable('room_id', room_id);
				so.addVariable('password', room_password);
				so.addVariable('creator', is_creator);
				so.addVariable('hash', game.hash);

				if (is_creator)
				{
					so.addVariable('options', room_options);
				}

				so.addParam('menu', false);

				so.write('game');

				document.getElementById('game_content').style.display = 'block';
				
				hideLoadingMessage();
			},
			error: function (data)
			{
				var so = new SWFObject('/multiplayer/lobby/lobby.swf?' + lobby_data.last_modified, 'lobby_swf', 640, 480, 9, '#000000');
				
				so.addVariable('player_data', lobby_data.player_data);
				so.addVariable('settings', lobby_data.settings);
				so.addVariable('game_options', lobby_data.game_options);
				so.addVariable('hash', lobby_data.hash);
				so.addVariable('server', 1);
				so.addVariable('local', 0);
				so.addParam('menu', 'false');
				so.write('lobby');
			
				document.getElementById('lobby_content').style.display = 'block';
				
				hideLoadingMessage();
				
				alert('There was a problem launching the game.\n\n' + game_id + '\n' + player_data);			
			}
		 });
	});	
}

function quit(game_id)
{
	showLoadingMessage();

	$('#game_content').fadeOut('def', function()
	{
		document.getElementById('game').innerHTML = 'Game SWF';
		
		//Done after the lobby has been closed so that there is a greater chance of a 'disconnect' showing up in the previous games list when a user quits DURING a game
		refreshPreviousGames(game_id, 50);
		refreshLeaderboards(game_id);
		
		refreshAd();
		
		$.ajax(
		{
			type: 'POST',
			dataType: 'json',
			url: '/multiplayer/lobby_data.php',
			data: 'game_id=' + game_id,
			success: function (lobby)
			{
				var so = new SWFObject('/multiplayer/lobby/lobby.swf?' + lobby.swf_last_modified_time, 'lobby_swf', 640, 480, 9, '#000000');

				so.addVariable('player_data', lobby.player_data);
				so.addVariable('settings', lobby.game_settings);
				so.addVariable('game_options', lobby.game_options);
				so.addVariable('hash', lobby.hash);
				so.addVariable('server', 1);
				so.addVariable('local', 0);
				so.addParam('menu', 'false');
				so.write('lobby');
				
				document.getElementById('lobby_content').style.display = 'block';
				
				hideLoadingMessage();
				
				//Save the data so that it can be used to reload the lobby in case the game fails to load
				lobby_data =
				{
					'last_modified' : lobby.swf_last_modified_time,
					'player_data' : lobby.player_data,
					'settings' : lobby.game_settings,
					'game_options' : lobby.game_options,
					'hash' : lobby.hash
				};
			},
			error: function (data)
			{
				hideLoadingMessage();
			
				alert('There was a problem launching the lobby. Please refresh the page.');
			}
		});	
	});	
}

function reconnect()
{
	document.getElementById('lobby_swf').Reconnect();
}

function refreshPreviousGames(game_id, max_to_display)
{
	$('#previous_games').load('/multiplayer/refresh_previous_games.php', { 'game_id': game_id, 'max_to_display' : max_to_display });
}

function refreshLeaderboards(game_id)
{
	$('#game_page_rankings').load('/multiplayer/refresh_leaderboards.php', { 'game_id': game_id });
}

function showLoadingMessage()
{
	document.getElementById('loading_message').style.display = 'block';
}

function hideLoadingMessage()
{
	document.getElementById('loading_message').style.display = 'none';
}

function refreshAd()
{
	// It won't be defined for premium users, so we must check first
	if (typeof(advertisement) != 'undefined')
	{
		advertisement.location = '/multiplayer/advertisement.html';
	}
}

function adStarted()
{
	//Hide the quit button
	document.getElementById('game_close_button').style.visibility = 'hidden';
}

function adFinished()
{
	//Show the quit button
	document.getElementById('game_close_button').style.visibility = 'visible';
}