// JavaScript Document

function Movie(url, w, h)
{
	this.url = url;
	this.width = w;
	this.height = h;
}

var movies = new Array();

movies.push(new Movie("movies/Coke_Two_Guys.mov", 400, 226));
movies.push(new Movie("movies/Ford_Jonathan.mov", 400, 226));
movies.push(new Movie("movies/McDonalds_Same_Guy.mov", 320, 240));
movies.push(new Movie("movies/Hyundai_Tunnels.mov", 320, 240));
movies.push(new Movie("movies/T-Mobile_Flashcards.mov", 320, 240));
movies.push(new Movie("movies/Amp_Camp.mov", 320, 240));
movies.push(new Movie("movies/Street_Tests.mov", 400, 226));
movies.push(new Movie("movies/RockTheVote_Ask_Yourself.mov", 350, 264));
movies.push(new Movie("movies/Hyundai_Holiday.mov", 320, 240));
movies.push(new Movie("movies/PETA_Beatdown.mov", 350, 264));
movies.push(new Movie("movies/Kites.mov", 320, 240));
movies.push(new Movie("movies/Amp_Shotgun.mov", 400, 226));

var currentIndex = 0;

var intervalID = -1;

function closeMovie()
{
	clearInterval(intervalID);
	intervalID = -1;
	document.getElementById("qtClose").innerHTML = "&nbsp;";
	document.getElementById("qtmovie").innerHTML = "<div class=\"playAll\"><a href=\"javascript:playAll();\"><img src=\"images/play_all.gif\" /></a></div>";
		
}

//called by a button
function playMovie(index)
{
	clearInterval(intervalID);
	intervalID = -1;
	startMovie(index);
	
	

}

//
function startMovie(index)
{
	//get the movie specified from the list
	var mov = movies[index];
	
	//show the close button
	document.getElementById("qtClose").innerHTML = "<a href=\"javascript:closeMovie();\">Close</a>";
	document.getElementById("qtClose").style.width = (Math.round((400-mov.width)/2)+mov.width)+"px";
	document.getElementById("movieHolderInner").style.height = (mov.height+16)+"px";
	
	//generate the quicktime text
	var qtString = QT_GenerateOBJECTText(mov.url, mov.width, mov.height+16, '', 'emb#NAME', 'movie_embed1', 'obj#Id', 'movie1', 'EnableJavascript', 'True',
	  	 'controller', 'True', 'bgcolor', '000000', 'AUTOPLAY', 'True', 'SCALE', 'Aspect', 'SaveEmbedTags', 'True', 'postdomevents', 'True');

	//add it to the qtmovie
	document.getElementById("qtmovie").innerHTML = qtString;
	
}


function writeTrace(str)
{
	document.getElementById("tracer").innerHTML = str;	
}

function movieEnd()
{
		
	//alert("movie ended");
	
	if (currentIndex < movies.length)
	{
		currentIndex++;
		playNext();
	}
	else
	{
		closeMovie();
	}
	
	
}

function monitorMovie()
{
	if (document.movie1 != null)
	{
		if (document.movie1.GetTime() == document.movie1.GetDuration())
		{
			clearInterval(intervalID);
			intervalID = -1;
			movieEnd();
		}
	}
	else 
	{
		clearInterval(intervalID);	
		intervalID = -1;
	}
	
}

function playNext()
{
	startMovie(currentIndex);
	
	if (_QTIsMSIE())
	{
		if (intervalID < 0)
		{
			intervalID = setInterval("monitorMovie()", 100);
		}
	}
	else 
	{
		//add the event listener
		rListener('qt_ended', 'movie1', 'movie_embed1', movieEnd);
	}
}


function playAll()
{
	
	currentIndex = 0;
	playNext();
	
	
}


function rEvent(obj, evt, handler, captures)
{

	if ( document.addEventListener )
	{
		obj.addEventListener(evt, handler, captures);
	}
	else
	{
		// IE
		obj.attachEvent('on' + evt, handler);
	}

}

function rListener(eventName, objID, embedID, listenerFcn)

{

	var obj = document.getElementById(objID);
	
	if ( !obj )
	{
		obj = document.getElementById(embedID);
	}
	
	if ( obj )
	{
		
		rEvent(obj, eventName, listenerFcn, false);
	}

}


