
/*
 * Chromeless player has no controls.
 */
  
// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
  alert("Erreur ("+errorCode+") : Vérifier le code de la vidéo !");
}

// 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) {
	var duration 	 = ytplayer.getDuration();
	var current_time = ytplayer.getCurrentTime();
	var cursor = $$('.you-tube .bar .time').first();
	var width = $$('.you-tube .bar').first().getWidth() - cursor.getWidth();
	current_time = width * current_time / duration;
	if( current_time == width ) {
		current_time =  0;
	} else
		current_time;
	cursor.setStyle({ left: current_time + 'px', position: 'absolute' });
    //updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
    //updateHTML("startBytes", ytplayer.getVideoStartBytes());
    //updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
    //updateHTML("volume", ytplayer.getVolume());
  }
}

// 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();
  }
}

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

function seekTo(seconds) {
  	if (ytplayer) {
		var duration = ytplayer.getDuration();
    	ytplayer.seekTo(seconds*duration, true);
	}
}

 var ytplayer = '';

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("yt_loaded_player");
  if( !ytplayer ) return false;
  setInterval(updatePlayerInfo, 250);
  updatePlayerInfo();
  ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
  ytplayer.addEventListener("onError", "onPlayerError");
  //Load an initial video into the player
  ytplayer.cueVideoById($F('yt_video_id'));
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
  // Lets Flash from another domain call JavaScript
  var params = { allowScriptAccess: "always", allowFullScreen: 'true' };
  // The element id of the Flash embed
  var atts = { id: "yt_loaded_player" };
  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                     "&enablejsapi=1&playerapiid=player1&fs=1",
                     "yt_player", $F('yt_width'), $F('yt_height'), "8", null, null, params, atts);
}
function _run() {
	if( $$('.you-tube').size() <= 0 || $F('yt_video_id').empty() || $F('yt_width').empty() || $F('yt_height').empty())
		return false;
		
  	loadPlayer();
	
	new Control.Slider('player_time', 'player_timeline', {
		onChange: function(value) { 
			if( value > 0 ) {
				$$('.you-tube .bar .pause').invoke('show');
				$$('.you-tube .bar .play').invoke('hide');
				$('yt_loaded_player').setStyle({ visibility: 'visible' }); 
			}
			seekTo(value, true);
      	}
	});
	
	$$('.you-tube .share .close').each(function(share){
		share.observe('click', function(){
			this.up('.share').hide();
		}.bindAsEventListener(share));
		share.onclick = function(){ return false; }
	});
	
	$$('.you-tube .play').each(function(play){
		play.observe('click', function(){
			$$('.you-tube .share').invoke('hide');
			$$('.you-tube .bar .pause', '.you-tube .bar .play').invoke('toggle');
			 $('yt_loaded_player').setStyle({ visibility: 'visible' }); 
			playVideo();
		});
		play.onclick = function(){ return false; }
	});
	
	$$('.you-tube .pause').each(function(pause){
		pause.observe('click', function(){
			
			$$('.you-tube .bar .pause', '.you-tube .bar .play').invoke('toggle');
			pauseVideo();		
		});
		pause.onclick = function(){ return false; }
	});	
	
	$$('.you-tube .bar .letter').each(function(letter){
		letter.observe('click', function(){
			pauseVideo();
			var share = $$('.you-tube .share');
			share.invoke('show');
			share.invoke('setOpacity', 0.9);
			$$('.you-tube .bar .pause').invoke('hide');
			$$('.you-tube .bar .play').invoke('show');
		});
		letter.onclick = function(){ return false; }
	});
	
}

google.setOnLoadCallback(_run);
