var ojiis_player_is_loaded = false;

function ojiis_player_init(data)
{	
	$('#playlist_loading_info').remove();
	
    var playItem = 0;
    
    var currentPageStart = 0;
    
    var myPlayList = data;
    
    var totalEpisodes = myPlayList.length;
    
    var jplr = $("#jquery_jplayer");
 
    function displayPlayList() {
        var playlist = $('#playlist_list ul');
    	playlist.append('<li id="playlist_previous_page" class="paginator offpage">&laquo; Newer Episodes</li>');
        for (i=0; i < totalEpisodes; i++) {
            $("#playlist_list ul").append("<li id='playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
            var item = $("#playlist_item_" + i);
            item.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");
                if (playItem != index) {
                    playListChange( index );
                } else {
                    $("#jquery_jplayer").play();
                }
            });
            // Hide off-page tracks (i bigger than 9 (myPlayList index starts at 0))
            if (i > 9)
            {
            	item.addClass('offpage');
            }
        }
    	playlist.append('<li id="playlist_next_page" class="paginator offpage">Older Episodes &raquo;</li>');
    	
        var prevPageButton = $('#playlist_previous_page');
        var nextPageButton = $('#playlist_next_page');
        
        if (totalEpisodes > currentPageStart + 10)
        {
        	nextPageButton.removeClass('offpage');
        }
        
        // Set pagination functions
        prevPageButton.click( function () {
        	// Hide old page
        	for (i=currentPageStart; i < currentPageStart + 10; i++)
        	{
        		$('#playlist_item_'+i).addClass('offpage');
        	}
        	currentPageStart -= 10;
        	// hide/show pagination buttons
        	if (currentPageStart == 0)
        	{
        		prevPageButton.addClass('offpage');
        	}
        	if (nextPageButton.hasClass('offpage'))
        	{
        		nextPageButton.removeClass('offpage');
        	}
        	// Show new page
        	for (i=currentPageStart; i < currentPageStart + 10; i++)
        	{
        		$('#playlist_item_'+i).removeClass('offpage');
        	}
        });
        
        nextPageButton.click( function (){
        	// Hide old page
        	for (i=currentPageStart; i < currentPageStart + 10; i++)
        	{
        		$('#playlist_item_'+i).addClass('offpage');
        	}
        	currentPageStart += 10;
        	// hide/show pagination buttons
        	if (currentPageStart + 10 > totalEpisodes)
        	{
        		nextPageButton.addClass('offpage');
        	}
        	if (prevPageButton.hasClass('offpage'))
        	{
        		prevPageButton.removeClass('offpage');
        	}
        	// Show new page
        	for (i=currentPageStart; i < currentPageStart + 10; i++)
        	{
        		$('#playlist_item_'+i).removeClass('offpage');
        	}
        });
    }
 
    function playListInit(autoplay) {
        if(autoplay) {
            playListChange( playItem );
        } else {
            playListConfig( playItem );
        }
    }
 
    function playListConfig( index ) {
        $("#playlist_item_"+playItem).removeClass("playlist_current");
        $("#playlist_item_"+index).addClass("playlist_current");
        playItem = index;
        jplr.jPlayer("setFile", myPlayList[playItem].mp3);
    }
 
    function playListChange( index ) {
        playListConfig( index );
        jplr.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 );
    }
    jplr.jPlayer({
        ready: function() {
    		$('#player_loading').remove();
            displayPlayList();
            playListInit(false);
        },
        swfPath: '/wp-content/plugins/ojiis_player',
        oggSupport: false
    });
    jplr.jPlayer("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);
    });
    jplr.jPlayer("onSoundComplete", function() {
        playListNext();
    });
 
    $("#ctrl_prev").click( function() {
        playListPrev();
        return false;
    });
 
    $("#ctrl_next").click( function() {
        playListNext();
        return false;
    });
    
    $("#ctrl_download").click(function(){
        window.location = myPlayList[playItem].mp3;
    });
}

function documentReady()
{
    if (!ojiis_player_is_loaded)
    {
        
        ojiis_player_is_loaded = true;
    }
}

$(document).ready(function(){$.getJSON("/wp-content/plugins/ojiis_player/playlist.php", ojiis_player_init);});
