﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="jquery.tmpl.js" />
/// <reference path="jquery.deserialize.js" />

if (!Array.prototype.filter) {
    Array.prototype.filter = function (fun /*, thisp */) {
        "use strict";

        if (this === void 0 || this === null)
            throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun !== "function")
            throw new TypeError();

        var res = [];
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in t) {
                var val = t[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, t))
                    res.push(val);
            }
        }

        return res;
    };
}

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
        "use strict";

        if (this === void 0 || this === null)
            throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0)
            return -1;

        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n !== n) // shortcut for verifying if it's NaN
                n = 0;
            else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
        }

        if (n >= len)
            return -1;

        var k = n >= 0
          ? n
          : Math.max(len - Math.abs(n), 0);

        for (; k < len; k++) {
            if (k in t && t[k] === searchElement)
                return k;
        }
        return -1;
    };
}

Array.prototype.uniq = function () {
    var cnt = {};
    var res = [];
    for (var i = 0; i < this.length; i++) {
        var t = this[i];
        cnt[t] = (cnt[t] || res.push(t) * 0) + 1;
    }
    return res;
};

alo1 = alo2 = typ1 = typ2 = []; //filled from vebra in the next scripts

$(document).ready(function () {
    var form = $("form");
    if (form.is("form")) {
        $.template("opt", '<option class="${c}" value="${v}">${n}</option>');

        var site = {
            lop: $("#lop"),
            hip: $("#hip"),
            alo: $("#alo"),
            typ: $("#typ")
        };

        var rm = function (e, i, a) { return (e != "(Not Specified)"); };
        alo1 = alo1.filter(rm);
        alo2 = alo2.filter(rm);
        typ1 = typ1.filter(rm);
        typ2 = typ2.filter(rm);

        var cls = function (a, n) {
            var result = "";
            var a1 = n == "a" ? alo1 : typ1;
            var a2 = n == "a" ? alo2 : typ2;
            if (a1.indexOf(a) != -1) {
                result += "buy ";
            }
            if (a2.indexOf(a) != -1) {
                result += "rent";
            }
            return result;
        };

        $.tmpl("opt", $.map(new Array().concat(alo1, alo2).uniq().sort(), function (a) { return { n: a, v: a, c: cls(a, "a")} })).appendTo(site.alo);
        $.tmpl("opt", $.map(new Array().concat(typ1, typ2).uniq().sort(), function (a) { return { n: a, v: a, c: cls(a, "t")} })).appendTo(site.typ);

        $("#lop>option[value!=0],#hip>option[value!=0]").filter(function (i) { return $(this).val() < 25000; }).addClass("rent");
        $("#lop>option[value!=0],#hip>option[value!=0]").filter(function (i) { return $(this).val() >= 25000; }).addClass("buy");
        $("option[value=0],option[value='']").addClass("buy rent");

        site.alo.o = site.alo.find("option");
        site.typ.o = site.typ.find("option");
        site.hip.o = site.hip.find("option");
        site.lop.o = site.lop.find("option");

        var update = function (n, c) {
            var p = site[n];
            p.empty().append(p["o"].filter(c));
            var f = n == "hip" ? p.find("option").last() : p.find("option").first();
            f.attr("selected", "selected");
        };

        $("#dbt").change(function () {
            var c = $(this).val() == 2 ? ".rent" : ".buy";
            $.each(["alo", "typ", "lop", "hip"], function (i, e) { update(e, c); });
        }).change();

        form.deserialize(form.attr("def"));
    }
});
