var barwidth = 330;

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");
}

function onytplayerStateChange(newState) {
setytplayerState(newState);
}

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function getX( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function formatsecs(intime)
{
  newstring = '';
  mins = Math.floor(intime / 60)
  if (mins < 10)
  {
   mins = '0' + mins;
  }
  secs = intime%60
  if (secs < 10)
  {
   secs = '0' + secs;
  }
  newstring = mins+':'+secs;
  return newstring;
}

function barseek()
{
var e = window.event
leftx = getX(document.getElementById('timebar'))
moveto = getDuration() * ((e.clientX - leftx) / barwidth)
//updateHTML("debugtag", leftx+' '+e.clientX+' '+moveto);
seekTo(moveto);
}

function updateytplayerInfo() {
//updateHTML("bytesloaded", getBytesLoaded());
//updateHTML("bytestotal", getBytesTotal());
//updateHTML("videoduration", getDuration());
dur = getDuration()
cur = getCurrentTime()
totalbytes = getBytesTotal();
bytesdone = getBytesLoaded();
loaddone = Math.floor(barwidth*(bytesdone/totalbytes));
loadleft = barwidth - loaddone;
playdone = Math.floor(barwidth*(cur / dur));
playleft = barwidth - playdone;
barblue = playdone;
if (loaddone > playdone)
{
bargray = loaddone - playdone
}
else
{
bargray = 1;
}
barwhite = barwidth - bargray - barblue;
newhtml = '<img id="playedimg" src="art/orangebar.jpg" height="8px" width='+barblue+'px"><img src="art/graybar.jpg" height="8px" width='+bargray+'px"><img src="art/white.gif" height="8px" width='+barwhite+'px">'
updateHTML("timebar", newhtml);
playedstring = formatsecs(Math.round(cur));
updateHTML("playedtime", playedstring);
totalstring = formatsecs(Math.round(dur));
updateHTML("totaltime", totalstring);
// updateHTML("startbytes",getBytesLoaded());
// 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();
updateHTML("playbutton",'<input type="image" src="art/pause.png" onclick="pause();">');
}
}

function pause() {
if (ytplayer) {
ytplayer.pauseVideo();
updateHTML("playbutton",'<input type="image" src="art/play.png" onclick="play();">');
}
}

function stop() {
if (ytplayer) {
ytplayer.stopVideo();
}
}

function getPlayerState() {
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();
}
}

function unMute() {
if (ytplayer) {
ytplayer.unMute();
}
}

function getEmbedCode() {
alert(ytplayer.getVideoEmbedCode());
}

function getVideoUrl() {
alert(ytplayer.getVideoUrl());
}

function setVolume(newVolume) {
if (ytplayer) {
ytplayer.setVolume(newVolume);
}
}

function getVolume() {
if (ytplayer) {
return ytplayer.getVolume();
}
}

function clearVideo() {
if (ytplayer) {
ytplayer.clearVideo();
}
}