///////////// ENGINE //////////////////
var flashEngineLoaded=false;
var flashEngineFailed=false;
var javaEngineLoaded=false;
var javaEngineFailed=false;

var flashEngine=null;
var javaEngine=null;

var engineLoaded=false;
var selectedEngine="";

function flashEngineInitPassed()
{
	flashEngineLoaded=true;
	if (!flashEngineFailed) 
	{
		appendInfo("Flash - OK!");
		soundEngineInitPassed("flash");
	}
}
function flashEngineInitFailed()
{
	flashEngineFailed=true;
}

function soundEngineInitPassed(engine)
{
	selectedEngine=engine;	
	engineLoaded=true;	
	hideInfoDivEvent();
}

var soundPlaying=false;

function playSound()
{
	if (selectedEngine=="flash")
		playFlashSound(currentTempo);

	if (selectedEngine=="flash" || selectedEngine=="java" || selectedEngine==midi) playSoundUpdate();
}
function stopSound()
{
	if (selectedEngine=="flash")
		stopFlashSound();	
	stopSoundUpdate();
}
function playSoundUpdate()
{
	soundPlaying=true;
	document.getElementById("startButton").innerHTML="STOP";
}
function stopSoundUpdate()
{
	soundPlaying=false;
	document.getElementById("startButton").innerHTML="START";
}

//function testEIResult()
//{
//	alert("testEIResult");
//}
function playFlashSound()
{
//	flashEngine.testEI();
	flashEngine.playSound(""+currentTempo);	
}
function stopFlashSound()
{
	flashEngine.stopSound();
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
	  return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
	if (document.embeds && document.embeds[movieName])
	{  	
	  return document.embeds[movieName]; 
	 }
  }
  else 
  {
	return document.getElementById(movieName);
  }
}

function isEngineLoaded()
{
	if (!engineLoaded) return false;
	if (selectedEngine=="flash")
	{
		if (flashEngine==null)
			flashEngine=getFlashMovieObject("AcessibilityMetronome");
		if (flashEngine!=null) return true;
	}
	return false;
}

///////////////////////////////////////////////////
function startStopButtonClick()
{
	if (isEngineLoaded())
	{
		if (soundPlaying) stopSound();
		else playSound();
//		window.status+=".";
	}
}

function appendInfo(text)
{
	document.getElementById("infoDiv").innerHTML+=text;
}
function hideInfoDivEvent()
{
	setTimeout("hideInfoDiv()",2000);
}
function hideInfoDiv()
{
	document.getElementById("infoDiv").style.display="none";
}

///////////////////////// TEMPO /////////////////////////
var currentTempo=90;
var minTempo=20;
var maxTempo=330;

function tempoInputChange()
{
	setTempo(document.getElementById("tempoInput").value);
}
function tempoInputKeyPressed(event)
{
	try{
		if (isNaN(event.keyCode)) 
			return;
		if (parseInt(event.keyCode)==13)
			tempoInputChange();
	}catch(err)
	{}
}
function increaseTempo()
{
	setTempo(currentTempo+1);
}
function decreaseTempo()
{
	setTempo(currentTempo-1);
}
var setTempoCounter=0;
function setTempo(tempo)
{
	tapCancel();
	if (isNaN(parseInt(tempo)) || parseInt(tempo)+""!=tempo|| tempo<minTempo || tempo>maxTempo || tempo==currentTempo) 
	{
		document.getElementById("tempoInput").value=currentTempo;
		return;
	}
	
	currentTempo=parseInt(tempo);
	if (soundPlaying)
		playSound();
	document.getElementById("tempoInput").value=tempo;
}


///////////////////////// TAP /////////////////////////
var tapping=false;
var tapOffString="TAP&nbsp;here&nbsp;for&nbsp;custom&nbsp;tempo";
var tapOnString="TAP&nbsp;with&nbsp;enter&nbsp;or&nbsp;spacebar";
var tapArray=new Array();
var tapLast=-1;
var tapAvg5=-1;
var tapAvg20=-1;

function tapValues(tString,tInfo,t1,t5,t20,tCancel)
{
	document.getElementById("tapButton").innerHTML=tString;

	tid=document.getElementById("tapInfoDiv");
	if (tInfo)
		tid.style.visibility="visible";
	else
		tid.style.visibility="hidden";

	tcd=document.getElementById("tapCancel");
	if (tCancel)
		tcd.style.visibility="visible";
	else
		tcd.style.visibility="hidden";
	
	t1d=document.getElementById("tapLast");
	if (t1>0)
		t1d.style.visibility="visible";
	else
		t1d.style.visibility="hidden";
	t1d.innerHTML=t1;

	t5d=document.getElementById("tapAvg5");
	if (t5>0)
		t5d.style.visibility="visible";
	else
		t5d.style.visibility="hidden";
	t5d.innerHTML=t5;

	t20d=document.getElementById("tapAvg20");
	if (t20>0)
		t20d.style.visibility="visible";
	else
		t20d.style.visibility="hidden";
	t20d.innerHTML=t20;

}

function tapCancel()
{
	tapping=false;
	tapValues(tapOffString,false,-1,-1,-1,false);
	window.status="";
}
function tapStart()
{
	tapping=true;				
	tapArray=new Array();
	tapLast=-1;
	tapAvg5=-1;
	tapAvg20=-1;				
	tapValues(tapOnString,true,tapLast,tapAvg5,tapAvg20,true);
}

function tapEvent()
{
	tapArray[tapArray.length]=new Date().getTime();
	if (tapArray.length>1)
		tapLast=Math.round(60000/(tapArray[tapArray.length-1]-tapArray[tapArray.length-2]));
	if (tapArray.length>5)
		tapAvg5=Math.round(5*60000/(tapArray[tapArray.length-1]-tapArray[tapArray.length-6]));
	if (tapArray.length>20)
		tapAvg20=Math.round(20*60000/(tapArray[tapArray.length-1]-tapArray[tapArray.length-21]));
	tapValues(tapOnString,true,tapLast,tapAvg5,tapAvg20,true);
	window.status+=tapArray.length%10;
}
function tapButtonClick()
{
	if (!tapping)
		tapStart();
}
function tapKeypressed(e)
{
    e=e || event;
	if (!tapping) return;
	try{
		if (isNaN(e.keyCode)) 
			return;
		if (parseInt(e.keyCode)==13 || parseInt(e.keyCode)==32)
			tapEvent();
	}catch(err)
	{}
}
			

