$(document).ready(function(){
	playItem = -1;
	
	$("#jquery_jplayer_1").jPlayer({
        backgroundColor: "transparent",
		ready: function () {
			displayPlayList();
		},
		ended: function (event) {
			//$(this).jPlayer("play");
            playListNext();
		},
		swfPath: "http://www.sheerblue.nl/scripts/jplayer/",
		supplied: "mp3"
	}).jPlayer("onSoundComplete", function() {
        playListNext();
    });

	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_1").jPlayer("play");
                }
                $(this).blur();
                return false;
            });
        }
    }
	
    $("#jplayer_play").click(function() {
        if (playItem == -1)
        {
            playListConfig(0);
            $("#jquery_jplayer_1").jPlayer("play");
        }
    });
    
    $("#jplayer_previous").click( function() {
        playListPrev();
        $(this).blur();
        return false;
    });

    $("#jplayer_next").click( function() {
        playListNext();
        $(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_1").jPlayer("setMedia", {
			mp3: window.window.myPlayList[playItem].mp3,
		});
		
		//$("#jplayer_playlist_item_"+index).parent().animate({backgroundPosition:"0 -250px"}, {duration: 500});
    }

    function playListChange( index ) {
        playListConfig( index );
        $("#jquery_jplayer_1").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 );
    }
});
