// Academy Books TV Embed Code
// James Carppe - 2 July 2009

        function updateHTML(elmId, value) {
          document.getElementById(elmId).innerHTML = value;
        }
        
        function PadDigits(n, totalDigits) { 
          n = n.toString(); 
          var pd = ''; 
          if (totalDigits > n.length) { 
            for (i=0; i < (totalDigits-n.length); i++) { 
              pd += '0'; 
            } 
          } 
          return pd + n.toString(); 
        } 

        function setytplayerState(newState) {
          updateHTML("playerstate", newState);
        }

        function onYouTubePlayerReady(playerId) {
          ytplayer = document.getElementById("myytplayer");
          setInterval(updateytplayerInfo, 250);
          updateytplayerInfo();
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          cueNewVideo(videoid, 0);
        }
        
        function onytplayerStateChange(newState) {
          if (newState == 1) {
            document.getElementById("btn_playpause").src = "/images/abctv/btn_pause.gif";
          }
          else {
            document.getElementById("btn_playpause").src = "/images/abctv/btn_play.gif";
          }
        }
        
        function secs_to_minsecs(seconds) {
          theminutes = Math.floor(seconds / 60);
          if (theminutes < 0) {
            theminutes = 0;
          }
          theseconds = PadDigits(Math.floor(seconds % 60), 2);
          if (theseconds < 0) {
            theseconds = "00";
          }
          duration = theminutes + ":" + theseconds;
          return duration;
        }

        function updateytplayerInfo() {
          updateHTML("videoduration", secs_to_minsecs(getDuration()));
          updateHTML("videotime", secs_to_minsecs(getCurrentTime()));
        }


        // 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 playpause() {
          if (ytplayer) {
            if (ytplayer.getPlayerState() == 1) {
              pause();
            }
            else if (ytplayer.getPlayerState() > 1) {
              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) {
            document.getElementById("btn_togglemute").src = "/images/abctv/btn_nosound.gif";
            ytplayer.mute();
          }
        }

        function unMute() {
          if (ytplayer) {
            document.getElementById("btn_togglemute").src = "/images/abctv/btn_sound.gif";
            ytplayer.unMute();
          }
        }
        
        function toggleMute() {
          if (ytplayer) {
            if (ytplayer.isMuted() == true) {
              unMute();
            }
            else {
              mute();
            }
          }
        }
        
        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();
          }
        }
