var myPlayList;

var playListChange = function ( index ) {
	playItem = index;
	$("#jquery_jplayer").setFile(window.myPlayList[playItem].filename).play();
	window.metaDataChange(playItem);
}

var metaDataChange = function ( index ){
	$("#title").html('<em>'+window.myPlayList[index].title+'</em><br/>from <a href="'+window.myPlayList[index].albumLink+'" target="subframe">'+window.myPlayList[index].album+'</a>');
	$("#artist").html('<a href="'+window.myPlayList[index].artistLink+'" target="subframe">'+window.myPlayList[index].artist+'</a>');
	$("#alb").attr('src','img/albums/'+window.myPlayList[index].cover)
	$("#seemore").attr('href',window.myPlayList[index].link)
}


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

	$("#jquery_jplayer").jPlayer({
		ready: function() {
				$.ajax({
					type: 'get', url: '/getmusic.php', dataType: 'json',
					success: function(d){window.myPlayList = d;window.playListChange(0)}
				});
		},
		swfPath: 'js/jplayer'
	})
	.jPlayerId("play", "player_play")		
	.jPlayerId("pause", "player_pause")			
	.onSoundComplete( playListNext );
	
	$("#player_prev").click( function() {
		playListPrev();
		return false;
	});

	$("#player_next").click( function() {
		playListNext();
		return false;
	});




	function playListNext() {
		playItem = (playItem+1 < window.myPlayList.length) ? playItem+1 : 0;
		playListChange( playItem );
	}

	function playListPrev() {
		playItem = (playItem-1 >= 0) ? playItem-1 : window.myPlayList.length-1;
		playListChange( playItem );
	}
});