﻿var ytPollID = null;
var ytPollMsecs = 500;
var seekPoints;
var waitForSec = null;
function onYouTubePlayerReady(playerId) {
          ytplayer = document.getElementById("myytplayer");
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}
function onytplayerStateChange(newState) { 
  updateytplayerInfo();
  ytPoll(ytPollMsecs);
}
function seekTo(seconds) {
   tke_url("Videos", "seekTo()", seconds);
  if (ytplayer) {
    ytplayer.seekTo(seconds, true);
  }
}
function seekToAndPlay(seconds){
    seekTo(seconds);
    play();
}

function ytPoll(secs){
//alert("secs = " + secs)
    
    ytPollMsecs = secs ;
    
    ytPollvar = setTimeout("pollFunc()",ytPollMsecs);
}
function pollFunc(){
    setTimeout("pollFunc()",ytPollMsecs);
    updateytplayerInfo();
    setCurrSeekPoint();
}
function setCurrSeekPoint(index){
     
    if(seekPoints == null)
        return;
    var currIndex;
    
    var currMin = getCurrentTime();
    
    for(var i = 0; i < seekPoints.length; i++){
        if(currMin  >= seekPoints[i])
            currIndex = i;
        
    }
    
    for(var i = 0; i < seekPoints.length; i++){
        document.getElementById("ytSeekAnchor_" + (i+1).toString()).className = (i==currIndex )?"ytSeekLink_current":"ytSeekLink";
        document.getElementById("ytSeekIL_" + (i+1).toString()).className = (i==currIndex )?"videoSeekIL_current":"videoSeekIL";
    }
    
}
function updateytplayerInfo() {
    return;
  updateHTML("bytesloaded", getBytesLoaded());
  updateHTML("bytestotal", getBytesTotal());
  updateHTML("videoduration", getDuration());
  updateHTML("videotime", getCurrentTime());
  updateHTML("startbytes", getStartBytes());
  updateHTML("volume", getVolume());
}
function updateHTML(elmId, value){
    document.getElementById(elmId).innerHTML = value;
}
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 play() {
          if (ytplayer) {
            ytplayer.playVideo();
          }
        }
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();
  }
}
