var gamesUsed = 0;
var bunnyIns = null;  //instruction window is a property of document

function closeInstructions()
	{
	if(bunnyIns != null)
	  {
	  	bunnyIns.close();
		bunnyIns = null;
	  }
	}

function hangman_DoFSCommand(command, args) {
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	if(command == "init")
		{
		initGame();
		}
  	else if(command == "press")
		{
		keyPress(args);
		}
  	else if(command == "start")
		{
		game = new game();
		}
	else if(command == "instructions")
		{
		if(navigator.appName == "Microsoft Internet Explorer")
			var URL = "../inst.cfm";
		else
			{
			var currURL = document.location.href;
			var temp = new Array();
			temp = currURL.split("/");
			var length = temp[temp.length-1].length + temp[temp.length-2].length + 2;
			currURL=currURL.substring(0, currURL.length - length);
			var URL = currURL + "/inst.cfm";
			}
			if(bunnyIns == null)
				bunnyIns = window.open(URL, "bunnyInstructions", "height=400,width=350,directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,left=0,top=0");
			setTimeout("bunnyIns.focus();", 500); 
		}
	}
//var hangman= InternetExplorer ? hangman : document.hangman;
//Game Class
function game()
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	this.usedWords = new Array();
	this.roundsPlayed = 0;
	this.wordUsed = wordUsed;
	}
function wordUsed(wordIndex)
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	for(i = 0; i < this.roundsPlayed; i++)
		{
		if(wordIndex == this.usedWords[roundsPlayed])
			return true;
		}
	return false;
	}

//Game Class
//Word Class
function word(word)
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	this.currentWord = word;
	this.size = this.currentWord.length;
	this.contains = contains;
	}

function contains(letter)
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	var index = 0;
	while(index < this.size)
		{
		if(letter == this.currentWord.charAt(index))
			return true;
		else
			index++;
		}
	return false;
	}//Class

function setCat(theCat)
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	var size = theCat.length;
	for(i = 1; i <= 25; i++)
		{
		hangman.TGotoLabel("_flash0/Category/" + i, "blank");
		}
	for(var i = 0; i < size; i++)
		{
		hangman.TGotoLabel("_flash0/category/" + (i + 1), theCat.charAt(i));
		}
	}
		
		

function initGame()
	{
	setCat(category);
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	var wordList = new Array();
	wordList = gameContent;
	if((game.roundsPlayed) >= wordList.length)
		{
		hangman.TGotoLabel("_flash0", "gameOver");
		}
	else
		{
		setWord(wordList[gamesUsed], wordList[gamesUsed].length);
		var limit = wordList[gamesUsed].length;
		hangman.TGotoLabel("_flash0", wordList[gamesUsed].length);
	gameWord = new word(wordList[gamesUsed]);
	usedList = new word("!");
	hitIndex = 0;
	missIndex = 0;
	hangman.TGotoFrame("_flash0/Counter/Number", missIndex);
	difficulty = 6;
	
	for(var index = 1; index <= limit; index++)
		{
		var target = "_flash0/Block " + index;
		hangman.TGotoLabel(target, "Closed");
		hangman.TPlay(target);
		}
	resetKeyboard();
	
	//game.usedWords[] = wordIndex;
	game.roundsPlayed++;

	if(gamesUsed == wordList.length)
		gamesUsed = 0;
	else 
		gamesUsed++;
		}
	}

function resetKeyboard()
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	var kbdWord = "abcdefghijklmnopqrstuvwxyz";
	for(var index = 0; index < kbdWord.length; index++)
		{var theLetter = kbdWord.charAt(index);
		hangman.TGotoLabel("_flash0/Keyboard/" + theLetter, theLetter);
		}
	}
 
function setWord(word, size)
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	var targetList = new Array(size);
	for(index = 0; index < size; index++)
		{
		targetList[index] = "_flash0/Letter " + (index + 1);
		}		
	for(index = 0; index < size; index++)
		{
		hangman.TGotoLabel(targetList[index] , word.charAt(index));
		}
	
	}

function getCurrentWord()
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	var word;
	word = hangman.TCurrentLabel("_flash0/Letter 1");
	for(index = 1; index <= wordList[randomWordIndex].length; index++)
		{
		word = word + hangman.TCurrentLabel("_flash0/Letter " + (index + 1));
		}
	return word;
	} 

function keyPress(theLetter)
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	if(gameWord.contains(theLetter) && !(usedList.contains(theLetter)))
		{
		var target = "_flash0/Block " + (gameWord.currentWord.indexOf(theLetter)+1);
		hangman.TGotoLabel(target, "Open");
		hangman.TGotoLabel("_flash0/Keyboard/" + theLetter, "blank");
		usedList.currentWord = usedList.currentWord + theLetter;
		usedList.size = usedList.size + 1;
		hitIndex = hitIndex + 1;
		if(hitIndex >= gameWord.size)
			win();
		}
	else if( !(gameWord.contains(theLetter) ) && ! (usedList.contains(theLetter) ) )
		{
		var target = "_flash0/Block " + (gameWord.currentWord.indexOf(theLetter)+1);
		hangman.TGotoLabel("_flash0/Keyboard/" + theLetter, "blank");
		usedList.currentWord = usedList.currentWord + theLetter;
		usedList.size = usedList.size + 1;
		missIndex = missIndex + 1;
		hangman.TGotoFrame("_flash0/Counter/Number", missIndex);
		if(missIndex >= difficulty)
			lose();
		}		
	}

function lose()
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	for(i = 1; i <= 5; i++)
		{
		hangman.TGotoLabel("_flash0/block " + i, "Open");
		}
	hangman.Play();
	hangman.TGotoLabel("_flash0/Miss Animation", "lose");
	}

function win()
	{
	var hangman = InternetExplorer ? window.hangman : window.document.hangman;
	hangman.Play();
	hangman.TGotoLabel("_flash0/Miss Animation", "win");
	}	

function  fillList(wordList)
	{
	wordList = gameContent;
	}

function MD_random(r1, r2) {
  if (r2 > r1) return (Math.round(Math.random()*(r2-r1))+r1);
  else return (Math.round(Math.random()*(r1-r2))+r2);
}




