var wordlist = new Array();
var n;
var total = 0;
var correct;
var show = false;
var mySlide2 ;
var matchcase = 1;
var currentsound = '';
var t;
var soundon = true;

function applicationInit()
{
	mySlide2 = new Fx.Slide('card', {mode: 'horizontal'});
	mySlide2.hide();
	t=setTimeout(showstart,3210);
}

window.onload = applicationInit;

function showstart() 
{
	document.getElementById('message').style.display = 'block';
 	document.getElementById('wait').style.display = 'none';
 	soundon = false;
}

function start()
{
	n = 0;
	total = 0;
	correct = 0;
	
	var message = document.getElementById('message');
	message.innerHTML = 'Look, Memorise, Write!';

	
	//get list
	var list = document.getElementById('list');
	words = list.getElementsByTagName('li');

	for (i=0; i<words.length; i++) {
		wordlist[i] = words[i].innerHTML;
	}
	total = words.length;
	
	//stop if no spellings
	if (total==0) 
	{
		message.innerHTML = 'Sorry, there are no spellings in this list.';
		return false;
	}
	
	
		
	//hide list
	list.innerHTML = '';
	
	//display first word
	document.getElementById('card').innerHTML = wordlist[0];
	if (wordlist[0].length > 13) {
		document.getElementById('card').setAttribute('class','longword');
	}
	show=true;
	mySlide2.slideIn();
	
	//play sound if availiable
	//soundManager.stop('temp');
	if (sounds[0]!='') 
	{
		soundManager.play('temp0',"uploads/"+sounds[0]);
		currentsound = 'temp0';
		document.getElementById('replay').style.background = 'url(icon/replay.gif)';
	}
	else
	{
		document.getElementById('replay').style.background = 'url(icon/replaygrey.gif)';
		currentsound = '';
	}
	
	//enable input box
	document.getElementById('word').disabled = false;
	document.getElementById('word').focus();
	n++;
	return true;
}

function enter()
{
	document.getElementById('card').setAttribute('class','');
	if (currentsound!='') soundManager.stop(currentsound);
	//process word
	var entered = document.getElementById('word').value;
	//entered = entered.toLowerCase();
	
	var outerlist = document.getElementById('listouter');
	
	//put word on list
	var list = document.getElementById('list');
	var next = document.createElement('li');
	next.innerHTML = wordlist[n-1];
	
	var compare = entered.replace(/^\s+|\s+$/g,"");
	var compare2 = wordlist[n-1].replace(/^\s+|\s+$/g,"");
	
	//if not matching case compare lower case versions
	if (matchcase==0)
	{
		compare = compare.toLowerCase();
		compare2 = compare2.toLowerCase();
	}	
	
	if (compare == compare2) 
	{
		
		correct++;
		next.className = 'correct';
	}
	else
	{
		
		next.className = 'incorrect';
	}
	
	list.appendChild(next);

	n++;
	
	//check if new word
	if (n>total)
	{
		//finish
		document.getElementById('word').disabled = true;
		document.getElementById('word').value = '';
		document.getElementById('card').innerHTML = '';
		show=false;
		document.getElementById('message').innerHTML = "You got "+correct+" out of "+total+" spellings correct. <a href='#' onclick='return start()'>Click here</a> to try again.";
		
		//add total to list
		var list = document.getElementById('list');
		var next = document.createElement('p');
		next.innerHTML = correct+'/'+total;
		list.appendChild(next);
		
		document.getElementById('replay').style.background = 'url(icon/replaygrey.gif)';
		currentsound='';
		n=0;
		
	}
	else
	{
		//newword
		temp = wordlist[n-1].replace(' ', '&nbsp;');
		document.getElementById('card').innerHTML = temp;
		if (temp.length > 13) {
			document.getElementById('card').setAttribute('class','longword');
		}
		show=true;
		mySlide2.slideIn();
		
		//play sound if availiable
		//if (sounds[n-1]!='') soundManager.play('temp',"uploads/"+sounds[n-1]);
		
		//play sound if availiable
		if (sounds[n-1]!='') 
		{
			soundManager.play('temp'+(n-1),"uploads/"+sounds[n-1]);
			currentsound = 'temp'+(n-1)
			document.getElementById('replay').style.background = 'url(icon/replay.gif)';
		}
		else
		{
			document.getElementById('replay').style.background = 'url(icon/replaygrey.gif)';
			currentsound='';
		}
		
		document.getElementById('word').value = '';
	}
	
	outerlist.scrollTop = outerlist.scrollHeight;
	
	
	
	return false;	
}

function key()
{

	if (show)
	{	
		mySlide2.hide();
		show = false;
	}

}

function restart()
{
	if (total==0) return false;
	
	n = 0;
	total = 0;
	correct = 0;
	
	var message = document.getElementById('message');
	message.innerHTML = 'Listen and Write!';
	
	var list = document.getElementById('list');
	
	total = wordlist.length;
	
	//stop if no spellings
	if (total==0) 
	{
		message.innerHTML = 'Sorry, there are no spellings in this list.';
		return false;
	}
	
	list.innerHTML = '';
	
	//display first word
	document.getElementById('card').innerHTML = wordlist[0];
	show=true;
	mySlide2.hide();
	//mySlide2.slideIn();
	
	//play sound if availiable
	if (currentsound!='') soundManager.stop(currentsound);
	if (sounds[0]!='') 
	{
		soundManager.play('temp0',"uploads/"+sounds[0]);
		currentsound = 'temp0';
		document.getElementById('replay').style.background = 'url(icon/replay.gif)';
	}
	else
	{
		document.getElementById('replay').style.background = 'url(icon/replaygrey.gif)';
		currentsound = '';
	}
	
	
	//enable input box
	document.getElementById('word').disabled = false;
	document.getElementById('word').focus();
	n++;
	
	return false;
}

function replay () 
{
	if (currentsound!='') soundManager.stop(currentsound);
	if (n>0 && sounds[n-1]!='') soundManager.play(currentsound,"uploads/"+sounds[n-1]);
	document.getElementById('word').focus();
}
