

var lastSearch = "";

function init() {
    var startTime = new Date();
    var table = document.getElementById("tablebody");
    var i, j, tr, td1, td2, td3, a, x, fileType, dotPos;
    var counter = 1;
    for(i = 0, j = data.length; i < j; i++) {
        x = data[i];
        tr = document.createElement("tr");
        if(counter % 2 == 0)
            tr.className = "even";
        else
            tr.className = "odd";
        counter += 1;
        table.appendChild(tr);
        td1 = document.createElement("td");
        a = document.createElement("a");
        a.href = x[0];
        a.appendChild(document.createTextNode(x[0]));
        td2 = document.createElement("td");
        td3 = document.createElement("td");
        tr.appendChild(td1);
        tr.appendChild(td2);
        tr.appendChild(td3);
        td1.appendChild(a);
        dotPos = x[0].lastIndexOf(".");
        if(x[3] == "true")
            fileType = "dir";
        else if(dotPos != -1)
            fileType = x[0].substr(dotPos + 1, x[0].length - dotPos + 1);
        else
            fileType = "unknown";
        td1.className = "filename " + fileType;
        td2.appendChild(document.createTextNode(x[1]));
        td2.className = "filesize";
        if(td2.childNodes[0].nodeValue != "-")
            td2.className += " kb";
        td3.appendChild(document.createTextNode(x[2]));
        td3.className = "modified";
    }
    var status = document.getElementById("status");
    status.childNodes[0].nodeValue = status.childNodes[0].nodeValue + ((new Date() - startTime) / 1000) + "s";
}

function search(text) {
    text = text.toLowerCase();
    if(text == lastSearch)
        return;
    lastSearch = text;
    var table = document.getElementById("tablebody");
    var i, j, tr;
    var visible = 0;
    if(text) {
        for(i = 0, j = table.childNodes.length; i < j; ++i) {
            tr = table.childNodes[i];
            if(tr.childNodes[0].childNodes[0].childNodes[0].nodeValue.toLowerCase().indexOf(text) != -1) {
                tr.style.display = "";
                ++visible;
                tr.className = ((visible % 2 == 0) ? "even" : "odd");
            }
            else
                tr.style.display = "none";
        }
    }
    else {
        for(i = 0, j = table.childNodes.length; i < j; ++i) {
            tr = table.childNodes[i];
            tr.style.display = "";
            tr.className = ((i % 2 == 0) ? "even" : "odd");
        }
    }
}