var n = 0;
var count = 0;
var t;
var timeout;
var sound = false;
var pause = false;
var nextevent = null;
var toolbar = "<a href='#' style='margin-right:1px;' onclick='if(blur()) blur(); doRestart();'><img src='images/toolbar/restart.gif' border='0' /></a><a href='#' onclick='if(blur()) blur(); replayWord()' style='margin-right:1px;' ><img src='images/toolbar/replay.gif' border='0' /></a><a style='margin-right:1px;'  href='#' onclick='if(blur()) blur(); doPause()' id='playpause'><img width='83' src='images/toolbar/pause.gif' border='0' /></a><a style='margin-right:1px;' href='#' onclick='if(blur()) blur(); doPrevious()'><img src='images/toolbar/previous.gif' border='0' /></a><a style='margin-right:1px;' href='#' onclick='if(blur()) blur(); doNext()'><img src='images/toolbar/next.gif' border='0' /></a><div id='info' style='position:absolute; top:0; right:0;'></div>";

function start()
{
	nextevent = 'startapp';
	timeout=setTimeout("startApp()",10000);
}

function startApp()
{
	nextevent = null;
	if (word.length > 0)
	{
		//document.getElementById("message").innerHTML = "<a href='javascript:runFlashCards()'>Play</a>";
		document.getElementById("message").innerHTML = toolbar;

		n = 0;
		document.getElementById('playpause').innerHTML = "<img src='images/toolbar/pause.gif' border='0' width='83' />";
		pause = false;
		runWord();
	}
	else
	{
		document.getElementById("message").innerHTML = "No Words! <a href='javascript:history.go(-1)'>Back</a>";
	}
}

function doPause()
{
	if (pause)
	{
		switch (nextevent)
		{
			case 'startapp':
				startApp();
				break;
			case 'playphoneme':
				playPhoneme();
				break;
			case 'runword':
				runWord();
				break;
			default:
				runWord();
		}
		document.getElementById('playpause').innerHTML = "<img src='images/toolbar/pause.gif' border='0' width='83' />";
		pause = false;
	}
	else
	{
		clearTimeout(t);
		document.getElementById('playpause').innerHTML = "<img src='images/toolbar/play.gif' border='0' width='83' />";
		pause = true;
	}
}

function replayWord()
{
	clearTimeout(t);
	document.getElementById('playpause').innerHTML = "<img src='images/toolbar/pause.gif' border='0' width='83' />";
	pause = false;
	runWord();
}


function doPrevious()
{
	if (n>0)
	{
		n--;
		clearTimeout(t);
		document.getElementById('playpause').innerHTML = "<img src='images/toolbar/pause.gif' border='0' width='83' />";
		pause = false;
		runWord();
	}
}

function doNext()
{
	if (n < (word.length-1))
	{
		n++;
		clearTimeout(t);
		document.getElementById('playpause').innerHTML = "<img src='images/toolbar/pause.gif' border='0' width='83' />";
		pause = false;
		runWord();
	}
}

function doRestart()
{
	n = 0;
	clearTimeout(t);
	document.getElementById('playpause').innerHTML = "<img src='images/toolbar/pause.gif' border='0' width='83' />";
	pause = false;
	runWord();
}
		

function playPhoneme()
{
	if (count < audio[n].length)
	{
		document.getElementById("w"+(count-1)).style.color='#000000';
		document.getElementById("w"+count).style.color = '#ff0000';
		soundManager.play('temp-'+n+'-'+count,"sounds/"+audio[n][count]+".mp3");
		
		count++;
		nextevent = 'playphoneme';
		t=setTimeout("playPhoneme()",2000);
	}
	else
	{
		nextevent = null;
		finishWord();
	}
}


function finishWord()
{
	n++;
	nextevent = 'runword';
	t=setTimeout("runWord()",3000);
	document.getElementById("w"+(count-1)).style.color="#000000";
	soundManager.play('temp'+n,"sounds/"+audioword[n-1]);
}

function runFlashCards()
{
	clearTimeout(t);
	n = 0;
	//document.getElementById("message").innerHTML = "<a href='javascript:runFlashCards()'>Click here</a> to restart...";
	document.getElementById("message").innerHTML = toolbar;

	document.getElementById("word").innerHTML = '';
	document.getElementById("info").innerHTML = 'Word '+(n+1)+" of "+word.length;
	
	for (i=0; i<wordd[n].length; i++)
	{
		var node = document.createElement('span');
		node.setAttribute('id', 'w'+i);
		node.innerHTML = wordd[n][i];
	
		document.getElementById("word").appendChild(node);
	}
		
		
	//play 1st phoneme
	count = 0;
	//alert (document.getElementById("w"+count).innerHTML);
	
	document.getElementById("w"+count).style.color = '#ff0000';
	//document.getElementById("w"+count).style.color="#ff0000;";
	soundManager.play('temp-'+n+'-'+count,"sounds/"+audio[n][count]+".mp3");
	
	count++;
	nextevent = 'playphoneme';
	t=setTimeout("playPhoneme()",2000);
	
	
}

function runWord()
{
	nextevent = null;
	if (n < word.length)
	{
		document.getElementById("word").innerHTML = word[n];
		
		document.getElementById("info").innerHTML = 'Word '+(n+1)+" of "+word.length;
		
		//play 1st phoneme
		
		count = 0;
		//id = "w"+(count-1);
		//alert (id);
		//document.getElementById("w"+(count-1)).style.color="#000000;";
		document.getElementById("w"+count).style.color="#ff0000";
		soundManager.play('temp-'+n+'-'+count,"sounds/"+audio[n][count]+".mp3");
		
		count++;
		nextevent = 'playphoneme';
		t=setTimeout("playPhoneme()",2000);
		
	}
	else
	{
		n=0;
		document.getElementById('playpause').innerHTML = "<img src='images/toolbar/play.gif' border='0' width='83' />";
		pause = true;
		document.getElementById("word").innerHTML = '';
		document.getElementById("info").innerHTML = '';
	}
}