// JavaScript Document

// Creates and fills a two column table with a graph from the data from the array.
(function () {
    var theTable = document.getElementById("spreadtable"),
        theTableBody = theTable.tBodies[0],
        count = 0,
        total = 0,
        limit,
        grLen = ticklistOpts.grades.length,
        count = new Array(grLen),
        personal = new Array(grLen),
        i,
        j,
        rte,
        multiplier = ticklistOpts.multiplier;

    limit = ticklistOpts.routes.length;

    for( i = 0; i < grLen; i += 1) {
        count[i] = 0;
        personal[i] = 0;
    }

    for (i = 0; i < limit; i += 1) {
        rte = ticklistOpts.routes[i];
        for(j = grLen - 1; j > -1; j -= 1) {
            if (rte.grade.indexOf(ticklistOpts.grades[j]) === 0) {
                count[j] += 1;
                if (rte.flags & 0x01) {
                    personal[j] += 1;
                }
                break;
            }
        }
    }

    // To get the page to validate there needs to be a row in the table body, however it is 
    // not needed by the viewer of the page, so delete it before adding the real content.
    theTableBody.deleteRow(0);
    for (i = 0; i < grLen; i += 1) {
        var newRow = theTableBody.insertRow(-1),
            innerText = (i % 2) ? "<b>" + ticklistOpts.grades[i] + "<\/b>" : ticklistOpts.grades[i] ;

        if(count[i] > 0) {
            innerText += " (" + personal[i] + "/" + count[i] + ")";
        }
        newRow.insertCell(0).innerHTML = innerText;
        if (count[i] > 0) {
            if(personal[i] > 0) {
                newRow.insertCell(1).innerHTML = "<img src='..\/graphics\/green.gif' height='15' width='" + personal[i] * multiplier + "'><img src='..\/pictures\/red.gif' height='15' width='" + (count[i]-personal[i]) * multiplier + "' alt=''\/>";
            } else {
                newRow.insertCell(1).innerHTML = "<img src='..\/pictures\/red.gif' height='15' width='" + count[i] * multiplier + "' alt=''\/>";
            }
        } else {
            newRow.insertCell(1).innerHTML = "<b>&nbsp;<\/b>";
        }
    }
}(ticklistOpts));


