/**
 * Game
 * Interact with the player and open dialog box
 */

function Game()
{
	this.gameInMapMode = false;
	this.nextDialogId = 1;
	this.map = null;
	this.player = null;
}

/**
 * Callback function called when the game is loaded.
 */
Game.prototype.init = function()
{
	var game = this;

	//Set the click action
	$("#enter_button").click(function()
	{
		//Start the game
		if(!this.gameInMapMode) game.start();
	});
}

/**
 * Initialize and start the game.
 */
Game.prototype.start = function()
{
	//Get rid of the div#game content
	$("#game").html("");
	
	//Load the map
	this.map = new Map();
	var gameContainer = $("#game").get(0);
	var game = this;
	this.map.load(gameContainer, function()
	{
		//The map is loaded
		game.gameInMapMode = true;
		
		//Load the player
		game.player = new Player(game, gameContainer, game.map);
		game.player.start();
	});
}

/**
 * Open a dialogbox
 * Check if the game is in map mode, if yes ask the little man to go to the right place and open the dialogbox.
 * if not open the dialogbox directly.
 *
 * @param id dialogbox ID
 * @param calledFromPlayer boolean variable which indicates if the Player called the function
 */
Game.prototype.openDialog = function(id, calledFromPlayer)
{
	var title = '';
	var src = '';
	switch(id)
	{
		case 'menu_cv':
			title = 'CV';
			src = 'html/cv/index.html';
			break;
		case 'menu_projects':
			title = 'Projects';
			src = 'html/projects/index.html';
			break;
		case 'menu_photos':
			title = 'Photos';
			src = 'html/photo_gallery/index.html';
			break;
		case 'menu_links':
			title = 'Links';
			src = 'html/links/index.html';
			break;
	}

	if(this.gameInMapMode && !calledFromPlayer)
	{
		//Ask the player to open the dialogbox
		//
		
		//Get the station
		var station = null;
		var stations = this.map.getStations();
		for(var i in stations)
			if(stations[i].action == id)
			{
				station = stations[i];
				break;
			}
		if(station == null) return;
			
		//Move the player to the station
		this.player.goToStation(station);
	}
	else
	{
		//Open directly the dialogbox
		var dialog = new Dialog(this);
		dialog.open(title, src);
	}
}
