﻿var currentPlayer = "";
var playing = 0;   // 1 = pause, 2 = playing.
var initialized = false;

function two(x) { return ((x > 9) ? "" : "0") + x }

function formatTime(ms) {
    var sec = Math.floor(ms / 1000);
    var min = Math.floor(sec / 60);
    return min + ":" + two(sec % 60);
}

function initializeSounds() {
    $(".barContainer").click(function(e) {
        var offset = $(this).offset();
        var relativeX = e.pageX - offset.left;
        seek($(this).attr("playerId"), relativeX);
    });

    for (var i = 0; i != tracks.length; ++i) {

        var updateFunction = function() {

            var duration = this.duration;
            if (this.readyState != 3) { // Not completely loaded yet. Estimate duration
                duration = parseInt((this.bytesTotal / this.bytesLoaded) * this.duration);
            }

            var playWidth = 200.0 * (this.position / duration);
            var bufferWidth = 200.0 * (this.bytesLoaded / this.bytesTotal);
            var position = formatTime(this.position);

            setBarPos(position, playWidth, bufferWidth, this.sID);
        };

        var sound = soundManager.createSound({
            id: tracks[i].Id,
            url: rootUrl + tracks[i].Url,
            whileloading: updateFunction,
            whileplaying: updateFunction,
            onfinish: stop,
            onload: function() { $("#duration" + this.sID).text(formatTime(this.duration)); },
            autoPlay: false,
            autoLoad: false
        });
    }
    initialized = true;
}

function setBarPos(position, playWidth, bufferWidth, player) {

    playWidth = parseInt(playWidth);
    bufferWidth = parseInt(bufferWidth);

    $("#position" + player).text(position);

    var bar1Width = playWidth;
    $("#player" + player + " .bar1").attr("style", "width: " + bar1Width + "px; "); // + (bar1Width < 1.0 ? "display: none" : ""));

    var bar2Width = bufferWidth - playWidth;
    $("#player" + player + " .bar2").attr("style", "width: " + bar2Width + "px; "); // + (bar2Width < 1.0 ? "display: none" : ""));

    var bar3Width = 200 - bufferWidth;
    $("#player" + player + " .bar3").attr("style", "width: " + bar3Width + "px; "); // + (bar3Width < 1.0 ? "display: none" : ""));
}

function playPauseClicked(player) {
    if (!initialized) return;

    if (playing == 0) {            // Not playing anything
        play(player, true);
    }
    else if (playing == 1) {      // A player is paused
        play(player, player != currentPlayer);
    }
    else if (playing == 2) {         // A player is playing
        if (player == currentPlayer)
            pause();
        else {
            play(player, player != currentPlayer);
        }
    }
}

function play(player, change) {
    $("#playPauseImage" + player).attr("src", "../Images/pause.png");

    if (change) {
        stop();
        soundManager.play(player);
    }
    else {
        var sound = soundManager.getSoundById(player);
        sound.resume();
    }

    currentPlayer = player;
    playing = 2;
}

function pause() {
    $("#playPauseImage" + currentPlayer).attr("src", "../Images/play.png");
    var sound = soundManager.getSoundById(currentPlayer);
    sound.pause();
    playing = 1;
}

function stop() {
    if (playing != 0) {
        $("#playPauseImage" + currentPlayer).attr("src", "../Images/play.png");
        var sound = soundManager.getSoundById(currentPlayer);
        if (sound.playState == 1)
            sound.stop();
        playing = 0;
    }
}

function stopClicked(player) {
    if (!initialized) return;

    if (playing != 0 && currentPlayer == player) {
        stop();
    }
}

function seek(player, position) {
    if (!initialized) return;

    if (playing != 0 && currentPlayer == player) {
        var sound = soundManager.getSoundById(currentPlayer);
        var duration = sound.duration;
        if (this.readyState != 3) { // Not completely loaded yet. Estimate duration
            duration = parseInt((sound.bytesTotal / sound.bytesLoaded) * sound.duration);
        }
        var calculatedPos = position * duration / 200.0;
        sound.setPosition(calculatedPos);
    }
}
