/**
 * Handle filter creation for standings page(s)
 *
 * @author Ryan Maulding <ryan@igl.net>
 */

var modes = {
    digit: [
        {text: "greater than", value: "+" },
        {text: "less than", value: "-" },
        {text: "is", value: "" },
        {text: "is not", value: "!" }
    ],
    text: [
        {text: "contains", value: "~" },
        {text: "doesn't contain", value: "!~" },
        {text: "begins with", value: "^" },
        {text: "ends with", value: "$" },
        {text: "is", value: "" },
        {text: "is not", value: "!" }
    ],
    select: [
        {text: "is", value: ""},
        {text: "is not", value: "!"}
    ],
    checkbox: []
};

var initializeFilters = function() {
    $("#add_filter").change( function() {
        addFilter($(this).val());
        this.selectedIndex = 0;
    });

    $("input[name^='rm_']").click(removeRow);
};

var removeRow = function() {
    tbody = $(this).parents("tbody");
    propertyName = tbody.attr("class");
    property = properties[propertyName];
    trows = tbody.find("tr");
    if (trows.length > 1) {
        th = $(this).parents("tr").find("th");
        if (th.text() == property.label) {
            tcols = $(this).parents("tr").find("td");
            if (tcols.length > 2) {
                mode = tcols.eq(0);
            }
            $(this).parents("tr").remove();
            th = tbody.find("tr").eq(0).find("th");
            if (th.text() != property.label) {
                th.removeAttr("colspan");
                th.text(property.label);
            }
            tcols = tbody.find("tr").eq(0).find("td");
            if (tcols.length < 3) {
                tcols.eq(0).before(mode);
            }
        } else {
            $(this).parents("tr").remove();
        }

        th = trows.find("th").eq(0);
        if (th.text() != property.label) {
            th.text(property.label);
        }
    } else {
        $("#add_filter option[value='"+propertyName+"']").removeAttr("disabled");
        tbody.remove();
    }
};

var addFilter = function(propertyName) {
    property = properties[propertyName];

    if ($("#add_filter option[value='"+propertyName+"']").eq(0).attr("disabled")) {
        alert("A filter already exists for that property.");
        return;
    }

    if (property.type == "checkbox") {
        $("#add_filter option[value='"+propertyName+"']").attr("disabled", "disabled");
    }

    tbody = $("#filters tbody."+propertyName);
    if (tbody.length < 1) {
        tbody = $("<tbody />").attr("class", propertyName);
        tbodies = $("#filters table tbody");
        tbodies.eq(tbodies.length - 2).after(tbody); 
    }

    trows = $("#filters tbody."+propertyName+" tr");

    tr = $("<tr />");
    th = $("<th />");
    if (trows.length < 1) {
        th.text(property.label);
    } else if (property.type == "digit") {
        th.html("<small>and</small>");
    } else {
        th = $("<th colspan=\"2\" />");
        th.html("<small>or</small>");
    }
    th.attr("scope", "row");
    tr.append(th);

    if ((trows.length < 1 || property.type == "digit") && property.type != "checkbox") {
        td = $("<td />").attr("class", "mode");
        select = $("<select />").attr("name", propertyName+"_"+trows.length+"_mode");
        mode = modes[property.type];
        for (i=0; i<mode.length; i++) {
            option = $("<option />");
            option.val(mode[i].value);
            option.text(mode[i].text);
            select.append(option);
        }
        td.append(select);
        tr.append(td);
    }

    td = $("<td />").attr("class", "filter");
    if (property.type == "checkbox") {
        td = $("<td colspan=\"2\" />").attr("class", "filter");
        for (i=0; i<property.options.length; i++) {
            checkbox = $("<input type=\"checkbox\" />").attr("name", propertyName+"_"+i+"_mode");
            checkbox.val(property.options[i]);
            label = $("<label />").attr("for", propertyName+"_"+i);
            label.text(property.options[i]);
            td.append(checkbox);
            td.append(label);
        }
    } else if (property.type == "select") {
        select = $("<select />").attr("name", propertyName+"_"+trows.length);
        for (i=0; i<property.options.length; i++) {
            option = $("<option />");
            option.val(property.options[i]);
            option.text(property.options[i]);
            select.append(option);
        }
        td.append(select);
    } else {
        input = $("<input type=\"text\" />").attr("name", propertyName+"_"+trows.length);
        td.append(input);
        if ((propertyName == "rank") && (trows.length < 1)) {
            td.append("&nbsp;(To find unranked users, use the filter 'Rank is 0')");
        }
    }
    tr.append(td);
    
    td = $("<td />").attr("class", "actions");
    input = $("<input type=\"button\" />");
    input.val("-");
    input.click(removeRow);
    td.append(input);
    tr.append(td);

    tbody.append(tr);
};

initializeFilters();
