//Copyright 2006 Virginia Tech

//Welcome to the code.  Yes, some of it is ... unsophisticated.  But it's still in proof of concept stage, so deal.
var theTime=0;
var counter=0;
var die;
//timeJump is the measurement in milliseconds of how long to wait between time checks

var timeJump=1000;

var lastChanged=-1;

var numevents=events.length;

//startupbuffer() is used to trigger the recursive call to perpectual.  It is called as an onload()
//in the body of the html
function startupbuffer(){
	//wait 20 seconds to give QT time to cue up - do not put 
	//events within the first 20 seconds of a file
	//Or maybe you can.  Needs testing under IE
	window.setTimeout('perpetual()', timeJump);
}

//Where the magic happens.
function perpetual(){
	//Run only if the maximum time has not been exceeded or the program terminated for any reason.
	if (counter < maximum && die != 1){
		//Get the time from the movie
		newTime=document.themovie.GetTime();

//		foo=document.getElementById("debug");
//		displaySeconds=Math.floor((newTime/600)%60);
//		if(displaySeconds<10){
//			extra="0";
//		}
//		else{
//			extra="";
//		}
//		displayMinutes=Math.floor(((newTime/600)-displaySeconds)/60);
//		foo.innerHTML=displayMinutes + ":" + extra + displaySeconds;
		
		//If the difference is greater than just more than the last supposed check, it means the user
		//has skipped around, and we need to change the HTML to match the streaming file
		if (Math.abs(newTime - theTime) >= 1.3*timeJump){
			//ugly  bit, to be replaced with binary search
			//We start at one, because we don't need to evaluate position 0 - it will be found if
			//position 1 is greater than the current position.
			for (forcounter=1;forcounter < numevents;forcounter++){
				//find the event that is one after the last possible current one.  Then go back by one
				if (events[forcounter]["time"] > newTime){
					toget=forcounter-1;
					break;	
				}
				else{
					toget=forcounter;
				}
			}
			//Do the update
			catchUp(toget);
		}
		
		theTime=newTime;

		//check on this - used in case the user goes all the way to the begining
		if (theTime == 0){
			theTime=1;
		}
			
		toChange = findNextEvent(theTime);

		if (toChange > -1 && toChange != lastChanged){
			lastChanged = toChange;
			doChange(events[forcounter]["type"], toChange);
		}
		
		window.setTimeout('perpetual()', timeJump);
		counter++;
	}
	
	else {
		if (die != 1){
			resetcounter=confirm("Expected Maximum length of program exceeded.  Continue with interactive environment?");
			if (resetcounter){
				counter=0;
				window.setTimeout('perpetual()', timeJump);
			}
		}
	}
}

function findNextEvent(theTime){
	for(forcounter=0; forcounter < numevents; forcounter++){
			checkClock=events[forcounter]["time"]-theTime;

			if ((checkClock >= 0) && (checkClock <= timeJump)){
				if (lastChanged == forcounter){
					//alert("bailing");
					break;
				}
			
				return forcounter;
				break;
			}
			
	}
	return -1;
}

function catchUp(forcounter){
	var top;
	if (forcounter == 0){
		var div = document.getElementById("screen");
		div.innerHTML = events[0]["content"];
		lastChanged=0;
	}
	else{
		if(lastChanged != forcounter){
			lastChanged=forcounter;
			doChange(events[forcounter]["type"], forcounter);
		}

//		top=forcounter;

//		do{
//			forcounter--;
//			if (events[forcounter]["type"] == "clear"){
//				break;
//			}
//		}
//		while (forcounter >= 0)	

//		while (forcounter <= top){
//			lastChanged = forcounter;
//			doChange(events[forcounter]["type"], forcounter);
//			forcounter++;
//		}
	}
}

function doChange(type, forcounter){
	switch(type){
		case("clear"):
			updatePage(forcounter);
			break;
		//If you're reading along, there are other options not used in this presentation
	}
}


		
function updatePage(number){
	var div = document.getElementById(events[number]["target"]);
	div.innerHTML = events[number]["content"];
}

		
//goTo() is used to go to an arbitrary time in the movie.  It takes its argument in the form of seconds
function goTo(time){
	document.themovie.Stop();
	time=time*600;
	//do something useful with this at some point
	//alert(document.themovie.GetTimeScale());

	document.themovie.SetTime(time);
	document.themovie.Play();
}

function goAny(time){
	var toDevide = time;
	var theTime= toDevide.split(":");
	var sum = parseInt(theTime[0])*60 + parseInt(theTime[1]);
	//alert(sum);
	goTo(sum);
}

function rapid(forcounter){
		document.themovie.Stop();
		if (forcounter<0){
			forcounter=0;
		}

		if (forcounter==numevents){
			return 1;
		}

		doChange(events[forcounter]["type"], forcounter);
		var div = document.getElementById("position");
		div.innerHTML = 'Counter location: ' + forcounter;
		forcounter++;
		//You would think that you could just do a while loop here, wouldn't you?
		//Well, you can't.  So welcome to the fun world of recurssion.
		window.setTimeout ('rapid("' + forcounter +'")', 100);
}

function stepThrough(){
	document.themovie.Stop();
	die=1;
	if (lastChanged < 0){
		lastChanged=0;
	}
	if (lastChanged == numevents){
		alert("Last event reached");
	}
	else{
		doChange(events[lastChanged]["type"], lastChanged);
		var div = document.getElementById("position");
		div.innerHTML = 'Counter location: ' + lastChanged;
		lastChanged++;
	}
}

//	
function getTime(){
	theTime=document.themovie.GetTime();
	theSeconds=theTime/600;
	alert(theTime + " = " + theSeconds + " seconds");
}
		


