
/**
 * Pelisali JS
 */

var global_flash_game_api_object;

$(document).ready(function()
{
	pelisali_init();

	sIFR.replace(clarendon, {
		selector: '#pelisali_game_teaser_list li h3',
		css: '.sIFR-root { color: #002142; font-size: 22px; margin-bottom: 0; line-height: 1em; }',
		wmode: 'transparent'
	});

	/*
	setTimeout(function()
	{
		global_flash_game_api_object = new FGA();
		global_flash_game_api_object.load_modal('test', '');
	}, 1000);
	*/

});

// -------------------------------------------------------------------------------------------------

function pelisali_init()
{
	$('.open_rules').click(function()
	{
		var game = $(this).attr('href').split('#')[1];

		open_overlay_page(_root + 'pelit/ajax_modal/rules_and_prices/' + game);

		return false;
	});

	set_pelisali_log_in_teaser_form();
}



function set_pelisali_log_in_teaser_form()
{
	return; // No ajax

	$('#pelisali_log_in_teaser_form').submit(function(e)
	{
		e.preventDefault();
		return false;
	});
}



function open_overlay_page(url)
{
	$('#overlay_page_content').load(url, function()
	{
		$('#flip_content_content').css({visibility: 'hidden'});

		$('#game_overlay_page_close_link').click(function()
		{
			$('#overlay_page_content').html('');
			$('#flip_content_content').css({visibility: 'visible'});
			return false;
		});

		// Center the overlay
		$('#game_overlay_page_wrapper').css({
			top: 150,
			left: ($('body').width() / 2) - ($('#game_overlay_page_wrapper').width() / 2)
		});

		// Position the close button
		$('#game_overlay_page_close_link').css({
			top: $('#game_overlay_page_wrapper').offset().top - 10,
			left: ($('#game_overlay_page_wrapper').offset().left + $('#game_overlay_page_wrapper').width()) + 50
		});

	});
}

// -------------------------------------------------------------------------------------------------

// Flash games call this function
function flash_game(method, param_1, param_2, param_3, param_4, param_5)
{
	global_flash_game_api_object = new FGA();
	eval('global_flash_game_api_object.' + method + '(param_1, param_2, param_3, param_4, param_5);');
}

// -------------------------------------------------------------------------------------------------

// Flash_Game_API (FGA) class
function FGA() {

	var game_id			= '';
	var current_points	= 0;
	var user_logged		= false;
	var remember_token	= false;

	// Game has started (user clicked "start")
	this.game_init = function(set_game_id)
	{
		FGA.game_id = set_game_id;
		console.log('Game ' + FGA.game_id + ' started.');
	}


	// Send points to server
	this.send_points = function(game_id_x, points, token)
	{
		console.log('Game ' + FGA.game_id + ' sends ' + points + ' points with token ' + token);

		// We might need this later on outside of the usual flow (logging in after submitting score)
		FGA.remember_token = token;

		var post  = 'game_id='	+ FGA.game_id;
			post += '&points='	+ points;
			post += '&token='	+ token;

		this.server('receive_points', post);
	}


	// Player has probably logged in after finishing a game
	this.send_points_again = function()
	{
		var post  = 'game_id='	+ FGA.game_id;
			post += '&points='	+ FGA.current_points;
			post += '&token='	+ FGA.remember_token;
			post += '&again=1';

		this.server('receive_points', post);
	}


	// Talks with the server which will return a JS function to be run by this class
	this.server = function(func, post)
	{
		var self = this;
		$.ajax({
			url: _root + 'flash_server_api/FAPI_' + func,
			type: 'post',
			data: post,
			success: function(response)
			{
				console.log('API Server call returned: ' + response);
				return eval('self.' + response);
			}
		});
	}


	// Saving points was successful
	this.points_saved = function(points, logged)
	{
		FGA.current_points	= points;
		FGA.user_logged		= logged;

		this.show_point_modal();
	}


	// Something went wrong, server will hopefully tell us what
	this.server_error = function(msg)
	{
		alert('Palvelinvirhe: ' + msg);
	}


	// Get the game over score board
	this.show_point_modal = function()
	{
		var data = 'game_id=' + FGA.game_id + '&current_points=' + FGA.current_points;

		this.load_modal('game_over_score_board', data);
	}


	// Loads up a modal or message area over flash game
	this.load_modal = function(uri, data)
	{
		// Destroy possible modal
		$('#game_modal').remove();

		// Create new modal
		$('body').append('<a href="#" id="game_modal_close_button" class="game_modal_close_link">X</a><div id="game_modal"><div id="game_modal_content"></div></div>');

		$('#game_modal').hide();

		$.ajax({
			url: _root + 'pelit/ajax_modal/' + uri,
			type: 'post',
			data: data,
			success: function(response)
			{
				// Render the ajax content, reveal the modal and center it
				$('#game_modal_content').html(response);
				$('#game_modal').show();
				$('#game_modal').css({
					top: 150,
					left: ($('body').width() / 2) - ($('#game_modal').width() / 2)
				});
				// Position the close button
				$('#game_modal_close_button').css({
					top: $('#game_modal').offset().top + 10,
					left: ($('#game_modal').offset().left + $('#game_modal').width()) - 30
				});

				// Close button
				$('.game_modal_close_link').click(function()
				{
					// Boot game
					boot_game();

					// Close modal
					$('#game_modal').remove();

					// Remove close button
					$('#game_modal_close_button').remove();
					return false;
				});
			}
		});
	}

} // End class FGA


// EOF
