jQuery(document).ready(function(){

	var volumeChangeStep = 10;
	var currentVolume = 0;
	var tmpVolume = 0;
	
	var playItem = 0;

	var myPlayList = [
		{name:"background_atmo",mp3:"audioplayer/background_atmo_sauber.mp3"},
/*		{name:"failforever_stimmingremix_v2",mp3:"audioplayer/failforever_stimmingremix_v2.mp3"},
		{name:"comecloser_stimming_v2",mp3:"audioplayer/comecloser_stimming_v2.mp3"}, */
	];

	$("#jquery_jplayer").jPlayer({
		ready: function () {
			playListInit(true); // Parameter is a boolean for autoplay.
			currentVolume = $("#jquery_jplayer").jPlayer("getData", "volume");
		},
		volume: 10,
		swfPath: "audioplayer/fls"

	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
	/*	jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime)); */
		currentVolume = $("#jquery_jplayer").jPlayer("getData", "volume");
	})
	.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 ) {
		$("#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);
	}

	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 );
	}
	
	$("#jplayer_volume_min").click(function() {
		if(currentVolume>=10){
			tmpVolume = currentVolume - volumeChangeStep;
			$("#jquery_jplayer").jPlayer("volume", tmpVolume);
			$(this).blur();
			currentVolume = tmpVolume;
		}else{}
		
		return false;
	});
	
	$("#jplayer_volume_max").click(function() {
		if(currentVolume<=90){
			tmpVolume = currentVolume + volumeChangeStep;
			$("#jquery_jplayer").jPlayer("volume", tmpVolume);
			$(this).blur();
			currentVolume = tmpVolume;
		}else{}
		
		return false;
	});
});
