﻿$(document).ready(init);

var songIndex = 0;
var songs = [];

function init() {
    //SHOW AFTER DOCUMENT IS READY. PREVENTS DISPLAY BEFORE LAYOUT IS READY
    $("#player-container").css("display" ,"block");

    $("div.playlist div a").each(function(i) {
        $(this).attr("song-index", i);
        songs.push($(this).attr("url"));
    });

    $("#player-container div.control-panel ul li").mousedown(function() {
        var t = $(this);
        t.attr("class", t.attr("id") + "-down");
    });
    $("#player-container div.control-panel ul li").mouseup(function() {
        var t = $(this);
        t.attr("class", t.attr("id"));
    });

    $("#jpId").jPlayer({
        ready: function() {
            $("#prev").click(function() {
                if (songIndex > 0)
                    songIndex--;

                setSong(songIndex, true);
            });

            $("#next").click(function() {
                if (songIndex < songs.length - 1)
                    songIndex++;

                setSong(songIndex, true);
            });

            $("div.playlist div a").click(function(e) {
                songIndex = $(this).attr("song-index");
                setSong(songIndex, true);
            });

            $("#volBarContainer").click(function(e) {
                setVolumeBarValue($("#jpId").jPlayerGetInfo("volume"));
            });

            $("#volMin").click(function(e) {
                setVolumeBarValue($("#jpId").jPlayerGetInfo("volume"));
            });
            $("#volMax").click(function(e) {
                setVolumeBarValue($("#jpId").jPlayerGetInfo("volume"));
            });

            setSong(0, true);
            setVolumeBarValue($("#jpId").jPlayerGetInfo("volume"));
        },
        swfPath: 'scripts'
    });

    $("#jpId").jPlayerId("play", "play");
    $("#jpId").jPlayerId("pause", "pause");
    $("#jpId").jPlayerId("stop", "stop");
    $("#jpId").jPlayerId("loadBar", "loadBar");
    $("#jpId").jPlayerId("playBar", "playBar");
    $("#jpId").jPlayerId("volumeBar", "volBarContainer");
    $("#jpId").jPlayerId("volumeMin", "volMin");
    $("#jpId").jPlayerId("volumeMax", "volMax");

    $("#jpId").onProgressChange(function(loadPercent, ppr, playedPercentAbsolute, playedTime, totalTime) {
        $("#player-container span.playTime").text(toMMSS(playedTime));
        $("#player-container span.totalTime").text(toMMSS(totalTime));

        if (playedTime == totalTime && totalTime != "0") {
            if (songIndex < songs.length - 1)
                $("#next").trigger("click");
            else
                $("#jpId").stop();
        }
    });

    $("#jpId").onSoundComplete(function() {

    })
}

function setSong(index, play) {
    if (songIndex < 0 || songIndex >= songs.length)
        return;

    $("#player-container div.playlist div").removeClass("playing");
    var div = $("#player-container div.playlist div:eq(" + index + ")");
    div.addClass("playing");

    $("#player-container div.playlist").scrollTo("div:eq(" + index + ")");

    $("#jpId").setFile(songs[index]);
    $("#player-container div.title-bar span").html(div.find("a").attr("song-name") + "...");
    if (play) {
        $("#jpId").play();
        $("#pause").css("display", "block");
        //$("div.playlist div:eq(" + index + ") a[active=Y]");
        //$("#player-container div.playlist ul li:eq(" + index + ")").addClass("playing");
    }
}
