//
// QueryStrings
//
function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}
}

QueryString_Parse();

// Helper Function
function NewHint(hint)
{
	var newHintObj = new Object();
	newHintObj.hint = hint;
	newHintObj.answers = new Array(arguments.length-1);
	
	for (var i=0;i<arguments.length-1;i++)
		newHintObj.answers[i] = arguments[i+1];
		
	gHangMan.hintObj[gHangMan.hintObj.length] = newHintObj;
}

// Class Hangman

function Hangman_ResetGame()
{
	window.location.replace(window.location.pathname+"#hangman");
}

function Hangman_ChooseLetter(letter)
{
	this.currGuess += letter;
	
	for (var i=0;i<this.letters.length;i++)
	{
		if (this.letters[i]==letter)
		{
			this.letters[i] = " ";
			break;
		}
	}
	
	if (this.currAnswer.indexOf(letter)<0)
		this.guessesLeft--;
		
	this.Show();
}

function Hangman_DoWin()
{
	return '<P class="alert">You win!!!</P>' + this.msgWin;
}

function Hangman_DoLose()
{
	var html = '<P class="alert">Sorry, you lose.</P>';
	
	if (this.showCorrectAnswer)
		html += '<p>The correct answer is "' + this.currAnswer + '".</p>';
		
	html += this.msgLose;
	
	return html;
}

function Hangman_GetHintString()
{
	return '<P><STRONG>Hint:</STRONG> '+ this.hint + '</P>';
}

function Hangman_GetGuessString()
{
	var guessStr = "";
	
	for (var i=0;i<this.currAnswer.length;i++)
	{
		var lookFor = this.currAnswer.substring(i,i+1);
		if (lookFor == " ")
			guessStr += " ";
		else if (this.currGuess.indexOf(lookFor)>=0)
			guessStr += lookFor;
		else
			guessStr += "_";
			
	}
	return guessStr;
}

function Hangman_GetGuessHTML()
{
	var guessStr = "<P>";
	
	for (var i=0;i<this.currAnswer.length;i++)
	{
		guessStr += " ";
		var lookFor = this.currAnswer.substring(i,i+1);
		if (lookFor == " ")
			guessStr += "   ";
		else if (this.currGuess.indexOf(lookFor)>=0)
			guessStr += lookFor;
		else
			guessStr += "_";
			
	}
	guessStr += "</P>";
	
	return guessStr ;
}

function Hangman_GetLetters()
{
	var letterStr = "<P>";
	
	for (var i=0;i<this.letters.length;i++)
	{
		if ((i==9)||(i==18))
			letterStr += "<br>";
		else
			letterStr += " ";
			
		if (this.letters[i]==" ")
		{
			letterStr += " ";
		}
		else
		{
			letterStr += '<a class="linkStyle" href=\'javascript:gHangMan.ChooseLetter("' + this.letters[i] + '")\'>' + this.letters[i] + '</a>';
		}
	}
	letterStr += "</P>";
	return letterStr;
}

function Hangman_GetHTML()
{	
	var guessStr = this.GetGuessString();
		
	var lettersText;

	if (guessStr==this.currAnswer)
		lettersText = this.DoWin();
	else if (this.guessesLeft<=0)
		lettersText = this.DoLose();
	else
		lettersText = this.GetLetters();


	var html = '<table border="0" cellspacing="0" cellpadding="0">';
	html += '<tr><td align="center">';
	html += '<table border="1" bordercolor="black" cellspacing="0" cellpadding="2"><tr><td align="center">';
	html += '<table border="0" cellspacing="0" cellpadding="0">';
	html += '<tr>';
	html += '	<td align="center"><h1 align="center"><a name="hangman">Hangman!</a></h1></td>';
	html += '</tr>';
	html += '</table>';
	html += '</td></tr><tr><td align="center">';
	html += '<table border="0" cellspacing="4" cellpadding="0">';
	html += '<tr>';
	html += '	<td><IMG border="0" SRC="images/spacer.gif" height="1" width="116"></td>';
	html += '	<td><IMG border="0" SRC="images/spacer.gif" height="1" width="140"></td>';
	html += '</tr>';
	html += '<tr>';
	html += '	<td><img border="0" name="gallows" src="images/' + this.imageName + this.guessesLeft + '.gif"></td>';
	html += '	<td ><p>' + lettersText + '</p></td>';
	html += '</tr>';
	html += '</table>';
	html += '</td></tr><tr><td align="center">';
	html += '<table border="0" cellspacing="0" cellpadding="2">';
	html += '<tr>';
	html += '	<td align="center">' +  this.GetHintString() + '</td>';
	html += '</tr>';
	html += '<tr>';
	html += '	<td align="center">' + this.GetGuessHTML() + '</td>';
	html += '</tr>';
	html += '<tr>';
	html += '	<td align="center"><form><input id="reset" type="button" value="Next Word" onclick="gHangMan.ResetGame()"></form></td>';
	html += '</tr>';
	html += '</table>';
	html += '</td></tr></table>';
	html += '</td></tr></table>';

	return html;
}

