/*
 * This is "weightChart.js", a Javascript tool for generating
 * a chart of weight data.  To use it, simply include it in
 * your HEAD of the page, and then define your weight data,
 * ordered most-recent-first also in the HEAD section:
 *
 *   var weightData = new Array(
 *      new datum(2005,  6, 13, 161.0),
 *      new datum(2005,  6, 10, 164),
 *      ...
 *      new datum(2005,  6,  7, 167)
 *   );
 *
 * (Remember to leave the comma off the end of the last one.)
 *
 * Then call "showPage();" in the BODY of the page.
 *
 * The default values below can be changed by assigning new
 * values in your page.  (Don't use the "var" keyword there.)
 *
 *************************************************************
 * Copyright 2005 Rick Miller.
 * Licensed under the GPL.
 *************************************************************
 */

// Defaults:
var targetPounds = 150;
var targetTolerance = 5;
var x_min = 135;
var x_max = 195;
var y_empty_max = 9;
var chartChar = 'X';
var chartCharStep = 0.5;

function datum(year, month, day, pounds, comment)
{
        this.date = new Date(year, month - 1, day);
        this.pounds = pounds;
        this.comment = comment
}

function formattedDate(date)
{
        var year = (date.getYear() + 1900).toString();

        var month = (date.getMonth() + 1).toString();
        while (month.length < 2) {
                month = "0" + month;
        }

        var dayOfMonth = date.getDate().toString();
        while (dayOfMonth.length < 2) {
                dayOfMonth = "0" + dayOfMonth;
        }

        return year + "." + month + "." + dayOfMonth;
}

function writeBar(date, pounds, comment)
{
        var count;
        var kilograms;

        kilograms = 0.45359237 * pounds;

        document.write(formattedDate(date));
        document.write( " " + Math.round(pounds) + "lbs");
        document.write(" " + Math.round(kilograms) + "kg");
        document.write("\t");

        if (pounds <= x_min) {
                document.write("&lt;\n");
        }

        if (pounds > x_min) {
                count = Math.min(pounds, targetPounds - targetTolerance) - x_min;
                document.write("<font color=\"#0000ff\">");
                while (count > 0) {
                        document.write(chartChar);
                        count -= chartCharStep;
                }
                document.write("</font>");
        }

        if (pounds > (targetPounds - targetTolerance)) {
                count = Math.min(pounds - (targetPounds - targetTolerance), targetTolerance * 2);
                document.write("<font color=\"#00ff00\">");
                while (count > 0) {
                        document.write(chartChar);
                        count -= chartCharStep;
                }
                document.write("</font>");
        }

        if (pounds > (targetPounds + targetTolerance)) {
                count = Math.min(pounds - targetPounds - targetTolerance, x_max - targetPounds - targetTolerance);
                document.write("<font color=\"#ff0000\">");
                while (count > 0) {
                        document.write(chartChar);
                        count -= chartCharStep;
                }
                if (pounds > x_max) {
                        document.write("&gt;");
                }
                document.write("</font>");
        }

        if (comment) {
                document.write("\t(" + comment + ")");
        }

        document.write("\n");
}

function writeSpaces(count)
{
        if (count > y_empty_max) {
                var i;
                for (i = 0; i <= (y_empty_max / 3); i++) {
                        document.write("\n");
                }
                document.write("\t--------        [ GAP ]        --------\n");
                for (i = 0; i <= (y_empty_max / 3); i++) {
                        document.write("\n");
                }
        } else {
                while (count > 0) {
                        document.write("\n");
                        count--;
                }
        }
}

function daysDifferent(to_date, from_date)
{
        return Math.round((to_date - from_date) / (24 * 60 * 60 * 1000));
}

function drawChart()
{
        document.write("<pre><font size=\"1\">\n");

        for (var i = 0; i < weightData.length; i++) {
                writeBar(weightData[i].date,
                        weightData[i].pounds,
                        weightData[i].comment);
                if (i < (weightData.length - 1)) {
                        writeSpaces(daysDifferent(weightData[i].date, weightData[i + 1].date) - 1);
                }
        }

        document.write("</font></pre>\n");
}

function showPage()
{
        document.write("<h1>Rick's \"Brutally Honest\" Weight Chart</h1>\n\n");

        document.write("Target: " + targetPounds + " pounds<br>\n");
        document.write("Tolerance: +/- " + targetTolerance + " pounds\n");
        document.write("<p>\n");

        drawChart();
}

