var fs_keys1 = ['Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L',"'",'Z','X','C','V','B','N','M','"','-','&','1','2','3','4','5','6','7','8','9','0'];
var fs_keys2 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',"'",'.','-','&','0','1','2','3','4','5','6','7','8','9'];

function fs_Hangman(){}
fs_Hangman.prototype.start_game = function(word){
	this.tries=6;        
	new Ajax.Request('/games/hangman_start_game', {asynchronous:true, evalScripts:true}); 

}
fs_Hangman.prototype.init = function(word, category,id){
	this.word = word;
	this.category = category;
	this.guess = "";
	this.entered="";
	this.id=id;
	for(i=0;i<this.word.length;i++)
		this.guess+=this.word.charAt(i)==' '?' ':'*';
	fs_hangman.draw_keyboard(0);
	fs_hangman.show_word();
	
}
fs_Hangman.prototype.show_word = function(){
var w="";
document.getElementById(this.word_out).innerHTML=(this.messages.word  + ": <b>" + this.guess + "</b>&nbsp; &nbsp; " + this.messages.category + ": " + this.category);
}

fs_Hangman.prototype.start = function(keyb_div, word_div, hang_div){
	this.key_out = keyb_div;
	this.word_out = word_div;
	this.hang_out = hang_div;

	this.start_game();
}	
fs_Hangman.prototype.guess_letter = function(l){
	l=fs_keys2[parseInt(l.substr(1))];
	
	if(this.entered.indexOf(l)==-1)
		this.entered+=l;
	else{
		alert(this.messages.already_tried);
		return false;
	}		
	
	var fs_found=false		;
	var pos=new String(this.word.toUpperCase()).indexOf(l);
	while(pos!=-1){
		fs_found = true;
		this.guess = this.guess.substr(0,pos)+l+this.guess.substr(pos+1);
		pos=this.word.toUpperCase().indexOf(l,pos+1);
	}

if(fs_found==true){
	this.show_word();
	if(this.guess==this.word.toUpperCase()){
		document.getElementById(this.hang_out).src="/images/hangman/won.gif";
		this.send_status(1);
		}

	
}
else
	{
		this.tries--;
		document.getElementById(this.hang_out).src="/images/hangman/hint"+(6-this.tries)+".gif";
		if(this.tries==0){
			this.guess=this.word;
			this.show_word();
			this.send_status(0);
			}
	}
}
	
fs_Hangman.prototype.restart = function(val){
if(confirm(this.restart_message[val]))
		this.start_game();
else{
	document.getElementById(this.key_out).style.display="none";	
	document.getElementById(this.hang_out).style.display="none";
	document.getElementById(this.word_out).style.display="none";
	document.getElementById("hangman_start_button").style.display="";
	}		
}

fs_Hangman.prototype.send_status = function(val){
		
new Ajax.Request('/games/hangman_game_over?id=' + escape(this.id) + '&hints=' + parseInt(this.tries) + '&won=' + (val==1?'true':'false'), {asynchronous:true, evalScripts:true,
	onComplete:function(request){eval(request.responseText);}}); 
}

fs_Hangman.prototype.draw_keyboard = function(which){
var ret="<table>";
var keys=fs_keys2;
for(var i=0;i<keys.length;i++){
if(i%10==0)ret+="<tr>";
ret+=("<td>"+
"<span onclick='fs_hangman.guess_letter(this.id);this.onmouseout=function(){};this.onmouseover=function(){};' id='k" + i + "' onmouseover='this.className=\"ht_hover\";' onmouseout='this.className=\"ht_even\";'" +

" class='ht_even'>"+ keys[i] +"</span></td>");
if(i%10==9)ret+="</tr>";
}
ret+="</table>";
document.getElementById(this.key_out).innerHTML = ret;
}

fs_Hangman.prototype.add_word = function(word, category){
new Ajax.Updater('result', '/games/hangman_add_word?word=' + escape(word) + '&category=' + parseInt(category), {asynchronous:true, evalScripts:true}); 
}

var fs_hangman = new fs_Hangman();
fs_hangman.restart_message = window.hangman_restart_messages ? window.hangman_restart_messages : ['You have lost!\n  Play again?','Congratulations, you have won!\n      Play again?'];
fs_hangman.messages = window.hangman_messages ? window.hangman_messages : {
    already_tried : "You have already tried that key!",
    word : "Word",
    category : "Category"
}


