// vim: set ts=4 sts=4 sw=4 et:
'use strict';

var contentDiv = document.getElementsByClassName('content')[0];
var meetString = document.getElementById('meet');

// TODO: Actually have a toggle for this.
var usingLbs = true;

// TODO: Share this with main-index.js. A bunch of functions can be shared, actually.
function weight(kg) {
    if (kg === undefined)
        return '';
    if (!usingLbs)
        return String(kg);
    return String(common.kg2lbs(kg));
}

function parseWeightClass(x) {
    if (x === undefined)
        return '';
    if (!usingLbs)
        return String(x);
    if (typeof x === 'number')
        return String(Math.floor(common.kg2lbs(x)));
    return String(Math.floor(common.kg2lbs(x.split('+')[0]))) + '+';
}


function number(num) {
    if (num === undefined)
        return '';
    return String(num);
}


function string(str) {
    if (str === undefined)
        return '';
    return str;
}


function maketd(str) {
    var td = document.createElement('td');
    td.appendChild(document.createTextNode(str));
    return td;
}


function weightMax(row, cola, colb) {
    var a = row[cola];
    var b = row[colb];
    if (a === undefined)
        return weight(b);
    if (b === undefined)
        return weight(a);
    return weight(Math.max(a,b));
}


// Fills in the <tbody> given the current query.
function getIndices(query) {
    // No query: nothing to draw.
    if (query.q === undefined) {
        return [];
    }

    function filter(row) {
        return row[opldb.NAME] === query.q;
    }

    var indices = db_make_indices_list();
    indices = db_filter(indices, filter);

    // Update the name display here: if the name matches something
    // in the database, we're safe from HTML injection.
    if (indices.length > 0) {
        lifterString.innerHTML = 'Meet Results for ' + query.q;
    } else {
        // Don't inject query.q here: may be HTML!
        lifterString.innerHTML = 'Lifter not found.'
    }

    var sortFn = common.getSortFn(sortCol.id, sortAsc);
    indices.sort(sortFn);
    return indices;
}


function appendtd(tr, string) {
    var td = document.createElement("td");
    td.appendChild(document.createTextNode(string));
    tr.appendChild(td);
}

function appendtdlink(tr, string, url) {
    var td = document.createElement("td");
    var a = document.createElement("a");
    a.setAttribute('href', url);
    a.appendChild(document.createTextNode(string));
    td.appendChild(a);
    tr.appendChild(td);
}

function appendtdraw(tr, innerHTML) {
    var td = document.createElement("td");
    td.innerHTML = innerHTML;
    tr.appendChild(td);
}


function buildtable(indices) {
    var frag = document.createDocumentFragment();
    var table = document.createElement("table");
    frag.appendChild(table);

    var thead = document.createElement("thead");
    table.appendChild(thead);
    var tr = document.createElement("tr");
    thead.appendChild(tr);

    var cols = ["Wilks Place", "Div Place", "Name", "Division", "Sex", "Age", "Equip", "Class",
                "Weight", "Squat", "Bench", "Deadlift", "Total", "Wilks"];
    for (var i = 0; i < cols.length; ++i) {
        var td = document.createElement("td");
        td.appendChild(document.createTextNode(cols[i]));
        tr.appendChild(td);
    }

    var tbody = document.createElement("tbody");
    table.appendChild(tbody);

    for (var i = 0; i < indices.length; ++i) {
        var tr = document.createElement("tr");
        tbody.appendChild(tr);

        var rowobj = common.makeRowObj(opldb.data[indices[i]], 0);
        appendtd(tr, String(i+1));
        appendtd(tr, rowobj.place);
        appendtdraw(tr, rowobj.name);
        appendtd(tr, rowobj.division);
        appendtd(tr, rowobj.sex);
        appendtd(tr, rowobj.age);
        appendtd(tr, rowobj.equip);
        appendtd(tr, rowobj.weightclass);
        appendtd(tr, rowobj.bw);
        appendtd(tr, rowobj.squat);
        appendtd(tr, rowobj.bench);
        appendtd(tr, rowobj.deadlift);
        appendtd(tr, rowobj.total);
        appendtd(tr, rowobj.wilks);
    }

    return frag;
}


function onload() {
    var query = common.getqueryobj();
    var meetid = db_get_meetid(query.f, query.d, query.n)
    if (meetid === -1)
        return;

    // TODO: This could be made faster by using binary search, since MeetID is sequential.
    var indices = db_make_indices_list();
    indices = db_filter(indices, function(x) { return x[opldb.MEETID] === meetid; });

    meetString.innerHTML = query.f + " &nbsp;/ &nbsp;" + query.d + " &nbsp;/ &nbsp;" + query.n;

    indices = db_sort_numeric_maxfirst(indices, opldb.WILKS);
    contentDiv.appendChild(buildtable(indices));
}


document.addEventListener("DOMContentLoaded", onload);

@@ -1285,789 +1285,8 @@
 %7D%0A%0A%0A
-// Fills in the %3Ctbody%3E given the current query.%0Afunction getIndices(query) %7B%0A    // No query: nothing to draw.%0A    if (query.q === undefined) %7B%0A        return %5B%5D;%0A    %7D%0A%0A    function filter(row) %7B%0A        return row%5Bopldb.NAME%5D === query.q;%0A    %7D%0A%0A    var indices = db_make_indices_list();%0A    indices = db_filter(indices, filter);%0A%0A    // Update the name display here: if the name matches something%0A    // in the database, we're safe from HTML injection.%0A    if (indices.length %3E 0) %7B%0A        lifterString.innerHTML = 'Meet Results for ' + query.q;%0A    %7D else %7B%0A        // Don't inject query.q here: may be HTML!%0A        lifterString.innerHTML = 'Lifter not found.'%0A    %7D%0A%0A    var sortFn = common.getSortFn(sortCol.id, sortAsc);%0A    indices.sort(sortFn);%0A    return indices;%0A%7D%0A%0A%0A
 func
@@ -1665,24 +1665,60 @@
 ndChild(a);%0A
+    td.style.whiteSpace = %22nowrap%22;%0A
     tr.appen
@@ -1809,24 +1809,24 @@
 ment(%22td%22);%0A
-
     td.inner
@@ -1843,16 +1843,52 @@
 erHTML;%0A
+    td.style.whiteSpace = %22nowrap%22;%0A
     tr.a

Set nowrap on meet <td>s for a little sanity.