/**
 * Youtube player functions
 *
 * @author		  Nicolas Zanghi
 * @copyright     Copyright 2010, Nicolas Zanghi
 */

	/**
	 * Display information about the current state of the player, lunch on interval in onYoutubePlayerReady()
	 */
	function updatePlayerInfo() {
	  if(ytplayer && ytplayer.getDuration) {
		var duration = Math.round(ytplayer.getDuration());
		if (duration > 0) {
			$("video_duration").innerHTML = secondsToHms(duration);
			$("video_currenttime").innerHTML = secondsToHms(Math.round(ytplayer.getCurrentTime()));
		}
		if (ytplayer.getCurrentTime() > 0)
			slider_control.setValue(ytplayer.getCurrentTime()/ytplayer.getDuration()*100);
	  }
	}

	/**
	 * called when player change state
	 */
	function onytplayerStateChange(newState) {
		if (newState == 3) { // loading
			$('spinner').style.display = 'inline';
			// in autoplay mode if is not loaded after 4 seconds skip
			if (autoplay)
				new PeriodicalExecuter(function(pe) {
					if (ytplayer.getPlayerState() == '4') {
						getRandomSong(); //play next 
					}
					pe.stop();
				}, 3);
		} else if (newState == 1) { // play
			$('spinner').style.display = 'none';
			Effect.Appear('details');
			$('song_video').setStyle({visibility: 'visible'});
			Effect.Appear('song_name');
			$('bt_play').style.display = 'none';
			$('bt_pause').style.display = 'inline-block';
			pe.stop();
		} else if (newState == 2) { // pause
			$('spinner').style.display = 'none';
			pe.stop();
		} else if (newState == '0' && autoplay) { // stop, play next
			getRandomSong(); //play next
			pe.stop();
		} else if (newState == '0') { // stop
			$('bt_play').style.display = 'inline-block';
			$('bt_pause').style.display = 'none';
			pe.stop();
		} else if (newState == '-1') { // ready
			Event.observe($('bt_play'), 'click', function(event) {btPlay();}, false);
			Event.observe($('bt_play'), 'mouseover', function(event) {$('bt_play').setAttribute('src', 'img/bt_play_hover.png');}, false);
			Event.observe($('bt_play'), 'mouseout', function(event) {$('bt_play').setAttribute('src', 'img/bt_play.png');}, false);
			$('bt_play').setAttribute('src', 'img/bt_play.png');

			getSongAtBegin(); // execute function in layout/map.ctp to load song if is asked by url

		}
	}

	/*function onPlayerError(errorCode) {
		$('error').innerHTML += errorCode+' ';
	} */

	/**
	 * setInterval updatePlayerInfo, set function onytplayerStateChange
	 */
	function onYouTubePlayerReady(playerId) {
		  ytplayer = document.getElementById("myytplayer");
		  setInterval(updatePlayerInfo, 250);
		  updatePlayerInfo();
		  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
		  //ytplayer.addEventListener("onError", "onPlayerError");
		  ytplayer.setPlaybackQuality('small');
	}

	/**
	 * Used to display video duration
	 */
	function secondsToHms(d) {
		d = Number(d);
		var h = Math.floor(d / 3600);
		var m = Math.floor(d % 3600 / 60);
		var s = Math.floor(d % 3600 % 60);
		return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);
	}




