/*
IMPORTANT ADVISE
The following code is protected by copyright 
 *  (c) 2009 Davide Rolando
except for the functions that don't contain the notice:
	/// Copyright © 2009 Davide Rolando///

Thus you can't use any of the copyright protected code 
without first obtaining written permission. 
Please email  webmaster@subsedit.com to obtain written consent.
*/

		function updateHTML(elmId, value) {
          //document.getElementById(elmId).innerHTML = value;
        }
		
        function setytplayerState(newState) {
          updateHTML("playerstate", newState);
        }
		var intval = ''
        function onYouTubePlayerReady(playerId) {
		  //alert("ready");
          ytplayer = document.getElementById("myytplayer");
          intval = setInterval(updateytplayerInfo, 250);
          updateytplayerInfo();
          //ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          ytplayer.addEventListener("onError", "onPlayerError");
        }

        function onytplayerStateChange(newState) {
          setytplayerState(newState);
        }

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

        function updateytplayerInfo() {
          //updateHTML("bytesloaded", getBytesLoaded());
          //updateHTML("bytestotal", getBytesTotal());
          //updateHTML("videoduration", getDuration());
          //updateHTML("videotime", getCurrentTime());
          //updateHTML("startbytes", getStartBytes());
        }

        // functions for the api calls
        function loadNewVideo(id, startSeconds) {	//doesn't work with standard player but with chromeless
          if (ytplayer) {
            ytplayer.loadVideoById(id, 0);
            return true;
          }
          return false;
        }

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

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

        function stop() {
          if (ytplayer) {
            ytplayer.stopVideo();
            return true;
          }
          return false;
        }
	
	function activeVid(){
		var vid = 'none';
		if (ytplayer) {
			vid = ytplayer.getVideoUrl();
		}
		return vid;
	}
	
        function getPlayerState() {
          if (ytplayer) {
            return ytplayer.getPlayerState();
          }
        }

        function seekTo(seconds) {
          if (ytplayer) {
            ytplayer.seekTo(seconds, true);
          }
        }
        
		function backOf(seconds) {
          if (ytplayer) {
            ytplayer.seekTo(getCurrentTime() - seconds, true);
          }
        }
		
        function getBytesLoaded() {
          if (ytplayer) {
            return ytplayer.getVideoBytesLoaded();
          }
        }

        function getBytesTotal() {
          if (ytplayer) {
            return ytplayer.getVideoBytesTotal();
          }
        }

        function getCurrentTime() {
          if (ytplayer) {
            return ytplayer.getCurrentTime();
          }
        }
