/*
 * Chromeless player has no controls.
 */
  
var currentTimeDisplay ; 
var currentSongId ; 
var isPlaying = false ; 
 

function prepareSong(songId) 
{
	if (currentSongId == songId && isPlaying)
	{
		// pause
		pauseVideo() ; 
		var song_control_button = $('#img_'+songId) ; 
		var img = '/media/ui/youtube_remote/play.png' ; 
		song_control_button.attr('src', img) ; 
		isPlaying = false ; 
	}
	else if (currentSongId == songId) 
	{
		// resume 
		playVideo() ; 
		var song_control_button = $('#img_'+songId) ; 
		var img = '/media/ui/youtube_remote/pause.png' ; 
		song_control_button.attr('src', img) ;
		isPlaying = true ;  
	}
	else
	{
		// use new song in player
		currentSongId = songId ; 
		if (!ytplayer) return ; 
		$('.youtube_remote_display').each( function(index){
			$(this).html("") ; 
		});
		
		var img = '/media/ui/youtube_remote/play.png' ; 
		$('.song_control_button').each( function(index){
			$(this).attr('src', img) ; 
		}) ; 	
		
		var song_control_button = $('#img_'+songId) ; 
		currentTimeDisplay = 'display_'+songId ; 
		var img = '/media/ui/youtube_remote/pause.png' ; 
		song_control_button.attr('src', img) ; 
		ytplayer.loadVideoById(songId) ;
		ytplayer.playVideo() ; 
		isPlaying = true ; 
	}
}


// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
	if (document.getElementById(elmId)) document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
//  alert("Song load error.");
  
  $('.youtube_remote_display').each( function(index){
  	$(this).html("") ; 
  });
  
  var img = '/media/ui/youtube_remote/play.png' ; 
  $('.song_control_button').each( function(index){
  	$(this).attr('src', img) ; 
  }) ; 
  
  $('#img_'+currentSongId).remove() ; 
  $('#display_'+currentSongId).remove() ; 
  
  
  $.getJSON("/broken_youtube_link/", { yt_id: currentSongId },
  	function(data, textStatus) 
	{
	/*
		for (var item in data)
		{	
			var entry = data[item] ;
			alert("entry") ; 
		}
	*/
	}
	);
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
  updateHTML("playerState", newState);
}

// Display information about the current state of the player
function updatePlayerInfo() {
  // Also check that at least one function exists since when IE unloads the
  // page, it will destroy the SWF before clearing the interval.
  if(ytplayer && ytplayer.getDuration) 
  {
   // updateHTML("videoDuration", ytplayer.getDuration());
   	var seconds = parseInt(ytplayer.getCurrentTime().toString()) ;
   	var minutes = Math.floor(seconds/60) ; 
   	seconds = seconds % 60 ;              
   	var styledTime = "" + zeroPad(minutes, 2) + ":" + zeroPad(seconds, 2) ; 
    updateHTML(currentTimeDisplay, styledTime) ;
    
   // updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
   // updateHTML("startBytes", ytplayer.getVideoStartBytes());
   // updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
   // updateHTML("volume", ytplayer.getVolume()); 
  }
}

function zeroPad(num,count)
{
	var numZeropad = num + '';
	while(numZeropad.length < count) {
		numZeropad = "0" + numZeropad;
	}
	return numZeropad;
}

// Allow the user to set the volume from 0-100
function setVideoVolume() {
  var volume = parseInt(document.getElementById("volumeSetting").value);
  if(isNaN(volume) || volume < 0 || volume > 100) {
    alert("Please enter a valid volume between 0 and 100.");
  }
  else if(ytplayer){
    ytplayer.setVolume(volume);
  }
}

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

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

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

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

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("ytPlayer");
  // This causes the updatePlayerInfo function to be called every 250ms to
  // get fresh data from the player
  setInterval(updatePlayerInfo, 250);
  updatePlayerInfo();
  ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
  ytplayer.addEventListener("onError", "onPlayerError");
  //Load an initial video into the player
  //ytplayer.cueVideoById("ylLzyHk54Z0");
  
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
  // Lets Flash from another domain call JavaScript
  var params = { allowScriptAccess: "always" };
  // The element id of the Flash embed
  var atts = { id: "ytPlayer" };
  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                     "&enablejsapi=1&playerapiid=player1",
                     "videoDiv", "0", "0", "0", null, null, params, atts);
}

function _run() {
	loadPlayer();
}

google.setOnLoadCallback(_run);

