$(document).ready(function(){

	var playItem = 0;

	var myPlayList2 = [		
		{name:"Introduction",mp3:"introducing/introduction.mp3"},
		{name:"Isabelle, Part I",mp3:"introducing/isabelle-part-i.mp3"},
		{name:"Isabelle, Part II",mp3:"introducing/isabelle-part-ii.mp3"},
		{name:"Libido",mp3:"introducing/libido.mp3"},
		{name:"Where We Belong",mp3:"introducing/where-we-belong.mp3"},
		{name:"Moment Unfurled",mp3:"introducing/moment-unfurled.mp3"},
		{name:"I Hope",mp3:"introducing/i-hope.mp3"},
		{name:"Let Me Hear That",mp3:"introducing/let-me-hear-that.mp3"},
		{name:"Being",mp3:"introducing/being.mp3"},
		{name:"The Twilight Zone",mp3:"introducing/the-twilight-zone.mp3"},
		{name:"Goodbye",mp3:"introducing/goodbye.mp3"}
	];

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

	$("#jquery_jplayer2").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
		},
		oggSupport: false,
		customCssIds: true

	})
	.jPlayer("cssId", "play", "jplayer_play2")
	.jPlayer("cssId", "pause", "jplayer_pause2")
	.jPlayer("cssId", "stop", "jplayer_stop2")
	.jPlayer("cssId", "loadBar", "jplayer_load_bar2")
	.jPlayer("cssId", "playBar", "jplayer_play_bar2")
	.jPlayer("cssId", "volumeMin", "jplayer_volume_min2")
	.jPlayer("cssId", "volumeMax", "jplayer_volume_max2")
	.jPlayer("cssId", "volumeBar", "jplayer_volume_bar2")
	.jPlayer("cssId", "volumeBarValue", "jplayer_volume_bar_value2")
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime2.text($.jPlayer.convertTime(playedTime));
		jpTotalTime2.text($.jPlayer.convertTime(totalTime));
	})

	.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_previous2").click( function() {
		playListPrev();
		return false;
	});

	$("#jplayer_next2").click( function() {
		playListNext();
		return false;
	});
	
	$("#jplayer_play2").click( pausePlayer );
	$("#jplayer_load_bar2").click( pausePlayer );
	$("#jplayer_play_bar2").click( pausePlayer );

	function pausePlayer(e) {
        $("#jquery_jplayer").jPlayer("pause");  // Pause the other player
	}



	function displayPlayList() {
		for (i=0; i < myPlayList2.length; i++) {
			$("#jplayer_playlist2 ul").append("<li id='jplayer_playlist2_item_"+i+"'>"+ myPlayList2[i].name +"</li>");
			$("#jplayer_playlist2_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer2").jPlayer("play");
				}
			});
		}
	}

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

	function playListConfig( index ) {
		$("#jplayer_playlist2_item_"+playItem).removeClass("jplayer_playlist_current");
		$("#jplayer_playlist2_item_"+index).addClass("jplayer_playlist_current");
		playItem = index;
		$("#jquery_jplayer2").jPlayer("setFile", myPlayList2[playItem].mp3);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer2").jPlayer("play"); 
		$("#jquery_jplayer").jPlayer("pause"), $("#jquery_jplayer3").jPlayer("pause"); // Pause the other player

	}

	function playListNext() {
		var index = (playItem+1 < myPlayList2.length) ? playItem+1 : 0;
		playListChange( index );
	}

	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList2.length-1;
		playListChange( index );
	}
	
});
