jQuery(document).ready(function($){

	var playItem = 0;
	var myPlayList = [
	  {name:"01- Dedications",mp3:"http://www.chadbeall.com/songs/Dedications/01.mp3"},
	  {name:"02- Steal My Heart",mp3:"http://www.chadbeall.com/songs/Dedications/02.mp3"},
	  {name:"03- Looking In",mp3:"http://www.chadbeall.com/songs/Dedications/03.mp3"},
	  {name:"05- Once In A Lifetime",mp3:"http://www.chadbeall.com/songs/Dedications/05.mp3"},
	  {name:"09- In the Mist",mp3:"http://www.chadbeall.com/songs/Dedications/09.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
 
	$("#jplayer").jPlayer({ //Main jplayer Div
		swfPath: "http://www.chadbeall.com/wp-content/themes/theme19-chadbeall-v2/scripts/jplayer", //Change This
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
		},
		nativeSupport: true, oggSupport: false, errorAlerts: 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 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 {
					$("jplayer").jPlayer("play"); //Main jplayer Div
				}
				$(this).blur();
				return false;
			});
			/*$("#jplayer_playlist_get_mp3_"+i).data("index", i).click(function() {
				var index = $(this).data("index");
				$("#jplayer_playlist_item_"+index).trigger("click");
				$(this).blur();
				return false;
			});*/ //This shows the download link for the song
		}
	}
	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;
		$("#jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg); //Main jplayer Div
	}
	function playListChange( index ) {
		playListConfig( index );
		$("#jplayer").jPlayer("play"); //Main jplayer Div
	}
	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 );
	}
});
