google.load("swfobject", "2.2");

var ytplayer = null;

function updateHTML(elmId, value) {
	document.getElementById(elmId).innerHTML = value;
}

function setytplayerState(newState) {
	updateHTML("playerstate", newState);
}

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	ytplayer.addEventListener("onError", "onPlayerError");
	loadNewVideo('q-RlTsOL9xo','0');
	play();
}

function onytplayerStateChange(newState) {
	if (newState == "0") {
		loadNewVideo('q-RlTsOL9xo','0');
		play();
	}
}

function onPlayerError(errorCode) {
	alert("An error occured: " + errorCode);
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
	if (ytplayer) {
		ytplayer.loadVideoById(id, parseInt(startSeconds));
	} else {
		alert("ytplayer is null");
	}
}

function play() {
	if (ytplayer) {
		ytplayer.playVideo();
	}
}


