//Copyright 2006 Virginia Tech

var theTime;
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(){
	if (counter < maximum && die != 1){
		newTime=document.themovie.GetTime();
		if (Math.abs(newTime - theTime) >= 1.2*timeJump){
			//alert("caught it");
			//ugly  bit
			for (forcounter=0;forcounter < numevents;forcounter++){
				if (events[forcounter]["time"] > newTime){
					toget=forcounter--;
					break;	
				}
			}
			catchUp(toget);
		}
		
		theTime=newTime;

		//check on this
		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){
		//ugly!
		var div = document.getElementById("text");
		div.innerHTML = '';
	}
	else{
		top=forcounter;
		do{
			forcounter--;
			if (forcounter ==0 || (events[forcounter]["type"] == "clear" && events[forcounter]["target"] == "text")){
//			alert(forcounter);
				break;
			}
		}
		while (forcounter >= 0)	
		//alert (forcounter);
		while (forcounter <= top){
			//alert(forcounter);
			lastChanged = forcounter;
			doChange(events[forcounter]["type"], forcounter);
			forcounter++;
		}
	}
}

function doChange(type, forcounter){
	switch(type){
		case("clear"):
			updatePage(forcounter);
			break;
		case("newlist"):
			createList(forcounter);
			break;
		case("appendli"):
			updateList(forcounter);
			break;
		case("appendToBullet"):
			appendToBullet(forcounter);
			break;
	}
}


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

function createList(number){
	var div = document.getElementById(events[number]["target"]);
	var newlist = document.createElement("ul");
	newlist.name=events[number]["listname"];
	newlist.id=events[number]["listname"];
	//alert(window.events[number]["listname"]);
	if (!window.events[number]["classname"]){
		theClass=events[number]["listname"];
	}
	else{
		theClass=events[number]["classname"];
	}
	newlist.className=theClass;
	var newbullet = document.createElement("li");
	newbullet.innerHTML = events[number]["content"];
	newlist.appendChild(newbullet);
	div.appendChild(newlist);
}

function updateList(number){
	var list = document.getElementById(events[number]["listname"]);
	//alert (list);
	//alert (list.innerHTML);
	if (list==null){
		createList(number);
	}
	else{
		var newbullet = document.createElement("li");
		newbullet.innerHTML = events[number]["content"];
		list.appendChild(newbullet);
	}
}

function appendToBullet(number){
	var bullets = document.getElementsByTagName("li");
	if (bullets==null){
		updateList(number);
	}
	else{
		//Assumes that you'll always be expanding the last bullet added	
		bullets[bullets.length-1].innerHTML = bullets[bullets.length-1].innerHTML + events[number]["content"];
	}
}

function popUp(image, title, width, height, text){
	popup = window.open("", title, 'toolbar=no,width=' + width +',height=' + height);
	popup.title="";
	popup.document.write('<a href="javascript:window.close()"><img src="' + image + '"><br>' + text + '</a>');
	popup.document.close();
}

function mediaPlayer(location, title, text){
	document.themovie.Stop();

	//display the continue info.  Do this before the new QT player to avoid lag artifacts
	var displayBox=document.getElementById("infoscreen");
	displayBox.innerHTML='<a href="javascript:continueFromMedia();"><img src="./images/Germany3/click_to_continue.gif" width="192" height="160" border="0" /></a>';
	
	popup = window.open("", title, 'toolbar=no,width=500 ,height=120');
	popup.title='';
	popup.document.write('<a href="javascript:window.close()">Close Window</a><br><object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"  width="460" height="20" ><param name="src" value="' +  location +'"><param name="autostart" value="true"><embed width="460" height="20" src="' +  location +'"  autostart="true"></embed></object><br>' + text);
	//popup.document.close();
}

function continueFromMedia(){
	var displayBox=document.getElementById("infoscreen");
	displayBox.innerHTML='';
	
	popup.close();
	
	document.themovie.Play();
	
}
		
//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");
}
		
function changeOverflow(){
	if (document.getElementById("OF").value=="Allow Overflow"){
		document.getElementById("text").style.overflow="visible";
		document.getElementById("OF").value="Disable Overflow";
		}
	else{
		document.getElementById("text").style.overflow="hidden";
		document.getElementById("OF").value="Allow Overflow";
	}
	
}

function makeHeaderList(){
	headerDiv=document.getElementById("headerlist");
	headerList=document.createElement("ul");
	for (counter=0; counter<numevents; counter++){
		findheader='(<h1>)(.*?)</h1>';
		headers=events[counter]["content"].match(findheader);
		if (headers == null){
			continue;
		}
		else{
			//alert(headers[2]);
			newLI=document.createElement("li");
			newLI.innerHTML='<a href="javascript:goTo('+ ((events[counter]["time"] - 12000)/600) + ');">' + headers[2] + '</a>';
			headerList.appendChild(newLI);
		}
	}
	headerDiv.appendChild(headerList);
}

function handlePlayMedia(target){
	//stop the movie from playing
	document.themovie.Stop();
	
	/*
	//display the continue info.  Do this before the new QT player to avoid lag artifacts
	var displayBox=document.getElementById("infoscreen");
	displayBox.innerHTML='<a href="javascript:continueFromMedia();"><img src="./images/Germany3/click_to_continue.gif" width="192" height="160" border="0" /></a>';

	var theText=document.getElementById("text");
	hiddenPlayer=document.createElement("div");
	hiddenPlayer.ID="audioPlayerDiv";
	hiddenPlayer.innerHTML='<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="0" height="0" id="audioPlayer" ><param name="src" value="'+ target +'"><param name="autostart" value="true"><param name="enablejavascript" value="true"> <embed width="0" height="0" src="' + target + '" name="audioPlayer" id="audioPlayer" enablejavascript="true" autostart="true"></embed></object>';

	//hiddenPlayer.style.visibility="hidden";
	theText.parentNode.appendChild(hiddenPlayer);
	alert(document.audioPlayer.GetTime());
	*/
}