function Hangman_WriteHTML()
{
	if (this.hintIndex < 0)
	{
		this.hintIndex = Math.floor(Math.random()*this.hintObj.length);
		this.ansIndex = Math.floor(Math.random()*this.hintObj[this.hintIndex].answers.length);
	}
	
	this.hint = this.hintObj[this.hintIndex].hint;
	this.answers = this.hintObj[this.hintIndex].answers;
	
	var ans = this.hintObj[this.hintIndex].answers[this.ansIndex].split("|");
	this.currAnswer = ans[0].toUpperCase();
	if (ans.length>1)
		this.msgWin = ans[1];
	if (ans.length>2)
		this.msgLose = ans[2];
	
	document.write('<DIV ID=gameLayer STYLE="position:relative">'+this.GetHTML()+'</DIV>');
}

function Hangman_Show()
{
	var search = "?hintIndex=" + this.hintIndex + "&ansIndex=" + this.ansIndex + "&currGuess=" + this.currGuess + "&guessesLeft=" + this.guessesLeft;
	window.location.replace(window.location.pathname+search+"#hangman");
}

function Hangman()
{
	this.letters = new Array("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");
	this.hintObj = new Array();
	this.hint = null;
	this.answers = null;
	this.imageName = "hangman";
	this.currAnswer = "";
	this.currGuess = "";
	this.guessesLeft = 6;
	this.hintIndex = -1;
	this.ansIndex = -1;
	this.showCorrectAnswer = false;
	this.msgWin = "";
	this.msgLose = "";

	// Check query string and initialize fields if set
	var qHintIndex = QueryString("hintIndex");
	
	if (qHintIndex != null)
	{
		this.hintIndex = parseInt(qHintIndex);
		this.ansIndex = parseInt(QueryString("ansIndex"));
		this.currGuess = QueryString("currGuess");
		this.guessesLeft = parseInt(QueryString("guessesLeft"));

		for (var i=0;i<this.letters.length;i++)
		{
			if (this.currGuess.indexOf(this.letters[i])>=0)
				this.letters[i] = " ";
		}
	}

	
	this.ResetGame = Hangman_ResetGame;
	this.ChooseLetter = Hangman_ChooseLetter;
	this.DoWin = Hangman_DoWin;
	this.DoLose = Hangman_DoLose;
	this.GetHintString = Hangman_GetHintString;
	this.GetGuessString = Hangman_GetGuessString;
	this.GetGuessHTML = Hangman_GetGuessHTML;
	this.GetLetters = Hangman_GetLetters;
	this.GetHTML = Hangman_GetHTML;
	this.WriteHTML= Hangman_WriteHTML;
	this.Show = Hangman_Show;
}

gHangMan = new Hangman();

// Add your hints and answers here (as many as you want)
NewHint("Consider Abraham as their spiritual father","Muslim");
NewHint("practices or ways of the prophets and make it as the norm of their way of life","sunnah","hadith");
NewHint("complete submission to Allah","Islam");
NewHint("Called to be true in faith","hanif");
NewHint("A community in Islam","ummah");
NewHint("came from an idolatrous family of origin","Abraham");
NewHint("Prophet of Islam","Muhammad");
NewHint("Abraham's descendants through Ishmael","Arabs","Muslims");
NewHint("Abraham's descendants through Isaac","Jews","Christians");
NewHint("Prophet of Allah whom he speaks directly","Moses");
NewHint("First Prophet of Allah","Adam");
NewHint("Who confirming the Law that had come before him","Jesus");
NewHint("O People of the Book","Jews","Christians");
NewHint("Books were the Law of Moses recorded","Taurat");
NewHint("Books of Gospel and Letters","Injeel");
NewHint("Books of Psalms and writings","Zaboor");



