function updateHTML(elmId, value) {
	document.getElementById(elmId).innerHTML = value;
}

function setytplayerState(newState) {
	updateHTML("playerstate", newState);
}

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
//	setInterval(updateytplayerInfo, 250);
//	updateytplayerInfo();
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	loadNewVideo(document.getElementById("loadvideoid").value, "0")
}

function onytplayerStateChange(newState) {
//	setytplayerState(newState);
	if (newState == 0) { 
		if (parent.looping == true) {
			document.getElementById("volpct").innerHTML = "";
			parent.LoopVid();
		} else {
			parent.EndVid();
			window.location = 'slides/slides.asp';
		}
	}
}

function updateytplayerInfo() {
	updateHTML("bytesloaded", getBytesLoaded());
	updateHTML("bytestotal", getBytesTotal());
	updateHTML("videoduration", getDuration());
	updateHTML("videotime", getCurrentTime());
	updateHTML("startbytes", getStartBytes());
	updateHTML("volume", getVolume());
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
	if (ytplayer) {
		ytplayer.loadVideoById(id, parseInt(startSeconds));
	}
}

function cueNewVideo(id, startSeconds) {
	if (ytplayer) {
		ytplayer.cueVideoById(id, startSeconds);
	}
}

function play() {
	if (ytplayer) {
		ytplayer.playVideo();
		document.getElementById("volpct").innerHTML = "";
	}
}

function pause() {
	if (ytplayer) {
		ytplayer.pauseVideo();
		document.getElementById("volpct").innerHTML = "Paused";
	}
}

function stop() {
	if (ytplayer) {
		ytplayer.stopVideo();
		document.getElementById("volpct").innerHTML = "Stopped";
	}
}

function getPlayerState() {
// unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5). 

	if (ytplayer) {
		return ytplayer.getPlayerState();
	}
}

function seekTo(seconds) {
	if (ytplayer) {
		ytplayer.seekTo(seconds, true);
	}
}

function getBytesLoaded() {
	if (ytplayer) {
		return ytplayer.getVideoBytesLoaded();
	}
}

function getBytesTotal() {
	if (ytplayer) {
		return ytplayer.getVideoBytesTotal();
	}
}

function getCurrentTime() {
	if (ytplayer) {
		return ytplayer.getCurrentTime();
	}
}

function getDuration() {
	if (ytplayer) {
		return ytplayer.getDuration();
	}
}

function getStartBytes() {
	if (ytplayer) {
		return ytplayer.getVideoStartBytes();
	}
}

function mute() {
	if (ytplayer) {
		ytplayer.mute();
		document.getElementById("volpct").innerHTML = "Mute";
	}
}

function unMute() {
	if (ytplayer) {
		ytplayer.unMute();
		showVolume();
	}
}
        
function getEmbedCode() {
	alert(ytplayer.getVideoEmbedCode());
}

function getVideoUrl() {
	alert(ytplayer.getVideoUrl());
}
        
function setVolume(newVolume) {
	if (ytplayer) {
		ytplayer.setVolume(newVolume);
		showVolume();
	}
}
function showVolume() {
	if (ytplayer) {
		gv = getVolume();
		gvs = "";
		gv = Math.round(gv/5)
		for (i=1;i<=gv;i++) {gvs = gvs + "|"}
		ugv = 20 - gv;
		gvs = gvs + "<span style='color:#aaa'>"
		for (i=1;i<=ugv;i++) {gvs = gvs + "|"}
		gvs = gvs + "</span>";
		document.getElementById("volpct").innerHTML = "Volume<br>" + gvs;
	}
}

function getVolume() {
	if (ytplayer) {
		return ytplayer.getVolume();
	}
}
function clearVideo() {
	if (ytplayer) {
		ytplayer.clearVideo();
	}
}
        
