wordToSpellOut = "";
wordIndex = 0;
loadComplete = false;
whereToSpell = "bigone";	
bigoneObj = null;
doneBlinkingBoolean = true;
comingSoon = false;

// Entry point for the test results page
function teachMeButton(wordToPlay)
{
	cleanW = cleanWord(wordToPlay);
	$('#spelledWordCell'+cleanW).hide();
	$('#spelledWordCellTeachMe'+cleanW).show();	
	wordSelectObject = document.getElementById('selectWord');
	i=0;
	while (i<wordSelectObject.options.length)
	{
		if (wordSelectObject.options[i].text == wordToPlay)
		{
			wordSelectObject.selectedIndex = i;
			i = wordSelectObject.options.length;
		}
		i++;
	}
	whereToSpell = 'spellItOut' + cleanW;
	bigoneObj = document.getElementById(whereToSpell);	
	selectedWord = wordSelectObject.options[wordSelectObject.selectedIndex].text;
	spellBigOneOut(selectedWord);
	updateWordCounter();			
	return false;
}

// Entry point for the teachme page
function onWordSelect()
{
	bigoneObj = document.getElementById(whereToSpell);		
	wordSelectObject = document.getElementById('selectWord');
	selectedWord = wordSelectObject.options[wordSelectObject.selectedIndex].text;
	comingSoon = false;
	if (selectedWord.indexOf("Coming Soon") > 0)
	{
		selectedWord = selectedWord.replace(" -- Coming Soon --",'');		
		comingSoon = true;
	}
	updateWordCounter();
	spellBigOneOut(selectedWord);
}

function pausecomp(millis) 
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
} 

function spellBigOneOut(wordToSpell)
{
		playAll = false;
		wordToSpellOut = wordToSpell;
		spellWordOut();
}

function spellWordOut()
{
	if (!loadComplete)
	{
		alert('Please be patient... still loading sounds...')
		return false;
	}
	wordIndex = 0;
	//showAndPlay();	
	playWordFirst();
}

function showAndPlay() 
{
	// before playing any new word... stop all sounds so there are no overlaps
	soundManager.stopAll();
	// switch the display class to dark
	//bigoneObj = document.getElementById(whereToSpell);
	setOpacity(10);
	// get the text area to display the word in
	wordSpan = document.getElementById(whereToSpell);
	// display the word part
	wordPart = wordToSpellOut.substring(0,wordIndex+1);
	wordSpan.innerHTML = wordPart;	
	// play the character sound
	// we have to convert the letter to spell out to lower case in order to match it up with the 
	// corresponding sound manager lower case letter index
	originalLetter = wordToSpellOut.substring(wordIndex,wordIndex+1); 
	letterToSound = originalLetter.toLowerCase();
	switch (letterToSound)
	{
		case "-":
			letterToSound = "hyphen";
			break;
		case " ":
			letterToSound = "space";
			break;
		case "'":
			letterToSound = "apos";
			break;
        case ".":
            letterToSound = "period";
            break;
	}
	// check to see if the letter to be sounded is a capital
	re = /^[A-Z]/;
	if (originalLetter.match(re))
	{
		letterToSound = "_cap" + letterToSound;
	}
	
	if (wordIndex == wordToSpellOut.length-1)
	{
		if (!comingSoon)
		{
			soundManager.play(letterToSound,{onfinish:playWord});		
		}
		else
		{
			soundManager.play(letterToSound);		
		}
	}
	else
	{
		soundManager.play(letterToSound,{onfinish:playNextChar});		
	}
}

function playNextChar()
{
	wordIndex++;
	if (wordIndex < wordToSpellOut.length)
	{
		showAndPlay();
	}
}

function playWord()
{
	// replace "dashes" with "_"
	// replace "spaces" with "_"
	// repalce "single quote" with "_"
	wordSound = cleanWord(wordToSpellOut);
	soundManager.play(wordSound, {onfinish:playSentence});	
}

function playWordFirst()
{
	// show the word as part of saying it
	setOpacity(10);
	wordSpan = document.getElementById(whereToSpell);
	wordSpan.innerHTML = wordToSpellOut;	
	wordSound = cleanWord(wordToSpellOut);
	if (!comingSoon)
	{
		soundManager.play(wordSound, {onfinish:showAndPlay});	
	}
	else
	{
		showAndPlay();
	}
}

