$(document).ready(function(){
	
	
	var music_is_on = 0;
	
	var playItem = 0;

	var myPlayList = [
		{name:"These boots are made for walkin'",mp3:"audio/mp3/1.mp3",ogg:"audio/ogg/1.ogg"},
		{name:"Whole lotta love",mp3:"audio/mp3/2.mp3",ogg:"audio/ogg/2.ogg"},
		{name:"Hey you",mp3:"audio/mp3/3.mp3",ogg:"haudio/ogg/3.ogg"},
		{name:"Paper planes",mp3:"audio/mp3/4.mp3",ogg:"audio/ogg/4.ogg"},
		{name:"I believe in miracles",mp3:"audio/mp3/5.mp3",ogg:"audio/ogg/5.ogg"},
		{name:"Ready or not (reggae version)",mp3:"audio/mp3/6.mp3",ogg:"audio/ogg/6.ogg"},
		{name:"Down the drain",mp3:"audio/mp3/7.mp3",ogg:"audio/ogg/7.ogg"},
		{name:"Ain't no mountain high enough",mp3:"audio/mp3/8.mp3",ogg:"audio/ogg/8.ogg"}
];		    

	// 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(false); // Parameter is a boolean for autoplay.
			
                        var ctrlBar = "";

			for (i=0; i < 100; i++){
 				ctrlBar = ctrlBar + "<a href='#' id='lb-"+i+"'>|</a>";
			}

			$('#player_progress_ctrl_bar').html(ctrlBar);

		},
		oggSupport: true
	})
	.jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
 		var lpInt = parseInt(lp);
 		var ppaInt = parseInt(ppa);

 		global_lp = lpInt;

 		var loadBar = "";
 		for (l=0; l < lpInt; l++){
 			loadBar = loadBar + "|";
 		}

 		var playBar = "";
 		for (i=0; i < ppaInt; i++){
 			playBar = playBar + "|";
 		}

 		$('#player_progress_play_bar').text(playBar);
 		$('#player_progress_load_bar').text(loadBar);
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	$("#prev_button").click( function() {
		playListPrev();
		$(this).blur(); 
		return false;
	});
	
	$("#jplayer_play").click( function() {
		$("#jquery_jplayer").jPlayer("play");
		music_is_on = 1;
		return false;
	});

	$("#next_button").click( function() {
			playListNext();
			$(this).blur();
			return false;
	});
        
        $("#player_progress_ctrl_bar a").live( "click", function() {
		$("#jquery_jplayer").jPlayer("playHead", this.id.substring(3)*(100.0/global_lp));
		$(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 );
	}
	
        
});
