var xmlhttp;
function ajax_yt(){
xmlhttp=GetXmlHttpObject();
ytroot = resolvdocument();
rand = "?sid="+Math.random();
var url= ytroot+'php_functions/random_youtube_id.php' + rand;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET", url, true); 
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(null); 
}

function stateChanged(){
if (xmlhttp.readyState==4){
reponsephp = xmlhttp.responseText;
ytarray = reponsephp.split('|');
ytInitiate(ytarray[0], ytarray[1], ytarray[2], 'secondplay');
}
}

function GetXmlHttpObject(){
if (window.XMLHttpRequest){
  return new XMLHttpRequest();
  }
if (window.ActiveXObject){
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function ytInitiate(youtube_id, ytarticleid, ytarticletitle, firstplay){
	
ytcode = youtube_id;
ytarticle_id = ytarticleid;
ytarticle_title = ytarticletitle;
	
function loadPlayer() {
  var params = { allowScriptAccess: "always", wmode: "transparent" };
  var atts = { id: "ytPlayer" };
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
					 "&enablejsapi=1&playerapiid=player1&wmode=transparent", 
					 "videoDiv", "250", "160", "8", null, null, params, atts);
}

function loadNewVideo(ytcode) {
  if (ytplayer) {
	ytplayer.loadVideoById(ytcode, parseInt(0));
  }
}


if(firstplay == 'firstplay'){
loadPlayer();
}else{
loadNewVideo(ytcode);
}
}

 document.getElementById('youtubelink').innerHTML = '<a href="http://divingalmanac.com/article.php?article_id='+ytarticle_id+'">'+ytarticle_title+'</a>';

// This function is called when the player changes state
function onPlayerStateChange(newState) {
  if(newState == 3){
	   document.getElementById('overdiv').innerHTML = "";
	   document.getElementById('youtubelink').innerHTML = "Loading video...";
  }
    if(newState == 1){
 document.getElementById('overdiv').innerHTML = '<a href="../article.php?article_id='+ytarticle_id+'"><img src="../images/spacer.gif" alt="Random Youtube Video" width="250" height="160" border="0" /></a>';
 
 document.getElementById('youtubelink').innerHTML = '<a href="http://divingalmanac.com/article.php?article_id='+ytarticle_id+'">'+ytarticle_title+'</a>';
  }
  if(newState == 0){
	 ajax_yt();
  }  
}

function updatePlayerInfo(){
  if(ytplayer && ytplayer.getDuration) {
	ytduration = ytplayer.getDuration();
	ytcurtime = ytplayer.getCurrentTime();
	pourcentage = (ytcurtime * 100) / ytduration;
	pourcentage = pourcentage.toFixed(0)
	document.getElementById("playerState").innerHTML = '<hr width="'+pourcentage+'%" align="left" style="border : none; height : 2px; background-color : #000; margin-top:1px; margin-bottom:2px;" />';
  }
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("ytPlayer");
  ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
  setInterval(updatePlayerInfo, 250);
  updatePlayerInfo();
  ytplayer.cueVideoById(ytcode);
  ytplayer.mute();
  ytplayer.playVideo();
}

