
function Hangman ()
{
	var gameId = false;
	var gameType = false;
	this.letters = new Array();
	var gameOver = false;
	
	this.setGameType = function (gameType)
	{
		this.gameType = gameType;
	}
	
	this.tryLetter = function (letter)
	{
		if((this.letters[letter])||(this.gameOver))
		{
			return false;
		}
		this.setOffClass(letter);
		data = new Object();
		$.post(Config.APP_URL+'DataProvider/hangman/checkLetter/letter='+letter+"&game_id="+this.gameId,data,function(data){
			Hangman.getInstance().letterResponse(data);
		},
		"json");
	}
	
	this.setOffClass = function (letter)
	{
		$("#the_letter_"+letter).addClass("disabled");
		this.letters[letter] = 1;
	}
	
	this.letterResponse = function (data)
	{
		this.setNewWordStatus(data.word);
		this.setGameStatus(data.game_status);
		if(data.game_over)
		{
			this.gameOver = true;
			$("#play_again").css({"display":"block"});
		}
	}
	
	this.setNewWordStatus = function (word)
	{
		$("#the_word").html(word);
	}

	this.startGame = function ()
	{
		data = new Object();
		$.post(Config.APP_URL+'DataProvider/hangman/startGame/gameType='+this.gameType,data,function(data){
			Hangman.getInstance().gameStarted(data);
		},
		"json");
	}
	
	this.gameStarted = function (data)
	{
		this.gameId = data.game_id;
		this.setNewWordStatus(data.word);
		for(i in data.disabled)
		{
			this.setOffClass(data.disabled[i]);
		}
		
		this.setGameStatus(data.game_status);
	}
	
	this.setGameStatus = function (game_data)
	{
		$("#main_game_img").attr("src",Config.IMAGES_URL+"spanzuratoarea/game_images/"+game_data.img);
		if(game_data.remaining>0)
		{
			$("#main_image_desc").html(game_data.desc+"<span>Daca mai gresesti inca de  "+game_data.remaining+" ori curcanul va fi gatit!</span>");
		}
		else
		{
			$("#main_image_desc").html(game_data.desc);
		}
		
		return true;
	}
}

Hangman.__instance__=null;

Hangman.getInstance=function()
{
	if(Hangman.__instance__==null)
	{
		Hangman.__instance__=new Hangman();
	}
	return Hangman.__instance__;
}
