$(document).ready(function(){

	var playItem = 0;
	var firstTime = true;
	
  var myPlayList = [
		{name:"Lost And Lonely",    mp3:"http://ilovecde.com/_mp3/lost_and_lonely.mp3"},
		{name:"Cheated Love",       mp3:"http://ilovecde.com/_mp3/cheated_love.mp3"},
		{name:"The Love You Bring", mp3:"http://ilovecde.com/_mp3/the_love_you_bring.mp3"},
		{name:"It\'s Obvious",      mp3:"http://ilovecde.com/_mp3/its_obvious.mp3"}
	];


	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false);
		},
		oggSupport: false
	})
	.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();
	});

/*
	.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")
*/


/*
	$("#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><a id='playlist_item_"+i+"'><span>"+ myPlayList[i].name +"</span></a></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");
				
				  // Check to see if we should pause or play
				if($(this).hasClass("playing")){
				  updateListPlayStatus( index, false );				
				  $("#jquery_jplayer").pause();
				}else{
				  updateListPlayStatus( index, true );
  				if (playItem != index) {
  					playListChange( index );
  				} else {
            $("#jquery_jplayer").play();
  				}
				}
			});
		}
	}

	function playListInit( autoplay ) {
    if(autoplay){
      firstTime = false;
    }
		playListChange( playItem );
	}

	function playListConfig( index ) {
    $("#playlist_item_"+playItem).removeClass("playlist_current");
    $("#playlist_item_"+index).addClass("playlist_current");
    updateListPlayStatus( index, true );
		playItem = index;
		$("#jquery_jplayer").setFile(myPlayList[playItem].mp3, myPlayList[playItem].ogg);
	}
	
	function updateListPlayStatus( index, isPlaying ){
    if( !firstTime ){
      if( isPlaying ){
        $("#playlist_item_"+playItem).removeClass("paused");
        $("#playlist_item_"+playItem).removeClass("playing");
        $("#playlist_item_"+index).addClass("playing");
      }else{
        $("#playlist_item_"+playItem).addClass("paused");
        $("#playlist_item_"+playItem).removeClass("playing");
      }
    }
	}

	function playListChange( index ) {
		playListConfig( index );
    if(!firstTime){
  		$("#jquery_jplayer").play();
    }else{
      firstTime = false;
    }
	}

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