$(document).ready(function(){
	$('#player').css('display', 'block');

	var playItem = 0;

	var myPlayList = [
                {name:"Wake Us Up",mp3:"assets/music/wake_us_up.mp3"},
		{name:"Around the World",mp3:"assets/music/around_the_world.mp3"},
		{name:"Too Long Without You",mp3:"assets/music/too_long_without_you.mp3"},
		{name:"Sunlight",mp3:"assets/music/sunlight.mp3"},
		{name:"Cry For You",mp3:"assets/music/cry_for_you.mp3"},
		{name:"Limited Time",mp3:"assets/music/limited_time.mp3"}
	];

	$("#jquery_jplayer").jPlayer({
		ready: function() {
			playListInit(true); // Parameter is a boolean for autoplay.
		},
		swfPath: "assets/js"
	})
	.jPlayerId("play", "player_play")
	.jPlayerId("pause", "player_pause")
	.jPlayerId("stop", "player_stop")
	.jPlayerId("loadBar", "player_progress_load_bar")
	.jPlayerId("playBar", "player_progress_play_bar")
	.jPlayerId("volumeMin", "player_volume_min")
	.jPlayerId("volumeMax", "player_volume_max")
	.jPlayerId("volumeBar", "player_volume_bar")
	.jPlayerId("volumeBarValue", "player_volume_bar_value")
	.onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		var myPlayedTime = new Date(playedTime);
		var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
		var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
		$("#play_time").text(ptMin+":"+ptSec);

		var myTotalTime = new Date(totalTime);
		var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
		var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
		$("#total_time").text(ttMin+":"+ttSec);
	})
		
	$("#ctrl_prev").click( function() {
		playListPrev();
		return false;
	});

	$("#ctrl_next").click( function() {
		playListNext();
		return false;
	});
	
	function currentPlaying() {
		$("#currentPlaying #text p").text(myPlayList[playItem].name);
		Cufon.replace('div#currentPlaying #text p');

	}


	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
			currentPlaying();
			$("#jquery_jplayer").pause();
			$("#jquery_jplayer").stop();
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		playItem = index;
		$("#jquery_jplayer").setFile(myPlayList[playItem].mp3);
		
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer").play();
	}

	function playListNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		playListChange( index );
		currentPlaying();
	}

	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		playListChange( index );
		currentPlaying();
	}
});


