$(document).ready(function(){

	var playItem = 0;

	var myPlayList = [
			{name:"Great Expectations",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/01_Great_Expectations.mp3"},
		{name:"Real Marginal",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/02_Real_Marginal.mp3"},
		{name:"There's Been No Plan (No Fashionable Pants)",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/03_There's_Been_No_Plan.mp3"},
		{name:"Conquistador",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/04_Conquistador.mp3"},
		{name:"TV Defendant",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/05_TV_Defendant.mp3"},
		{name:"Place To Be",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/06_Place_To_Be.mp3"},
		{name:"Out-Of-Towners",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/07_Out-Of-Towners.mp3"},
		{name:"The Foundling",mp3:"http://paydaymillionaire.com/music/cheque-to-cheque/08_The_Foundling.mp3"}
	];

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");
	var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
			demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
		},
		oggSupport: false
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));

		demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

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

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

	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			$("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
			});
		}
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
		playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");
	}

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

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