$(document).ready(function(){

	var playItem = 0;

		mp3Track = $('.mp3FileNames li');
			$(function(){
				mp3FilePath = new Array;
				var i= 0;
				mp3Track.each(function(){
					orig = $(this).text();
					var mp3Name = orig.replace(/^\s+|\s+$/g, '') ;
						mp3FilePath[i] = "\/assets\/mp3\/"+mp3Name+".mp3";
					i++;
				});
		});
	
		trackName = $('.mp3TrackNames li');
			$(function(){
				trackNameText = new Array();
				var i = 0;
				trackName.each(function(){
					trackNameText[i] = $(this).text();
					i++;
			});
		});

		oggTrack = $('.mp3FileNames li');
			$(function(){
				oggFilePath = new Array;
				var i= 0;
				oggTrack.each(function(){
					oorig = $(this).text();
					var oggName = oorig.replace(/^\s+|\s+$/g, '') ;
						oggFilePath[i] = "\/assets\/ogg\/"+oggName+".ogg";
					i++;
				});
		});

			trackNameOne = trackNameText[0];
			trackNameTwo = trackNameText[1];
			trackNameThree = trackNameText[3];

			mp3FilePathOne = mp3FilePath[0];
			mp3FilePathTwo = mp3FilePath[1];
			mp3FilePathThree = mp3FilePath[2];

			oggFilePathOne = oggFilePath[0];
			oggFilePathTwo = oggFilePath[1];
			oggFilePathThree = oggFilePath[2];



	if(trackName.length == 1) {
		myPlayList = [
			{name: trackNameOne, mp3: mp3FilePathOne, ogg: oggFilePathOne}
		];
	}

	if(trackName.length == 2) {
		myPlayList = [
			{name: trackNameOne, mp3: mp3FilePathOne, ogg: oggFilePathOne},
			{name: trackNameTwo, mp3: mp3FilePathTwo, ogg: oggFilePathTwo}
		];
	}

	if(trackName.length == 3) {
		myPlayList = [
			{name: trackNameOne, mp3: mp3FilePathOne, ogg: oggFilePathOne},
			{name: trackNameTwo, mp3: mp3FilePathTwo, ogg: oggFilePathTwo},
			{name: trackNameThree, mp3: mp3FilePathThree, ogg: oggFilePathThree}
		];
	}



	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(true); // Parameter is a boolean for autoplay.
		},
		swfPath: "http://raremusicrecordings.co.uk/assets/js/jplayer",
		oggSupport: false
	})
	.jPlayerId("play", "player_play")
	.jPlayerId("pause", "player_pause")
	.jPlayerId("stop", "player_stop")
	.jPlayerId("loadBar", "player_progress_load_bar")
	.jPlayerId("playBar", "player_progress_play_bar")
	.jPlayerId("volumeMin", "player_volume_min")
	.jPlayerId("volumeMax", "player_volume_max")
	.jPlayerId("volumeBar", "player_volume_bar")
	.jPlayerId("volumeBarValue", "player_volume_bar_value")
	.onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		var myPlayedTime = new Date(playedTime);
		var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
		var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
		$("#play_time").text(ptMin+":"+ptSec);

		var myTotalTime = new Date(totalTime);
		var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
		var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
		$("#total_time").text(ttMin+":"+ttSec);
	})
	.onSoundComplete( function() {
		playListNext();
	});

	$("#ctrl_prev").click( function() {
		playListPrev();
		return false;
	});

	$("#ctrl_next").click( function() {
		playListNext();
		return false;
	});

	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			$("#playlist_list ul").append("<li id='playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
			$("#playlist_item_"+i).data( "index", i ).hover(
				function() {
					if (playItem != $(this).data("index")) {
						$(this).addClass("playlist_hover");
					}
				},
				function() {
					$(this).removeClass("playlist_hover");
				}
			).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				}
			});
		}
	}

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

	function playListConfig( index ) {
		$("#playlist_item_"+playItem).removeClass("playlist_current");
		$("#playlist_item_"+index).addClass("playlist_current");
		playItem = index;
		$("#jquery_jplayer").setFile(myPlayList[playItem].mp3, myPlayList[playItem].ogg);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_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 );
	}
});
