$(document).ready(function(){

	var playItem = 0;

	var myPlayList = [
		{name:"1: <strong>Shepherds, Shake Off Your Drowsy Sleep</strong> (4:09*)",mp3:"/newsite/music/01-Shepherds.mp3"},
		{name:"2: <strong>It Soon Will Be Evening</strong> (4:44*)",mp3:"/newsite/music/02-Soon-it-will-be.mp3"},
		{name:"3: <strong>O Come, Litte Children</strong> (3:32*)",mp3:"/newsite/music/03-O-Come.mp3"},
		{name:"4: <strong>Here, While the Oxen Kneel in Joy</strong> (4:32*)",mp3:"/newsite/music/04-Here-while.mp3"},
		{name:"5: <strong>Bring a Torch, Jeannette, Isabelle</strong> (2:32*)",mp3:"/newsite/music/05-Bring-a-Torch.mp3"},
		{name:"6: <strong>Concert of Angels</strong> (5:01*)",mp3:"/newsite/music/06-Concert-of-Angels.mp3"},
		{name:"7: <strong>O Glorious Night</strong> (2:51*)",mp3:"/newsite/music/07-O-Glorious-Night.mp3"},
		{name:"8: <strong>No&#235;l de la V&#236;erge</strong> (5:01*)<br />Mary Wilson, Kristen Frost, soprano soloists",mp3:"/newsite/music/08-Noel-de-la.mp3"},
		{name:"9: <strong>Haidschi, Bumbaidschi</strong> (2:37*)",mp3:"/newsite/music/09-Haidschi.mp3"},
		{name:"10: <strong>Here is Joy for Every Age</strong> (3:31*)",mp3:"/newsite/music/10-Here-is-Joy.mp3"},
		{name:"11: <strong>If Ye Would Hear the Angels Sing</strong> (4:05*)",mp3:"/newsite/music/11-If-ye-would.mp3"},
		{name:"12: <strong>Kling, Gl&#246;ckchen, Kling</strong> (3:23*)",mp3:"/newsite/music/12-Kling.mp3"},
		{name:"13: <strong>Long, Long Ago</strong> (6:16*)",mp3:"/newsite/music/13-Long-long-ago.mp3"},
		{name:"14: <strong>Come, All Ye Shepherds</strong> (3:03*)",mp3:"/newsite/music/14-Come-Ye-Shepherds.mp3"},
		{name:"15: <strong>Stille Nacht, heilige Nacht</strong> (3:42*)",mp3:"/newsite/music/15-Stille-nacht.mp3"},
		{name:"16: <strong>Ding, Dong! Merrily on High</strong> (2:06*)",mp3:"/newsite/music/16-Ding-dong.mp3"}
	];


	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
			//demoInstanceInfo($(this), $("#jplayer_info"));
		},
		swfPath: '/newsite/css-js/js',
		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 );
	}
});


