
$(document).ready(function(){

	var usePlayList = false;
	var playItem = 0;

	var myPlayList = [
		{name:"Liszt - Consolation no.3",filename:"../mp3/Consolation.mp3",album:"Piano 1"},
		{name:"Debussy - Rêverie",filename:"../mp3/09-ClaudeDebrussy_LaPlusQueLente.mp3",album:"Reve d'Amour"},
		{name:"Bach Silote - Prelúdio",filename:"../mp3/01_VillaLobos_Preludio.mp3",album:"Piano"}
	];


	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListEnable(true);
		}
	});

	$("#jquery_jplayer").jPlayerId("play", "player_play");
	$("#jquery_jplayer").jPlayerId("pause", "player_pause");
	$("#jquery_jplayer").jPlayerId("stop", "player_stop");
	$("#jquery_jplayer").jPlayerId("loadBar", "player_progress_load_bar");
	$("#jquery_jplayer").jPlayerId("playBar", "player_progress_play_bar");
	$("#jquery_jplayer").jPlayerId("volumeMin", "player_volume_min");
	$("#jquery_jplayer").jPlayerId("volumeMax", "player_volume_max");
	$("#jquery_jplayer").jPlayerId("volumeBar", "player_volume_bar");
	$("#jquery_jplayer").jPlayerId("volumeBarValue", "player_volume_bar_value");

	$("#jquery_jplayer").onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		var myPlayedTime = new Date(playedTime);
		var ptMin = (myPlayedTime.getMinutes() < 10) ? "0" + myPlayedTime.getMinutes() : myPlayedTime.getMinutes();
		var ptSec = (myPlayedTime.getSeconds() < 10) ? "0" + myPlayedTime.getSeconds() : myPlayedTime.getSeconds();
		$("#play_time").text(ptMin+":"+ptSec);

		var myTotalTime = new Date(totalTime);
		var ttMin = (myTotalTime.getMinutes() < 10) ? "0" + myTotalTime.getMinutes() : myTotalTime.getMinutes();
		var ttSec = (myTotalTime.getSeconds() < 10) ? "0" + myTotalTime.getSeconds() : myTotalTime.getSeconds();
		$("#total_time").text(ttMin+":"+ttSec);
	});

	$("#jquery_jplayer").onSoundComplete(endOfSong);

	$("#ctrl_prev").click( function() {
		playListPrev();
		return false;
	});

	$("#ctrl_next").click( function() {
		playListNext();
		return false;
	});

	function endOfSong() {
		if(usePlayList) {
			playListNext();
		}
	}

	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			$("#albuns ul").append("<li id='playlist_item_"+i+"'><a href='#'>"+ myPlayList[i].album +"</a></li>");
			$("#playlist_item_"+i).data( "index", i ).hover(
				function() {
					if (playItem != $(this).data("index")) {
						//$(this).addClass("selected");
					}
				},
				function() {
					//$(this).removeClass("selected");
				}
			).click( function() {
				var index = $(this).data("index");
				
				for (i=0; i < myPlayList.length; i++) $("#playlist_item_"+i).removeClass("selected");
				
				$(this).addClass("selected");
				if (playItem != index) {
					playListChange( index );
				}
				return false;
			});
		}
	}

	function playListEnable(e) {
		usePlayList = e;
		if(usePlayList) {
			playListChange( playItem );
			$("#jquery_jplayer").stop();
			$("#song_title").html("<em>"+myPlayList[playItem].name+"</em>");
		} else {
			$("#jquery_jplayer").stop();
		}
	}

	function playListChange( index ) {
		$("#playlist_item_"+playItem).removeClass("playlist_current");
		$("#playlist_item_"+index).addClass("playlist_current");
		playItem = index;
		$("#song_title").html(myPlayList[playItem].name);
		$("#jquery_jplayer").setFile(myPlayList[playItem].filename).play();
	}

	function playListNext() {
		if(usePlayList) {
			var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
			playListChange( index );
		}
	}

	function playListPrev() {
		if(usePlayList) {
			var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
			playListChange( index );
		}
	}
});
