// climbsum.js
// 
// Runs through the diary table and works out the number of routes 
// I've done at each grade and creates two summary tables (trad and
// sport) for this information.
// 
// Don't need to add events or whatever, just run the code hence the 
// (function () {})(); syntax.
// 

// Gets the text content of a cell.
function getCellValue(cellOrId) {
    var cell;
    if (typeof cellOrId === 'string') {
        cell = document.getElementById(cellOrId);
    } else {
        cell = cellOrId;
    }

    cell.normalize();
    if (cell.firstChild.nodeType === 3) {
        return cell.firstChild.nodeValue.replace(/^\s+|\s+$/g, '');
    } else if (cell.firstChild.nodeType === 1) {
        return cell.firstChild.firstChild.nodeValue.replace(/^\s+|\s+$/g, '');
    } else {
        return '';
    }
}

// This will get run automatically so no need for addEventListener or such like.
// It will automatically create the two summary tables.
(function () {
    var r = document.getElementById('diary'),
        s = document.getElementById('summary'),
        grades = [{grade: "E1", count: 0, led: 0, pts: 1, trad: 1},
                   {grade: "E2", count: 0, led: 0, pts: 2, trad: 1},
                   {grade: "E3", count: 0, led: 0, pts: 3, trad: 1},
                   {grade: "E4", count: 0, led: 0, pts: 4, trad: 1},
                   {grade: "E5", count: 0, led: 0, pts: 5, trad: 1},
                   {grade: "F6a", count: 0, led: 0, pts: 1, trad: 0},
                   {grade: "F6a+", count: 0, led: 0, pts: 1, trad: 0},
                   {grade: "F6b", count: 0, led: 0, pts: 2, trad: 0},
                   {grade: "F6b+", count: 0, led: 0, pts: 3, trad: 0},
                   {grade: "F6c", count: 0, led: 0, pts: 3, trad: 0},
                   {grade: "F6c+", count: 0, led: 0, pts: 4, trad: 0},
                   {grade: "F7a", count: 0, led: 0, pts: 4, trad: 0},
                   {grade: "F7a+", count: 0, led: 0, pts: 5, trad: 0}
                  ],
        rLen,
        gLen,
        i,
        g,
        t1,
        t2,
        b1,
        b2,
        c1 = [0, 0, 0, 0, 0],
        c2 = [0, 0, 0, 0, 0],
        d = document,
        row;

    if (!r || !s) {
        return;
    }
    rLen = r.rows.length;
    gLen = grades.length;

    for (i = 0; i < rLen; i++) {
        for (g = 0; g < gLen; g++) {
            if (getCellValue(r.rows[i].cells[2]) === grades[g].grade) {
                grades[g].count++;
                if (getCellValue(r.rows[i].cells[3]) === "y") {
                    grades[g].led++;
                }
            }
        }
    }

    // A couple of private functions to make the data rows and the header row.
    function mkRow(t, i) {
        var r = t.insertRow(-1);
        r.insertCell(0).innerHTML = grades[i].grade;
        r.insertCell(1).innerHTML = grades[i].count;
        r.insertCell(2).innerHTML = grades[i].led;
        r.insertCell(3).innerHTML = grades[i].count * grades[g].pts;
        r.insertCell(4).innerHTML = grades[i].led * grades[g].pts;
        if (grades[i].count > 0) {
            r.insertCell(5).innerHTML = Math.round((grades[i].led / grades[i].count) * 100) + "%";
        } else {
            r.insertCell(5).innerHTML = "";
        }
    }
    function mkHead() {
        var h = document.createElement('thead'),
            r = document.createElement('tr'),
            d = document;

        r.insertCell(0).appendChild(d.createTextNode('Grade'));
        r.insertCell(1).appendChild(d.createTextNode('Total'));
        r.insertCell(2).appendChild(d.createTextNode('Led'));
        r.insertCell(3).appendChild(d.createTextNode('Total Pts'));
        r.insertCell(4).appendChild(d.createTextNode('Lead Pts'));
        r.insertCell(5).appendChild(d.createTextNode('% led'));
        h.appendChild(r);

        return h;
    }
    // Now that we have all the summary info, add it to the document
    t1 = d.createElement('table');
    t2 = d.createElement('table');
    b1 = d.createElement('tbody');
    b2 = d.createElement('tbody');
    // Need to add the table headers
    t1.appendChild(mkHead());
    t2.appendChild(mkHead());

    // Loop over the grades object array and update the totals counters and make a row
    // with the grade specific information.
    for (g = 0; g < gLen; g++) {
        if (grades[g].trad === 1) {
            c1[0] += grades[g].count;
            c1[1] += grades[g].led;
            c1[2] += grades[g].count * grades[g].pts;
            c1[3] += grades[g].led * grades[g].pts;
            mkRow(b1, g);
        } else {
            c2[0] += grades[g].count;
            c2[1] += grades[g].led;
            c2[2] += grades[g].count * grades[g].pts;
            c2[3] += grades[g].led * grades[g].pts;
            mkRow(b2, g);
        }
    }
    // Now do the totals and give the row a class so we can use CSS to format it.
    row = b1.insertRow(-1);
    row.insertCell(0).innerHTML = "Totals";
    row.insertCell(1).innerHTML = c1[0];
    row.insertCell(2).innerHTML = c1[1];
    row.insertCell(3).innerHTML = c1[2];
    row.insertCell(4).innerHTML = c1[3];
    if (c1[0]) {
        row.insertCell(5).innerHTML = Math.round((c1[1] / c1[0]) * 100) + "%";
    } else {
        row.insertCell(5).innerHTML = "";
    }
    row.className = "tfooter";

    row = b2.insertRow(-1);
    row.insertCell(0).innerHTML = "Totals";
    row.insertCell(1).innerHTML = c2[0];
    row.insertCell(2).innerHTML = c2[1];
    row.insertCell(3).innerHTML = c2[2];
    row.insertCell(4).innerHTML = c2[3];
    if (c2[0]) {
        row.insertCell(5).innerHTML = Math.round((c2[1] / c2[0]) * 100) + "%";
    } else {
        row.insertCell(5).innerHTML = "";
    }
    row.className = "tfooter";

    t1.appendChild(b1);
    t1.createCaption().innerHTML = "Summary: Naturally Protected Climbs";
    t2.appendChild(b2);
    t2.createCaption().innerHTML = "Summary: Bolt Protected Climbs";
    s.appendChild(t1);
    s.appendChild(t2);
})();

