$(document).ready(function(){

	var playItem = 0;

	var myPlayList = [
	 {name:"HBR",mp3:"http://www.thefaults.co.uk/player/mp3/hbr.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/hbr.ogg"},
	  {name:"Desert Dreams",mp3:"http://www.thefaults.co.uk/player/mp3/desert-dreams.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/desert-dreams.ogg"},
	{name:"Paperboy",mp3:"http://www.thefaults.co.uk/player/mp3/paperboy.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/paperboy.ogg"},
	  
	  
		{name:"Money",mp3:"http://www.thefaults.co.uk/player/mp3/money.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/money.ogg"},
		{name:"Belvue Blues",mp3:"http://www.thefaults.co.uk/player/mp3/bellevue.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/bellevue.ogg"},
		{name:"Can't Wait",mp3:"http://www.thefaults.co.uk/player/mp3/cantwait.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/cantwait.ogg"},
		{name:"Turns Out Right",mp3:"http://www.thefaults.co.uk/player/mp3/turnsoutright.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/turnsoutright.ogg"},
		{name:"Mr Nice",mp3:"http://www.thefaults.co.uk/player/mp3/mrnice.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/mrnice.ogg"},
		{name:"Other Ways",mp3:"http://www.thefaults.co.uk/player/mp3/otherways.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/otherways.ogg"},
		{name:"Everytime",mp3:"http://www.thefaults.co.uk/player/mp3/everytime.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/everytime.ogg"},
		//{name:"Think You Know",mp3:"http://www.thefaults.co.uk/player/mp3/thinkyouknow.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/thinkyouknow.ogg"},
		{name:"Belvue Blues - Acoustic",mp3:"http://www.thefaults.co.uk/player/mp3/bellevuea.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/bellevuea.ogg"}
		//{name:"Mr Nice - Acoustic",mp3:"http://www.thefaults.co.uk/player/mp3/mrnicea.mp3",ogg:"http://www.thefaults.co.uk/player/ogg/mrnicea.ogg"}
	];
	

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");


	$("#jquery_jplayer").jPlayer({
		ready: function() {
			// displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
		},
		oggSupport: true
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));

		
	})
	.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;
        		$("#jplayer_playlist ul").html("<li>"+ myPlayList[playItem].name +"</li>");
      			  $("#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 );
	}
});


