var DEBUGJPLAYER = false;
$(document).ready(function(){
	$("dd:not(:first)").hide();

	$("dt a").click(function(){
		$("dd:visible").slideUp("slow");
		$(this).parent().next().slideDown("slow");
		return false;
	});

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");
	var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(true); // Parameter is a boolean for autoplay.
			if (DEBUGJPLAYER){
				demoInstanceInfo(this.element, $("#demo_info")); // This displays information about jPlayer's configuration in the demo page
			}
		},
		oggSupport: true,
		nativeSupport: true,
		swfPath:"/js"

	})

	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));
		if(DEBUGJPLAYER){
			demoStatusInfo(this.element, jpStatus); // This displays information about jPlayer's status in the demo page
		}
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	$("#jplayer_previous").click( function() {
		playListPrev();
		$(this).blur();
		return false;
	});

	$("#jplayer_next").click( function() {
		playListNext();
		$(this).blur();
		return false;
	});


});

var playItem = 0;
var CURRENTPLAYLIST = "MORELOVE";

var moreLovePlayList = [
	{name:"Sing To You",mp3:"http://mluttrell.net/mp3/morelove/01-SingToYou.mp3",ogg:"http://mluttrell.net/ogg/morelove/01-SingToYou.ogg"},
	{name:"More Love",mp3:"http://mluttrell.net/mp3/morelove/02-MoreLove.mp3",ogg:"http://mluttrell.net/ogg/morelove/02-MoreLove.ogg"},
	{name:"Like Candy",mp3:"http://mluttrell.net/mp3/morelove/03-LikeCandy.mp3",ogg:"http://mluttrell.net/ogg/morelove/03-LikeCandy.ogg"},
	{name:"It's Your Fault",mp3:"http://mluttrell.net/mp3/morelove/04-ItsYourFault.mp3",ogg:"http://mluttrell.net/ogg/morelove/04-ItsYourFault.ogg"},
	{name:"Better This Time",mp3:"http://mluttrell.net/mp3/morelove/05-BetterThisTime.mp3",ogg:"http://mluttrell.net/ogg/morelove/05-BetterThisTime.ogg"},
	{name:"Comets and a Wish",mp3:"http://mluttrell.net/mp3/morelove/06-CometsAndAWish.mp3",ogg:"http://mluttrell.net/ogg/morelove/06-CometsAndAWish.ogg"},
	{name:"She Walks",mp3:"http://mluttrell.net/mp3/morelove/07-SheWalks.mp3",ogg:"http://mluttrell.net/ogg/morelove/07-SheWalks.ogg"},
	{name:"Verbally",mp3:"http://mluttrell.net/mp3/morelove/08-Verbally.mp3",ogg:"http://mluttrell.net/ogg/morelove/08-Verbally.ogg"},
	{name:"Whisper My Name",mp3:"http://mluttrell.net/mp3/morelove/09-WhisperMyName.mp3",ogg:"http://mluttrell.net/ogg/morelove/09-WhisperMyName.ogg"},
	{name:"Comets and a Wish (remix)",mp3:"http://mluttrell.net/mp3/morelove/10-CometsAndAWish-remix.mp3",ogg:"http://mluttrell.net/ogg/morelove/10-CometsAndAWish-remix.ogg"}
];

var justShinePlayList = [
	{name:"Heavy Magic",mp3:"http://mluttrell.net/mp3/justshine/01-HeavyMagic.mp3",ogg:"http://mluttrell.net/ogg/justshine/01-HeavyMagic.ogg"},
	{name:"JustShine",mp3:"http://mluttrell.net/mp3/justshine/02-JustShine.mp3",ogg:"http://mluttrell.net/ogg/justshine/02-JustShine.ogg"},
	{name:"Phoenix Dreams",mp3:"http://mluttrell.net/mp3/justshine/03-PhoenixDreams.mp3",ogg:"http://mluttrell.net/ogg/justshine/03-PhoenixDreams.ogg"},
	{name:"Drifting In You",mp3:"http://mluttrell.net/mp3/justshine/04-DriftingInYou.mp3",ogg:"http://mluttrell.net/ogg/justshine/04-DriftingInYou.ogg"},
	{name:"Fruit Tree",mp3:"http://mluttrell.net/mp3/justshine/05-FruitTree.mp3",ogg:"http://mluttrell.net/ogg/justshine/05-FruitTree.ogg"},
	{name:"Fourth Floor",mp3:"http://mluttrell.net/mp3/justshine/06-FourthFloor.mp3",ogg:"http://mluttrell.net/ogg/justshine/06-FourthFloor.ogg"},
	{name:"Sky",mp3:"http://mluttrell.net/mp3/justshine/07-Sky.mp3",ogg:"http://mluttrell.net/ogg/justshine/07-Sky.ogg"},
	{name:"Line",mp3:"http://mluttrell.net/mp3/justshine/08-Line.mp3",ogg:"http://mluttrell.net/ogg/justshine/08-Line.ogg"},
	{name:"Drive",mp3:"http://mluttrell.net/mp3/justshine/09-Drive.mp3",ogg:"http://mluttrell.net/ogg/justshine/09-Drive.ogg"}
];

