var json_file = "http://24u.ulyssis.org/sites/default/files/tussenstand.json";

function fmtnum(n) {
    if (n < 10) {
        return "0" + n;
    }
    return n;
}

function time(add) {
    var d = new Date();
    var d = new Date(d.getTime() + add * 1000);
    var h = fmtnum(d.getHours());
    var m = fmtnum(d.getMinutes());
    var s = fmtnum(d.getSeconds());
    return "Volgende update: " + h + ":" + m + ":" + s;
}

function score() {
    var update = 10;
    $(".teamname").css("visibility", "hidden");
    $(".num").hover(function() {
        $(this).next().css("visibility", "visible");
    },
    function() {
        $(this).next().css("visibility", "hidden");
    });
    $(".teamblocks").append('<span id="updatein">' + time(update) + '</span>');
    $(this).everyTime(update * 1000, 'lapupdate', function() {
        $.getJSON(json_file, function(data) {
            if (data.length <= 0) {
                return;
            }
            var newlist = $("#score").clone();
            newlist.empty();
            for (var i = 0; i < data.length; i++) {
                var id = "#team" + data[i][0];
                newlist.append($(id));
            }
            $("#score").replaceWith(newlist);
        });
        $("#updatein").text(time(update));
    });
}

$(document).ready(function() {
    if ($("#score").length > 0) {
        score();
    }
});

