﻿/// <reference path="jquery-1.4.4.js" />
String.prototype.startsWith = function (a) { return this.substr(0, a.length) === a;}
var Srch = {
    behavior: { HIDING: 0, SHOWING: 1 }, form: null,
    addForm: function (selector, options) {
        Srch.form = new Srch.searchForm(selector, options);
    },
    getOption: function (val, text, selected) {
        var sel = false;
        if (selected)
            $.each(selected, function () {
                if (this == val)
                    sel = true;
            });
        var s = sel > 0 ? 'selected="selected"' : '';
        var v = val ? val : "";
        return "<option " + s + " value='" + v + "'>" + text + "</option>";
    },
    serializeVal: function (val) {
        if (val) return val.toString(); return "";
    },
    necro: function (el, en) {
        var d = "disabled";
        if (!en) el.attr(d, true); else el.removeAttr(d);
    },
    setInputs: function (group, enable) {
        var s = "input, select, textarea";
        group.filter(s).each(function () { Srch.necro($(this), enable); });
        group.find(s).each(function () { Srch.necro($(this), enable); });
        if (enable) group.show(); else group.hide();
    },
    triggerChanger: function (val, to, form) {
        var sh = val.behavior == Srch.behavior.SHOWING;
        var current;
        $.each(val.values, function (index, v) {
            if (v.val == to) current = v.selector;
            else Srch.setInputs(form.find(v.selector), !sh);
        });
        if (current) Srch.setInputs(form.find(current), sh);
    },
    searchForm: function (selector, options) {
        var that = this;
        this.form = $(selector);
        this.langCode = this.form.data('lang');
        this.locationUrl = this.form.data('location');
        this.dictUrl = this.form.data('dicts');
        if (options.submiters)
            this.form.find(options.submiters).click(function () { that.form.submit(); });
        if (options.changers)
            $.each(options.changers, function (index, val) {
                that.form.find(val.selector).change(function () {
                    Srch.triggerChanger(val, $(this).val(), that.form);
                }).change();
            });
        this.form.find('select[data-load]').each(function () {
            var select = $(this);
            that.loadLocations(select, function (data) { that.fillSelect(select, data); });
        });
        this.form.find('select[data-dict]').each(function () {
            var select = $(this);
            that.loadDictionaries(select, function (data) { that.fillSelect(select, data); });
        });
    }
};
Srch.searchForm.prototype.loadLocations = function (control, callback, par) {
    var t = control ? control.data("load") : "";
    var p = par ? par : "";
    $.post(this.locationUrl, { types: t, parents: p }, function (data) { callback(data); });
}
Srch.searchForm.prototype.loadDictionaries = function (control, callback) {
    var t = control ? control.data("dict") : "";
    $.post(this.dictUrl, { type: t, lang: this.langCode }, function (data) { callback(data); });
}
Srch.searchForm.prototype.fillSelect = function (select, data) {
    var that = this;
    select.empty();
    var empty = select.data("empty");
    var start = select.data("start");
    var starts = start ? start.toString().split(",") : new Array();
    var binds = select.data("binds");
    if (empty) select.append(Srch.getOption("", empty));
    $.each(data, function (index, val) { select.append(Srch.getOption(val.Id, val.Name, starts)); });
    if (select.attr("data-binds")) {
        select.change(function () {
            var binder = $(this);
            var toBind = that.form.find('#' + binder.data("binds"));
            if (toBind.length) {
                that.loadLocations(null, function (data) {
                    toBind.each(function () {
                        that.fillSelect($(this), data);
                        $(this).change();
                    });
                }, Srch.serializeVal(binder.val()));
            }
        }).removeAttr("data-binds");
    }
    if (start) {
        select.removeAttr("data-start");
        select.change();
    }
}
