binAutoPlay = true; //set to true to auto- start music, false to not start playing on load. Should be set when calling initMusicPlayer


function initMusicPlayer(playId,autoPlay)
{
	var playListId = "default";
		
	if ( ( typeof(playId) != "undefined" )  &&  ( typeof(playId) != null ) ) {
		if(  playId.length > 0) { playListId = playId;}
	} 
	
	if(typeof(autoPlay) == "boolean" ) { binAutoPlay = autoPlay;}
	
	//load from xml based on an identifier.
	var fileName = "xml/songlist_" + playListId + ".xml";
	$.ajax({
	    type: "GET",
	    url: fileName,
	    dataType: "xml",
	    success: parseXml
	  });
}

function  parseXml(xml)
{
	var songObjectList = new Array(); 
	var songList = new Array();
	var playList  = "";
	var songTemp = "";
	var songCount = 0;
	$(xml).find("song").each(function()
	{
		objSong=new Object();
		objSong.name= $(this).find("title").text() + ' (' + $(this).find("composer").text() + '  -  ' + $(this).find("performer").text() + ') ';
		objSong.mp3 =   $(this).find("url").text();
		songObjectList.push(objSong);
			songTemp = "";
		if (songCount > 0) {songTemp = ",\n";}
		songTemp = songTemp + '{name:"' + $(this).find("title").text() + ' (' + $(this).find("composer").text() + ' ,  ' + $(this).find("performer").text() + ')", mp3:"http://www.newmusicworks.org/mus1c_f1l3s/01 Muscadines.mp3"}';
		//build sonTemp to match: {name:"Muscadines (Steed Cowart, Joan Jeanrenaud, cello; William Winant, marimba)",mp3:"http://www.newmusicworks.org/mus1c_f1l3s/01 Muscadines.mp3"},
		songList.push(songTemp );  
		songCount++;
	});
	
	 buildMusicPlayer(songObjectList)
} //end parseXML function

function buildMusicPlayer(songObjectList) {	
		var myPlayList = songObjectList;	
	var playItem = 0;
	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");
	var jpStatus = $("#fplayer_status"); // For displaying information about jPlayer's status 

	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(binAutoPlay); // Parameter is a boolean for autoplay.
		},
		oggSupport: false
	})
	.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 {
					$("#jquery_jplayer").jPlayer("play");
				}
				$(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, myPlayList[playItem].ogg);
	}

	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 );
	}
}
