 function updateHTML(elmId, value) {
          document.getElementById(elmId).innerHTML = value;
        }

function setytplayerState(newState) {
          updateHTML("playerstate", newState);
        }

function onYouTubePlayerReady(playerId) {
//debug("player loaded");
          ytplayer = document.getElementById("myytplayer");
          setInterval(updateytplayerInfo, 250);
          //updateytplayerInfo();
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          ytplayer.addEventListener("onError", "onPlayerError");
		  ytplayer.unMute();
		 if (onWebsite)
			{
			zapOnSite(0);
			}
		else
			{
			cueNewVideo('Ce9hzex8gvA', 0); // alice tv intro
			}
        }

  function onytplayerStateChange(newState) {
          //setytplayerState(newState);
		  switch(newState)
		  {
			case -1: newState = 'UNSTARTED'; break;
			case 0: newState = 'ENDED'; break;
			case 1: newState = 'PLAYING'; break;
			case 2: newState = 'PAUSED'; break;
			case 3: newState = 'BUFFERING'; break;
			case 5: newState = 'VIDEOCUED'; break;
		  }
		
		  debug("LOCALSTATE: "+newState);
		  if (newState == 'VIDEOCUED')
			{
			if (fRemotePlayerLoaded)
				{
				//play();
				//status("start playing");
				}
			if (fMyPlayerLoaded == false)
				{
				fMyPlayerLoaded = true;
				SafeSendData("PLAYERREADY");
				}
			// play();
			}
		 else if (newState == 'ENDED')
			{
			clickPause(0);
			stop();
			//cueNewVideo('Ce9hzex8gvA', 0); // alice tv intro - buggy here, as MSN considers switchingm to other video also as ENDED...
			}
		 else if (newState == 'PLAYING')
			{
			clickPlay(0);
			}
        }

        function onPlayerError(errorCode) 
			{
			//alert("An error occurred: "+ errorCode);
			if (errorCode == 100)
				{
				status("Video no longer available");
				}
			}

        function updateytplayerInfo() {
          //updateHTML("bytesloaded", getBytesLoaded());
          //updateHTML("bytestotal", getBytesTotal());
          updateHTML("videoduration", secToTime(getDuration()));
          updateHTML("videotime", secToTime(getCurrentTime()));
          //updateHTML("startbytes", getStartBytes());

        }

		function sendAction(action)
		{
		SafeSendData(action);
		}

        // functions for the api calls
        function play() {
          if (ytplayer) {
			  
            ytplayer.playVideo();
          }
        }

        function pause() {
          if (ytplayer) {
            ytplayer.pauseVideo();
          }
        }

        function stop() {
          if (ytplayer) {
            ytplayer.stopVideo();
          }
        }

        function getPlayerState() {
          if (ytplayer) {
            return ytplayer.getState();
          }
        }

        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();
          }
        }

function loadNewVideo(id, startSeconds) {
          if (ytplayer) {
            ytplayer.loadVideoById(id, startSeconds);
          }
        }




function startVideo()
	{
		//alert('starting');
	// allowScriptAccess must be set to allow the Javascript from one domain to access the swf on the youtube domain
      var params = Array();
	  params['allowScriptAccess'] = "always";
	  params['wmode'] = "opaque";
	  params['menu'] = "false";
	  params['bgcolor'] = "#000000";

     // this sets the id of the object or embed tag to 'myytplayer'. You then use this id to access the swf and make calls to the player's API
     // var atts = { id: "myytplayer" };
	  var atts = Array();
	  atts['id'] = "myytplayer";
	  swfobject.embedSWF("http://www.youtube.com/apiplayer?key=AI39si7To9dKEhBFUO7cmDpvyZOnPErn7cfjNd0zzFCXP9k-Eh8RX3Av1eMw-UFgi5Yhd6BP6o48Xil2zJXwMwsfA6cA4kNXvA&enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "400", "300", "8", '', '', params, atts);	
	  //document.getElementById('ytapiplayer').style.display = 'none';
	}


function secToTime(sec)
{
if (sec < 0)
	{
	sec = 0;
	}
var minutes = Math.floor(sec/60);
var seconds = Math.round(sec - minutes*60);
minutes = minutes.toString();
seconds = seconds.toString();
if (minutes.length == 1)
	minutes = "0"+minutes;
if (seconds.length == 1)
	seconds = "0"+seconds;
time = minutes+":"+seconds;
return time;
}