﻿var vidPreview = "";
var nextVideo = "";
var numPlayed = 1;
var playerStateCtrlId = "";
var quickswitch = 0;

function changeVideo(vMain, vPreview, bPreview) {
    var vNow = "";
    stop();
    if (bPreview) {
        numPlayed = 1;
        nextVideo = vMain;
        vNow = vPreview;
        }
    else {
        vNow = vMain;
    }
    loadNewVideo(vNow, 0);
}

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

        function setytplayerState(newState) {
          if (playerStateCtrlId.length > 0)
              updateHTML(playerStateCtrlId, newState);
          if (newState == "1") {
              if (quickswitch == "1") {
                  quickswitch = 0;
                  stop();
                  clearVideo();
                  loadNewVideo(vidPreview, 0);
              }
          }
                  
          if (newState == "0") {
              if (numPlayed < 2) {
                  if (nextVideo.length > 0)
                  {
                     loadNewVideo(nextVideo, 0);
                  }
              }
              numPlayed = numPlayed + 1;
          }
        }

        function onYouTubePlayerReady(playerId) {
          ytplayer = document.getElementById("myytplayer");
          // setInterval(updateytplayerInfo, 250);
          // updateytplayerInfo();
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          ytplayer.addEventListener("onError", "onPlayerError");
        }

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

        function onytplayerStateChange(newState) {
            // onStateChange 
            // This event is fired whenever the player's state changes. 
            // Possible values are unstarted (-1), ended (0), playing (1), 
            // paused (2), buffering (3), video cued (5). 
            // When the SWF is first loaded it will broadcast an unstarted (-1) event. 
            // When the video is cued and ready to play it will broadcast a video cued event (5). 
          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, parseInt(startSeconds));
          }
        }

        function cueNewVideo(id, startSeconds) {
          if (ytplayer) {
            ytplayer.cueVideoById(id, startSeconds);
          }
        }

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

        function pause() {
          if (ytplayer) {
            ytplayer.pauseVideo();
          }
        }

        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();
          }
        }