function displayPlayList() {
	playItem = 0;
	var playList = moreLovePlayList;
	if (CURRENTPLAYLIST == "JUSTSHINE")	{ playList = justShinePlayList; }
	
	$("#jplayer_playlist ul").empty();
	for (i=0; i < playList.length; i++) {
		var listItem = (i == playList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
		listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ playList[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 ) {
	var playList = moreLovePlayList;
	if (CURRENTPLAYLIST == "JUSTSHINE"){ playList = justShinePlayList; }

	$("#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", playList[playItem].mp3, playList[playItem].ogg);
}

function playListChange( index ) {
	playListConfig( index );
	$("#jquery_jplayer").jPlayer("play");
}

function playListNext() {
	var playList = moreLovePlayList;
	if (CURRENTPLAYLIST == "JUSTSHINE")	{ playList = justShinePlayList; }

	var index = (playItem+1 < playList.length) ? playItem+1 : 0;
	playListChange( index );
}

function playListPrev() {
	var playList = moreLovePlayList;
	if (CURRENTPLAYLIST == "JUSTSHINE")	{ playList = justShinePlayList; }
	var index = (playItem-1 >= 0) ? playItem-1 : playList.length-1;
	playListChange( index );
}

function displayMoreLove()
{
	var options = {};

	$("#albumMoreLove").effect("shake",options,500,null);
	if (CURRENTPLAYLIST != "MORELOVE")	{
		CURRENTPLAYLIST = "MORELOVE"
		displayPlayList();
		playListInit(false); // Parameter is a boolean for autoplay.
	}

}

function displayJustShine()
{
	var options = {};

	$("#albumJustShine").effect("shake",options,500,null);
	if (CURRENTPLAYLIST != "JUSTSHINE")	{
		CURRENTPLAYLIST = "JUSTSHINE"
		displayPlayList();
		playListInit(false); // Parameter is a boolean for autoplay.
	}
}

function demoInstanceInfo(myPlayer, myInfo) {
	var jPlayerInfo = "<p>This jPlayer instance is running in your browser using ";

	if(myPlayer.jPlayer("getData", "usingFlash")) {
		jPlayerInfo += "<strong>Flash</strong> with ";
	} else {
		jPlayerInfo += "<strong>HTML5</strong> with ";
	}
	
	if(myPlayer.jPlayer("getData", "usingMP3")) {
		jPlayerInfo += "<strong>MP3</strong>";
	} else {
		jPlayerInfo += "<strong>OGG</strong>";
	}
	
	
	jPlayerInfo += " files.<br />This instance is using the constructor options:<br /><code>$(\"#" + myPlayer.jPlayer("getData", "id") + "\").jPlayer({<br />";
	
	jPlayerInfo += "&nbsp;&nbsp;&nbsp;nativeSupport: " + myPlayer.jPlayer("getData", "nativeSupport");
	jPlayerInfo += ", oggSupport: " + myPlayer.jPlayer("getData", "oggSupport");
	jPlayerInfo += ", customCssIds: " + myPlayer.jPlayer("getData", "customCssIds");


	jPlayerInfo += "<br />});</code></p>";
	myInfo.html(jPlayerInfo);
}

function demoStatusInfo(myPlayer, myInfo) {
	var jPlayerStatus = "<p>jPlayer is ";
	jPlayerStatus += (myPlayer.jPlayer("getData", "diag.isPlaying") ? "playing" : "stopped");
	jPlayerStatus += " at time: " + Math.floor(myPlayer.jPlayer("getData", "diag.playedTime")) + "ms.";
	jPlayerStatus += " (tt: " + Math.floor(myPlayer.jPlayer("getData", "diag.totalTime")) + "ms";
	jPlayerStatus += ", lp: " + Math.floor(myPlayer.jPlayer("getData", "diag.loadPercent")) + "%";
	jPlayerStatus += ", ppr: " + Math.floor(myPlayer.jPlayer("getData", "diag.playedPercentRelative")) + "%";
	jPlayerStatus += ", ppa: " + Math.floor(myPlayer.jPlayer("getData", "diag.playedPercentAbsolute")) + "%)</p>"

	jPlayerStatus += "src: " + myPlayer.jPlayer("getData", "diag.src");	

	myInfo.html(jPlayerStatus);
}

