// Variables
var ytl_player=null;
var ytl_videoPlaylistIndex=0;

// YouTube Player
function loadYouTubePlayerLoop(id, videoPlaylist) {
	ytl_videoPlaylist=videoPlaylist;
	swfobject.embedSWF(
		"http://www.youtube.com/v/"+ytl_videoPlaylist[ytl_videoPlaylistIndex]+"?enablejsapi=1&version=3&rel=0",
		id,
		"591",
		"335",
		"8",
		null,
		null,
		{ allowScriptAccess: "always", wmode: "transparent" },
		{ id: "ytl_player" }
	);
}

function onYouTubePlayerReady(playerId) {
	// Get the element
	ytl_player=document.getElementById("ytl_player");

	// Attach events
	ytl_player.addEventListener("onStateChange", "ytl_onPlayerStateChange");
	ytl_player.addEventListener("onError", "ytl_onPlayerError");
	
	// Mute
	//ytl_player.mute();

	// Play the video
	ytl_player.playVideo();
}

function ytl_playNextVideo() {
	ytl_videoPlaylistIndex++;
	if (ytl_videoPlaylistIndex>ytl_videoPlaylist.length-1) ytl_videoPlaylistIndex=0;
	ytl_player.loadVideoById(ytl_videoPlaylist[ytl_videoPlaylistIndex]);
}

function ytl_onPlayerStateChange(newState) {
	switch (newState) {
		case -1:	// Unstarted
			break;
		case 0:	// Ended
			ytl_playNextVideo();
			break;
		case 1:	// Playing
			break;
		case 2:	// Paused
			break;
		case 3:	// Buffering
			break;
		case 5:	// Video Cued
			break;
		default:
			// Nothing
	}
}

function ytl_onPlayerError(errorCode) {
	alert("An error occured of type:" + errorCode);
}
