var playItem = 0;
var isPlaying = false;
var myPlayList = [
	{name: "South Kensington", mp3: "/static/audio/South_Kensington.mp3"},
	{name: "Thunderstorms",    mp3: "/static/audio/Thunderstorms.mp3"},
	{name: "Slow Me Down",     mp3: "/static/audio/Slow_Me_Down.mp3"},
	{name: "Swedish Reggae",   mp3: "/static/audio/Swedish_Reggae.mp3"},
	{name: "Coffee Man",       mp3: "/static/audio/Coffee_Man.mp3"},
	{name: "In Montreal",      mp3: "/static/audio/In_Montreal.mp3"}
];

function enablePlayer() {
    // Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");

    $('#jQueryPlayer').jPlayer({ 
        swfPath: "/static/swf",
        ready: function() {
            playListInit();
            displayPlayList();
        },
        ended: function() {
    		playListNext();
	    },
        pause: function() {
    	    isPlaying = false;
	        var el = $('#musicPlayerInterface .jp-play');
    	    $(el).removeClass('jp-pause');
        },
        solution: 'html, flash',
        supplied: 'mp3',
        preload: 'metadata',
        volume: 0.8,
        errorAlerts: false,
        muted: false,
        cssSelectorAncestor: '#musicPlayerInterface',
        cssSelector: {
            play: '.jp-play',
            stop: '.jp-stop',
            volumeBar: '.jp-volume-bar',
            volumeBarValue: '.jp-volume-bar-value',
            currentTime: '.jp-play-time',
            duration: '.jp-total-time'
        }
    })
     
    $("#musicPlayerInterface .jp-previous").click(function() {
        playListPrev();
        $(this).blur();
        return false;
    });

    $("#musicPlayerInterface .jp-next").click(function() {
        playListNext();
        $(this).blur();
        return false;
    });
	$('#musicPlayerInterface .jp-play').click(function() {
        if (isPlaying) {
    	    isPlaying = false;
    	    $(this).removeClass('jp-pause');
    	    $('#jQueryPlayer').jPlayer('pause');
    	    return false;
        } else {
    	    isPlaying = true;
    	    $(this).addClass('jp-pause');
	    }
    });
}

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 {
				$("#jQueryPlayer").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;
	$("#jQueryPlayer").jPlayer("setMedia", {mp3: myPlayList[playItem].mp3});
	$("#jplayer_currentsong").text(myPlayList[playItem].name);
}

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


