//Much cleaner than the editor script, we simply load all the captions with timecodes, and pass the data to the playingDaemon.
function onYouTubePlayerReady(playerId){
	playerId = unescape(playerId);
	var ytplayer = document.getElementById(playerId);
	var allCaps = document.getElementById(playerId + "_captionContainer").getElementsByTagName("div");
	var capIndex = new Array();
	for ( var i=allCaps.length-1; i>=0; --i ){
		capIndex[i] = new Array(3);
		capIndex[i][0] = allCaps[i].id.split("_tc")[1];
		if (allCaps[i + 1]) {
			capIndex[i][1] = allCaps[i + 1].id.split("_tc")[1];
		}
		else {capIndex[i][1] = 600000;}
		capIndex[i][2] = allCaps[i];
	}
	allCaps = null;

	setInterval(function() {playingDaemon(ytplayer, capIndex, playerId)}, 100);
}


function playingDaemon(ytplayer, capIndex, playerId){
	if (ytplayer.getPlayerState() == 1){
		var time = Math.round(ytplayer.getCurrentTime()*10)/10;
		//document.getElementById("timeCode").innerHTML = time;
		var caption = document.getElementById(playerId + "_tc" + time);
		for ( var i=capIndex.length-1; i>=0; --i ){
			if (capIndex[i][0] <= time && capIndex[i][1] > time) {
				capIndex[i][2].style.display = "block";
			}
			else {capIndex[i][2].style.display = "none";}
		}
	}
}

function loadYTCMovie(movie) {
	// Attach the sylesheet again; the @import seems to break in odd conditions.
	// Creates a duplicate link rel for the stylesheet, but so do multiple YTC embeds on the same page.
	var headID = document.getElementsByTagName("head")[0];
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = 'http://www.spacetoast.net/caption/caption.css';
	headID.appendChild(cssNode);
	// Use SFWObject to load the YouTube video. Note the wmode param, which IE demands for overlaying onto a Flash movie.
	var params = { allowScriptAccess: "always", bgcolor: "#cccccc", wmode: "transparent" };
	var atts = { id: movie };
	swfobject.embedSWF("http://www.youtube.com/v/" + movie + "&amp;border=0&amp;enablejsapi=1&amp;playerapiid=" + movie, movie + "_ytapiplayer", "425", "344", "8", null, null, params, atts);
}

function loadYTCMovie480(movie) {
	// Attach the sylesheet again; the @import seems to break in odd conditions.
	// Creates a duplicate link rel for the stylesheet, but so do multiple YTC embeds on the same page.
	var headID = document.getElementsByTagName("head")[0];
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = 'http://www.spacetoast.net/caption/caption.css';
	headID.appendChild(cssNode);
	// Use SFWObject to load the YouTube video. Note the wmode param, which IE demands for overlaying onto a Flash movie.
	var params = { allowScriptAccess: "always", bgcolor: "#cccccc", wmode: "transparent" };
	var atts = { id: movie };
	swfobject.embedSWF("http://www.youtube.com/v/" + movie + "&amp;border=0&amp;enablejsapi=1&amp;playerapiid=" + movie, movie + "_ytapiplayer", "480", "385", "8", null, null, params, atts);
}