var videoIds = [];
var startTimes = [];
var playIndex = 0;

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
  InitializePlaylist(["6YBJATj-iAU", "sNsG5A1uaqQ", "uYJS0O-9tIc", "D9GLnPMeU_w", "XunlyUsvVqQ", "dh6pFgQhQZY","6PJ6B5ySdmA", "707VxB-ek4Q","9R1gKBkr8tU", "4ye3qPGiP8U", "-SfYO7UzlEo", "18lq9ZmshZ0", "7iGXP_UBog4", "s_Q5_zWikSI", "pyqUZvFgOQs", "kH-krlgo2e8", "0YHCiC7IIg8", "ptgSD2ilzEo", "i8axZTPOQTE", "87d-kvnVvoE"], [0, 0, 0, 0, 0]);
}

function onytplayerStateChange(newState) {
  if(newState = 0) {
    PlayNextPlaylistEntry();
  }
}

function InitializePlaylist(vIds, sTimes) {
  videoIds = vIds;
  startTimes = sTimes;
  LoadNextPlaylistEntry();
}

function LoadNextPlaylistEntry() {
  if(playIndex < videoIds.length) {
    loadNewVideo(videoIds[playIndex], startTimes[playIndex]);
    playIndex++;
  }
}

function LoadPrevPlaylistEntry() {
  if(playIndex > 1) {
    playIndex--;
    loadNewVideo(videoIds[playIndex-1], startTimes[playIndex-1]);
  }
}

function loadNewVideo(id, startSeconds) {
  if (ytplayer) {
    ytplayer.loadVideoById(id, startSeconds);
    setTimeout('displayDuration()', 1000);
  }
}

function stop() {
  if (ytplayer) {
    ytplayer.seekTo(0, true);
    ytplayer.pauseVideo();
  }
}

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

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

function previous() {
  if(ytplayer) {
    LoadPrevPlaylistEntry();
  }
}

function mute() {
  if (ytplayer) {
     ytplayer.mute();
  }
 }

 function unMute() {
   if (ytplayer) {
      ytplayer.unMute();
  }
 }

 function setVolume(newVolume) {
          if (ytplayer) {
            ytplayer.setVolume(newVolume);
          }
        }

        function getVolume() {
          if (ytplayer) {
            return ytplayer.getVolume();
          }
        }
		
function displayDuration() {
  document.getElementById("videoduration").innerHTML = getDuration()
}

function getDuration() {
  if (ytplayer) {
    var duration = ytplayer.getDuration();
    var hours = Math.floor(duration / 60);
    var minutes = Math.round(duration % 60);
    if(minutes < 10) {
      minutes = "0" + minutes;
    }
    return hours + ":" + minutes;
  }
}

function seekTo(seconds) {
  if (ytplayer) {
    ytplayer.seekTo(seconds, true);
  }
}