$(document).ready(function(){

	var playItem = 0;

	var myPlayList = [
		{name:"Maximum Output – Catch The Reigns",mp3:"http://www.stereomag.net/dls/01. Maximum Output - Catch The Reigns.mp3",ogg:"http://www.stereomag.net/dls/01. Maximum Output - Catch The Reigns.ogg"},
		{name:"im:puls – Ich weiß wie, aber warum…?",mp3:"http://www.stereomag.net/dls/02. im.puls - Ich weiß wie, aber warum....mp3",ogg:"http://www.stereomag.net/dls/02. im.puls - Ich weiß wie, aber warum....ogg"},
		{name:"The Foxery Jam – A Monkey’s Mind",mp3:"http://www.stereomag.net/dls/03. The Foxery Jam - A Monkey's Mind.mp3",ogg:"http://www.stereomag.net/dls/03. The Foxery Jam - A Monkey's Mind.ogg"},
		{name:"The Voodoolectric – Exhausted",mp3:"http://www.stereomag.net/dls/04. The Voodoolectric - Exhausted.mp3",ogg:"http://www.stereomag.net/dls/04. The Voodoolectric - Exhausted.ogg"},
		{name:"Tsunami – Fire In Your Head",mp3:"http://www.stereomag.net/dls/05. Tsunami - Fire In Your Head!.mp3",ogg:"http://www.stereomag.net/dls/05. Tsunami - Fire In Your Head!.ogg"},
		{name:"Stop Play and Record – Eu Gosto Tanto Disso",mp3:"http://www.stereomag.net/dls/06. Stop Play and Record - Eu Gosto Tanto Disso.mp3",ogg:"http://www.stereomag.net/dls/06. Stop Play and Record - Eu Gosto Tanto Disso.ogg"},
		{name:"Tomorrow – Petrol Patrol",mp3:"http://www.stereomag.net/dls/07. Tomorrow - Petrol Patrol.mp3",ogg:"http://www.stereomag.net/dls/07. Tomorrow - Petrol Patrol.mp3.ogg"},
		{name:"Fat Belly – Assking",mp3:"http://www.stereomag.net/dls/08. Fat Belly - Assking.mp3",ogg:"http://www.stereomag.net/dls/08. Fat Belly - Assking.ogg"},
		{name:"Battue – Dawn Of The Night",mp3:"http://www.stereomag.net/dls/09. Battue - Dawn Of Night.MP3",ogg:"http://www.stereomag.net/dls/09. Battue - Dawn Of Night.ogg"},
		{name:"Puppendoktor Pille – Schwer",mp3:"http://www.stereomag.net/dls/10. Puppendoktor Pille - Schwer.mp3",ogg:"http://www.stereomag.net/dls/10. Puppendoktor Pille - Schwer.ogg"}
	];

	// 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(true); // Parameter is a boolean for autoplay.
			demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
		},
		oggSupport: true
	})
	.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();
		$(this).blur();
		return false;
	});

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

	function displayPlayList() {
		$("#jplayer_playlist ul").empty();
		for (i=0; i < myPlayList.length; i++) {
			var listItem = (i == myPlayList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
			listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a></li>";
			$("#jplayer_playlist ul").append(listItem);
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
				$(this).blur();
				return false;
			});
		}
	}

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

	function playListConfig( index ) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().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 );
	}

});
