$(document).ready(function(){
	var playItem = 0;
	var myPlayList = [{name:"'Hi-Fly' - Randy Weston",mp3:"/HiFly.mp3"}];

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");

	$("#jquery_jplayer").jPlayer({
		swfPath: "/js/",
		ready: function() {
			playListInit(true); // Parameter is a boolean for autoplay.
		},
		nativeSupport: 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();
		$(this).blur();
		return false;
	});

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


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

	function playListConfig( index ) {
		playItem = index;
		$("#trackname").html(myPlayList[playItem].name); 
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
	}

	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 );
	}

	$("a.audiosample").click( changeTrack );
	$("object").click( killAudio );
	$("embed").click( killAudio );
	
	function changeTrack(e) {
		$("#trackname").text($(this).text());
		$("#jquery_jplayer").jPlayer("setFile", $(this).attr("href")).jPlayer("play");
		$(this).blur();
		return false;
	}
	function killAudio() {
		$("#jquery_jplayer").jPlayer( "pause" );
	}
});

