var myPlayList = [
    /*
    {name:"Purify Me",mp3:"music/purify_me.mp3"},
    {name:"Send Out Your Light",mp3:"music/send_out_your_light.mp3"},
    /**/
    {name:"Provocateurs", mp3:"podcasts/ProvocateursVII_12b30.mp3"},
    {name:"Shadows", mp3:"/podcasts/ShadowsI.mp3"}
];

function displayPlayList() {
    for (i=0; i < myPlayList.length; i++) {
	    $("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
	    $("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
		    var index = $(this).data("index");
		    if (playItem != index) {
			    playListChange( index );
		    } else {
			    $("#jquery_jplayer").jPlayer("play");
		    }
	    });
    }
}

function playListInit(autoplay) {
    if(autoplay) {
	    playListChange( playItem );
    } else {
	    playListConfig( playItem );
    }
}

function playListConfig( index ) {
    $("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
    $("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
    playItem = index;
    $("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
}

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 );
}

function setupPlayer()
{
    var jpPlayTime = $("#jplayer_play_time");
    var jpTotalTime = $("#jplayer_total_time");
    var jpStatus = $("#player_status"); // For displaying information about jPlayer's status in the demo page

    $("#jquery_jplayer").jPlayer({
	    ready: function() {
		    displayPlayList();
		    playListInit(false); // Parameter is a boolean for autoplay.
	    }
    })
    .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();
	    return false;
    });

    $("#jplayer_next").click( function() {
	    playListNext();
	    return false;
    });
}