var total_scale = 99999;
var total_max = 550;
function total_bar(item) {
    if (total_max / item.registrations < total_scale) total_scale = total_max / item.registrations;
    return "<div class='stats_bars' style='width:" + (item.registrations * total_scale) + "px;background:#" + item.color + ";'></div><div class='stats_numbers'>" + item.registrations + " of " + item.population + "</div>";
}
var percent_scale = 99999;
var percent_max = 350;
function percent_bar(item) {
    if (percent_max / item.percent < percent_scale) percent_scale = percent_max / item.percent;
    return "<div class='stats_bars' style='width:" + (item.percent * percent_scale) + "px;background:#" + item.color + ";'></div><div class='stats_numbers'>" + item.percent + "% of " + item.population + "</div>";
}
jQuery(document).ready(function() {
    var set_total = true;
    var set_perc = true;

    jQuery.getJSON("/syuh/stats.php", {
        "totals": true
    },
    function(data) {
        jQuery("#total_regs").text(data.total);
    });

    jQuery.getJSON("/syuh/stats.php", {
        "sort": "total"
    },
    function(data) {
        var append_list = [];
        jQuery.each(data,
        function(i, item) {
            if (set_total) {
                jQuery("#highest_total").text(i);
                set_total = false;
            }
            append_list.push("<div class='stats'><strong>" + i + "</strong><br/>" + total_bar(item) + "</div><div class='clear'></div>");
        });
        jQuery("#by_total").append(append_list.join(''));
    });

    jQuery.getJSON("/syuh/stats.php", {
        "sort": "percent"
    },
    function(data) {
        var append_list = [];
        jQuery.each(data,
        function(i, item) {
            if (set_perc) {
                jQuery("#highest_percent").text(i);
                set_perc = false;
            }
            append_list.push("<div class='stats'><strong>" + i + "</strong><br/>" + percent_bar(item) + "</div><div class='clear'></div>");
        });
        jQuery("#by_percent").append(append_list.join(''));
    });
});