function playSentence()
{
	// replace "dashes" with "_"
	// replace "spaces" with "_"
	// repalce "single quote" with "_"
	sentenceSound = cleanWord(wordToSpellOut);
	soundManager.play("sentence"+sentenceSound,{onfinish: playFinalWord, onplay:fadeWord});
}

function playFinalWord()
{	
	//fadeWord();
	// replace "dashes" with "_"
	// replace "spaces" with "_"
	// repalce "single quote" with "_"
	wordSound = cleanWord(wordToSpellOut);
	soundManager.play(wordSound, {onfinish:blinkWord});
}

function endOfSoundAction()
{
	if (playAll)
	{
		wordSelectObject = document.getElementById('selectWord');
		if (globalWordIndex < wordSelectObject.options.length)
		{
			wordToSpellOut = wordSelectObject.options[globalWordIndex].text;
			wordSelectObject.selectedIndex = globalWordIndex;
			globalWordIndex++;			
			updateWordCounter();
			spellWordOut();
		}
	}
		
}


function nextWord()
{
	wordSelectObject = document.getElementById('selectWord');
	selectedWordIndex = wordSelectObject.selectedIndex;
	if (selectedWordIndex < wordSelectObject.options.length-1)
	{
		selectedWordIndex++;
	}
	else
	{
		selectedWordIndex = 0;
	}
	wordSelectObject.selectedIndex = selectedWordIndex;		
	selectedWord = wordSelectObject.options[wordSelectObject.selectedIndex].text;
	spellBigOneOut(selectedWord);
	updateWordCounter();
}

function prevWord()
{
	wordSelectObject = document.getElementById('selectWord');
	selectedWordIndex = wordSelectObject.selectedIndex;
	
	if (selectedWordIndex > 0)
	{
		selectedWordIndex--;
	}
	else
	{
		selectedWordIndex = wordSelectObject.options.length-1;
	}
	wordSelectObject.selectedIndex = selectedWordIndex;		
	selectedWord = wordSelectObject.options[wordSelectObject.selectedIndex].text;
	spellBigOneOut(selectedWord);
	updateWordCounter();	
}


function spellAllWords()
{
	wordSelectObject = document.getElementById('selectWord');
	globalWordIndex = 0;
	globalEndIndex = wordSelectObject.options.length - 1;
	playAll = true;
	endOfSoundAction();
}

function updateWordCounter()
{
	wordSelectObject = document.getElementById('selectWord');
	selectedWordIndex = wordSelectObject.selectedIndex + 1;
	numberOfWords = wordSelectObject.options.length;
	document.getElementById('wordCounter').innerHTML = "Word " + selectedWordIndex + " of " + numberOfWords + " words";
}

function fadeWord()
{
	//bigoneObj = document.getElementById(whereToSpell);
	for (var i=0;i<11;i++)
		setTimeout('setOpacity('+(10-i)+')',100*i);		
	// wait until the other timeout functions complete before showing word again
	setTimeout('unfadeWord()',100*11);
}

function unfadeWord()
{
	//bigoneObj = document.getElementById(whereToSpell);
	for (var i=0;i<11;i++)
		setTimeout('setOpacity('+(i)+')',100*i);		
}

function setOpacity(value)
{
	//bigoneObj = document.getElementById(whereToSpell);
	bigoneObj.style.opacity = value/10;
	bigoneObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function blinkWord()
{		
  doneBlinkingBoolean = false;
	setTimeout('setOpacity(10)',500);
	setTimeout('setOpacity(2)',1000);
	setTimeout('endOfSoundAction()',3000);
		//setTimeout('updateWordClass("medium")',3000);
}

function playButtonWord()
{
	soundManager.play("buttonWord");
}
function playButtonMe()
{
	soundManager.play("buttonTeachMe2");
}
function cleanWord(someWord)
{
	return someWord.replace("-","_").replace(" ","_").replace("'","_").replace(".","_").replace(",","_").toLowerCase();
}