//
// 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="/library/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="New Game" 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("Beginning in the 17th Century golf balls were known by this name","Featherie");
NewHint("This material was used to make golf shafts beginning in 1826","Hickory");
NewHint("Mowers for golf courses were manufactured for the first time in 1832, but most courses of the time still use this to trim their grass","Sheep");
NewHint("These words were added to the title of St. Andrews Golf Club in 1834","Royal and Ancient");
NewHint("He won the first British Open, played in 1860","Willie Park");
NewHint("This famous golfer and golf course superintendant won back to back British Opens in 1861-62 and four times total","Old Tom Morris");
NewHint("The first recorded hole-in-one was made by a very famous golfer who also won the British Open four times and died young on Christmas Day in 1875","Young Tom Morris");
NewHint("The traditional British Open trophy was first awarded in 1873 and is now known by this name","Claret Jug");
NewHint("Gutties were a type of golf ball manufactured from a rubber taken from a specific tropical tree. What was the full name of the guttie?","Gutta Percha");
NewHint("This golfer was one of the first truly famous golfers. He won the British Open six times, the first time in 1896. The PGA Tour's scoring trophy is named after him today","Harry Vardon");
NewHint("He was the first American President known to have played golf","William McKinley");
NewHint("The first wound golf ball debuted in 1898 and quickly replaced the guttie. What was this one-piece, rubber-core ball called?","Haskell");
NewHint("This common golf scoring term first came into use in 1898 at a golf club in New Jersey","Birdie");
NewHint("This famous golf course designer served as an apprentice to Old Tom Morris at St. Andrews in 1899. He went on to design many masterpieces, perhaps the most famous being Pinehurst No. 2","Donald Ross");
NewHint("This material became popular for use in wooden clubheads around 1900","Persimmon");
NewHint("This feature that is found on all irons was first used in 1902","Grooves");
NewHint("This feature now found on all golf balls first appeared on the Haskell ball in 1905","Dimples");
NewHint("Three of golf's all-time greats were born in 1912. One was Ben Hogan, another was Sam Snead. Who was the third?","Byron Nelson");
NewHint("In one of golf's greatest upsets - and a tournament that is credited with popularizing golf in the U.S. - this American amateur defeated British stars Harry Vardon and Ted Ray in a playoff for the U.S. Open. Books have been written about him, but his name is hard to spell","Francis Ouimet");
NewHint("This 14-year-old reached the quarterfinals of the U.S. Amateur Championship in 1916, his first of many appearances in the event","Bobby Jones");
NewHint("This famous California golf course opened in 1919","Pebble Beach");
NewHint("This golf great burst onto the scene at age 20 in 1922, winning both the U.S. Open and PGA Championship","Gene Sarazen");
NewHint("The first American-born golfer to win the British Open was one of the most famous athletes of the 1920s","Walter Hagen");
NewHint("In a 72-hole match play event in 1926, Bobby Jones was thrashed by this golfer 12-and-11. Who beat Jones in that match?","Walter Hagen");
NewHint("This team competition was first played in 1927","Ryder Cup");
NewHint("Golf's only grand slam winner retired from competitive golf at age 28","Bobby Jones");
NewHint("It is the Ryder Cup-style event played between teams of amateurs representing the U.S. and Great Britain and Ireland","Walker Cup");
NewHint("The Walker Cup is an amateur team event for men in the style of the Ryder Cup. What is the amateur team event for women called?","Curtis Cup");
NewHint("The Ryder Cup-style event for women pros pits teams representing the U.S. and Europe and is named after the founder of Ping","Solheim Cup");
NewHint("This famous female golfer may have been the greatest female athlete of all-time","Babe Didrikson Zaharias");
NewHint("This famous Georgia golf course opened in 1933","Augusta National");
NewHint("The first Masters was played in 1934. The first winner later became the first two-time winner. Who was he?","Horton Smith");
NewHint("This golfer hit The Shot Heard Round the World, a double-eagle from the No. 15 fairway during the final round of the 1935 Masters","Gene Sarazen");
NewHint("The Pebble Beach National Pro-Am tournament used to be known by another name - the name of this famous singer and actor","Bing Crosby");
NewHint("What was the name of the tactic that was once legal under the rules of putting your ball into the line of your opponent's putt in order to block your opponent's path to the hole?","Stymie");
NewHint("This golfer received a delay of his induction into the Navy in 1942 in order to play in the PGA Championship - and then won the tournament","Sam Snead");
NewHint("In 1945, this golfer won 11 events in a row and 18 total","Bryon Nelson");
NewHint("In 1945, this woman made the 36-hole cut in the PGA Tour Los Angeles Open","Babe Didrikson Zaharias");
NewHint("The U.S. Women's Open was first played in 1946, and one of the first great professionals of women's golf - this World Golf Hall of Fame member - won it","Patty Berg");
NewHint("This golfer won 13 times on the PGA Tour in 1946, including the U.S. Open","Ben Hogan");
NewHint("This Texas golfer won the Masters three times, and in 1947 became the first to shoot all four rounds under par in the event","Jimmy Demaret");
NewHint("In 1948, this great South African golfer set a PGA Tour record for margin of victory, winning one event by 16 strokes","Bobby Locke");
NewHint("This man was the first Masters champion to wear the now-traditional Green Jacket","Sam Snead");
NewHint("This legendary golfer's first PGA victory came in the 1955 Canadian Open","Arnold Palmer");
NewHint("This LPGA legend amassed 82 career victories, the first coming in 1955. Ben Hogan once said she had the best golf swing he'd ever seen","Mickey Wright");
NewHint("This career grand slam winner got his first PGA Tour victory at the 1958 Kentucky Derby Open","Gary Player");
NewHint("The first time most golf fans heard of this golfer was when he won the 1959 U.S. Amateur Championship, the first of his two U.S. Amateur wins","Jack Nicklaus");
NewHint("He was the first non-American to win the Masters when he did so in 1961","Gary Player");
NewHint("Arnold Palmer blew a 7-shot lead over the final nine holes of the 1966 U.S. Open, then lost an 18-hole playoff for the title to this man","Billy Casper");
NewHint("This man incorporated the company that makes Ping golf clubs in 1967 in response to growing demand for his Anser putter","Karsten Solheim");
NewHint("In one of golf's most famous disqualifications, this golfer - who would have been in a playoff - lost the 1968 Masters when he signed an incorrect scorecard","Roberto De Vicenzo");
NewHint("When this golfer won the 1968 U.S. Open, it was his first major championship. And he got it by becoming the first golf to break par in all four rounds of the U.S. Open","Lee Trevino");
NewHint("This is Lee Trevino's nickname","Merry Mex");
NewHint("This is Jack Nicklaus' nickname","Golden Bear");
NewHint("This astronaut hit a 6-iron shot on the surface of the moon in 1971","Alan Shepherd");
NewHint("This golfer is the only one so far to win the U.S. Open, British Open and Canadian Open in the same year. The year was 1971","Lee Trevino");
NewHint("This golf ball brand was introduced by Spalding in 1972 and continues to be one of the most popular brands with recreational players","Top Flite");
NewHint("In one of golf's greatest final-round performances, this golfers shoots 63 at Oakmont Country Club to win the U.S. Open","Johnny Miller");
NewHint("This famous Texas golfer won his first tournament as a pro - the Texas Open - in 1973","Ben Crenshaw");
NewHint("The Tournament Players Championship - what some refer to as the fifth major - was first played in 1974. Who was the first winner?","Jack Nicklaus");
NewHint("No golfer finished under par at the 1974 U.S. Open. The winner, Hale Irwin, shot 7-over par. By what monicker has this tournament come to be known?","Massacre at Winged Foot");
NewHint("This golfer won the first of his five British Open titles in 1975","Tom Watson");
NewHint("The robotic golfer that the USGA uses to test golf balls is named after a famous golfer. What is the name of the USGA's ball-testing machine?","Iron Byron");
NewHint("This golfer was the first to shoot 59 in tournament play when he did it at the 1977 Danny Thomas Memphis Classic","Al Geiberger");
NewHint("This is the device used to measure green speed","Stimpmeter");
NewHint("This LPGA star burst onto the scene with five consecutive wins in 1978","Nancy Lopez");
NewHint("The tournament that is credited with launching the Senior PGA Tour was a 2-man team event first played in Austin, Texas, in 1978. What was it called?","Legends of Golf");
NewHint("Signalling a coming tide of great European players, this 20-year-old won the British Open in 1979","Seve Ballesteros");
NewHint("In the 1980s, this Australian brought a sex-sells approach to the LPGA Tour, posing for a famous cheesecake calendar","Jan Stephenson");
NewHint("Added to the USGA Handicapping System in 1985, this number is usually found on scorecards and represents the difficulty of a golf course for bogey golfers","Slope Rating");
NewHint("This golfer holed out a bunker shot on the tournament's final hole to edge Greg Norman and win the 1986 PGA Championship","Bob Tway");
NewHint("The International is a PGA Tour event in which the highest score wins. What is the name of the type of scoring used at this event?","Modified Stableford");
NewHint("This golfer is famous for his sword dance following birdie putts","Chi Chi Rodriguez");
NewHint("This golfer won back-to-back U.S. Opens in 1988 and 1989 and later captained a U.S. Ryder Cup team","Curtis Strange");
NewHint("The Callaway golf club company became one of the most popular brands in the world when it introduced this club in 1991","Big Bertha Driver");
NewHint("He was the first golfer to win the U.S. Junior Amateur Championship in consecutive years","Tiger Woods");
NewHint("This event is a Ryder Cup-style event that pits U.S. and International teams in match play competition","Presidents Cup");
NewHint("Despite not winning a tournament, this golfer was named Rookie of the Year on the LPGA Tour in 1994","Annika Sorenstam");
NewHint("Ben Crenshaw won his second Masters in 1995, just days after the death of his mentor, this famous golf coach and instructor","Harvey Penick");
NewHint("This golfer's first win on the LPGA Tour was the 1995 U.S. Women's Open","Annika Sorenstam");
NewHint("At the 1996 Masters, Nick Faldo came from six shots behind on the final day to win by five shots over this man, his playing partner during the round","Greg Norman");
NewHint("This golfer blew a 3-shot lead with one hole to play at the 1999 British Open, eventually losing in a playoff","Jean Van de Velde");
NewHint("This golfer qualified for the U.S. Women’s Amateur Public Links Championship at the age of 10","Michelle Wie");
NewHint("In 2002, the U.S. Open was played at a public golf course for the first time. It is a course in a New York state park that was designed by A.W. Tillinghast. Name the course","Bethpage Black");
NewHint("This was Babe Didrikson Zaharias' given first name","Mildred");
NewHint("This English-born golfer nicknamed Lighthorse holds the record for most PGA Tour wins - 31 - without a major","Harry Cooper");