﻿//..................youtube api...............
var defaultVolume = 0;
var videoTotalDuration = 0;
var videoCurrentTime = 0;
var videoTotalBytes = 0;
var videoCurrentBytes = 0;
var onOrOff = 0;

function updateHTML(elmId, value) {
  document.getElementById(elmId).innerHTML = value;
}

function setytplayerState(newState) {
  updateHTML("playerstate", newState);
}

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
  ytplayer.cueVideoById('XGo_fCcfYpc',0.5);
  setInterval(updateytplayerInfo, 250);
  updateytplayerInfo();
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
 
}

function onytplayerStateChange(newState) {
  setytplayerState(newState);
}

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, startSeconds);
    playOrPause = 2;
  }
  defaultVolume = getVolume();
  setDefaultVolume();
  onOrOff = 1;
}
function setSize(width, height){
  if (ytplayer){
    ytplayer.setSize(width, height)
  }
}

function cueNewVideo(id, startSeconds) {
  if (ytplayer) {
    ytplayer.cueVideoById(id, startSeconds);
  }
}

function play() {
  if (ytplayer) {
    ytplayer.playVideo();
    playOrPause = 2;
  }
}

function pause() {
  if (ytplayer) {
    ytplayer.pauseVideo();
    playOrPause = 1;
  }
}

function stop() {
  if (ytplayer) {
    ytplayer.stopVideo();
    playOrPause = null;
  }
  clearVideo();
}

function getPlayerState() {
  if (ytplayer) {
    return ytplayer.getState();
  }
}

function seekTo(seconds) {
  if (ytplayer) {
    ytplayer.seekTo(seconds, true);
  }
}

function getBytesLoaded() {
  if (ytplayer) {
    videoCurrentBytes = ytplayer.getVideoBytesLoaded();
    videoBytesProgress();
    return ytplayer.getVideoBytesLoaded();
  }
}

function getBytesTotal() {
  if (ytplayer) {
    return ytplayer.getVideoBytesTotal();
  }
}

function getCurrentTime() {
  if (ytplayer) {
    videoCurrentTime = ytplayer.getCurrentTime();
    videoProgress();
    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();
  }
}
//....................youtube api end...................