var FilterWidget = function() {

    this.observers = [];

    this.name = 'FilterWidget';
    this.node = $("#searching-block");

    this.listen = function(e, obj) {
        if (obj.type == 'search-widget') {
            this.update();
        }
    }

    this.update = function() {
        this.notify('change');
    }
}

FilterWidget.prototype = new Observable();

var ExtendedFieldsWidget = function() {

    this.type = 'search-widget';
    this.observers = [];

    this.init = function() {
        this.name = 'ExtendedFieldsWidget';
        this.link = jQuery('#extended_fields_link');
        this.fields = jQuery('#extended_fields');
        this.mode = jQuery('#reo-filter-mode');
        this.link.css('display', 'block');
        if (this.mode.attr('value') == 'simple' || !this.mode.attr('value')) {
            this.hide();
        }
        else {
            this.show();
        }
    }

    this.listen = function(e, obj) {
        if (obj.name == 'ReoTypeFieldWidget' && e == 'change') {
            this.updateFields(obj.reoFields);
        }
    }

    this.updateFields = function(fields) {
        var $fieldBlocks = this.fields.find('[id^=extended_fields-]');
        if (fields.length == 0) {
            $fieldBlocks.show();
            return;
        }
        for (var i=0; i<$fieldBlocks.length; i++) {
            var display = false;
            var $field = $($fieldBlocks[i]);
            var fieldName = $field.attr('id').substring(16); // extract "name" from "extended_fields-[name]"
            for (var j=0; j<fields.length; j++) {
                if (fieldName == fields[j]) {
                    display = true;
                    break;
                }
            }
            display ? $field.show() : $field.hide();
        }
    }

    this.clear_ext_fields = function(){
        jQuery('#id_sea_view').val('');
        jQuery('#id_distance_from_sea').val('');
        jQuery('#id_distance_from_sea').val('');
        jQuery('#id_distance_from_center').val('');
        jQuery('#id_total_area_from').val('');
        jQuery('#id_total_area_to').val('');
        jQuery('#id_living_area_from').val('');
        jQuery('#id_living_area_to').val('');
        jQuery('#id_flat_livin_rooms').val('');
    }

    this.toggle = function() {
        if (this.is_visible) {
            this.hide();
        }
        else {
            this.show();
        }
    }

    this.onToggle = function() {
        function wrapper(obj) {
            function wrapped() {
                obj.notify('toggle');
            }
            return wrapped;
        }
        return wrapper(this);
    }

    this.show = function() {
        this.link.html(widget_locale.basic);
        this.fields.slideDown('fast', this.onToggle());
        this.mode.attr('value', 'extended');
        this.is_visible = true;
    }

    this.hide = function() {
        this.link.html(widget_locale.extended);
        this.fields.slideUp('fast', this.onToggle());
        this.mode.attr('value', 'simple');

        this.is_visible = false;

        this.clear_ext_fields();
    }
}

ExtendedFieldsWidget.prototype = new Observable();

var TenderTypeFieldWidget = function() {
    this.type = 'search-widget';
    this.observers = [];
    this.name = 'TenderTypeFieldWidget';
    this.value = null;

    this.init = function() {
        this.field = jQuery('#id_type');
        this.value = parseInt(this.field.attr('value'));
    }

    this.update = function(value) {
        this.value = value;
        this.notify('change');
    }
}

TenderTypeFieldWidget.prototype = new Observable();

var BaseChoiceRelatedWidget = function() {

    this.type = 'search-widget';

    this.listen = function(e, obj) {
        if (e == 'change') {
            if (obj.name == 'TenderTypeFieldWidget') {
                this.update(obj);
            }
        }
    }

    this.update = function(obj) {
        var show = false;
        for (var i=0; i<this.allowed_values.length; i++) {
            if (parseInt(this.allowed_values[i]) === parseInt(obj.value)) {
                show = true;
            }
        }
        show ? this.show() : this.hide();
    }

    this.onToggle = function() {
        function wrapper(obj) {
            function wrapped() {
                obj.notify('toggle');
            }
            return wrapped;
        }
        return wrapper(this);
    }

    this.show = function() {
        this.container.slideDown('fast', this.onToggle());
        this.field.attr('disabled', false);
    }

    this.hide = function() {
        this.field.attr('disabled', true);
        this.container.slideUp('fast', this.onToggle());
    }

}

BaseChoiceRelatedWidget.prototype = new Observable();

var ReoDescriptionFieldWidget = function(allowed_values) {
    this.observers = [];
    this.name = 'ReoDescriptionFieldWidget';
    this.allowed_values = allowed_values;

    this.init = function() {
        this.container = jQuery('#form-field-wrapper-reo_description');
        this.field = jQuery('#id_reo_description');
    }

}

ReoDescriptionFieldWidget.prototype = new BaseChoiceRelatedWidget();

var RentPricePeriodFieldsWidget = function(allowed_values) {
    this.observers = [];
    this.name = 'RentPricePeriodFieldsWidget';
    this.allowed_values = allowed_values;

    this.init = function() {
        this.container = jQuery('#rent_dates_fields');
        this.start_field = jQuery('#id_actual_start_at');
        this.end_field = jQuery('#id_actual_end_at');
    }

    this.show = function() {
      this.start_field.attr('disabled', false);
      this.end_field.attr('disabled', false);
      this.container.slideDown('fast', this.onToggle());
  }

    this.hide = function() {
        this.start_field.attr('disabled', true);
        this.end_field.attr('disabled', true);
        this.container.slideUp('fast', this.onToggle());
    }
}

RentPricePeriodFieldsWidget.prototype = new BaseChoiceRelatedWidget();

var PricePeriodTypeFieldWidget = function(allowed_values) {
    this.observers = [];
    this.name = 'PricePeriodTypeFieldWidget';
    this.allowed_values = allowed_values;

    this.init = function() {
        this.container = jQuery('#form-field-wrapper-price_period');
        this.field = jQuery('#id_price_period');
    }

}

PricePeriodTypeFieldWidget.prototype = new BaseChoiceRelatedWidget();

var ReoReTypeFieldWidget = function() {
    this.type = 'search-widget';
    this.name = 'ReoReTypeFieldWidget';
    this.observers = [];

    this.init = function() {
        this.field = jQuery('#id_reo__re_type');
        this.value = this.field.attr('value');
    }

    this.update = function(value) {
        this.value = value;
        this.notify('change');
    }
}

ReoReTypeFieldWidget.prototype = new Observable();

var ReoTypeFieldWidget = function(data) {
    this.type = 'search-widget';
    this.name = 'ReoTypeFieldWidget';
    this.observers = [];
    this.data = data;
    this.parent = null;
    this.reoFields = [];

    this.init = function() {
        this.field = jQuery('#id_reo__type');
        this.field.change(this.onchange());
    }

    this.listen = function(e, obj) {
        if (e == 'change') {
            if (obj.name == 'ReoReTypeFieldWidget') {
                this.parent = obj.value;
                this.update();
            }
        }
    }

    this.onchange = function() {
        function wrapper(obj) {
            function wrapped(){
                obj.setReoFields();
                obj.notify('change');
            }
            return wrapped;
        }
        return wrapper(this);
    }

    this.setReoFields = function() {
        this.reoFields = [];
        var value = this.field.val();
        for (var i = 0; i < this.data.length; i++) {
            if (parseInt(data[i][1]) == parseInt(value)) {
                this.reoFields = data[i][3];
            }
        }
    }

    this.update = function() {
        this.set_options();
        this.field.change();
    }

    this.set_options = function() {
        var current = this.field.attr('value');
        var html = this.make_option('', widget_locale.undefined_text, true ? current == '' : false);
        for (var i=0; i<this.data.length; i++) {
            if (parseInt(this.data[i][0]) == parseInt(this.parent) || !this.parent) {
                html += this.make_option(this.data[i][1], this.data[i][2], true ? current == this.data[i][1] : false); // + '\n';
            }
        }
        this.field.html(html);
    }

    this.make_option = function(value, name, selected) {
        return '<option value="' + value + '"' + (selected ? ' selected="selected"' : '') + '>' + name + '</option>';
    }
}

ReoTypeFieldWidget.prototype = new Observable();

var SearchTypeSwitchWidget = function() {
    this.type = 'search-widget';
    this.observers = [];

    this.listen = function(){};

    this.switchTab = function() {
        function wrapper(obj) {
            function wrapped() {
                var $tag = $(this);
                var name = $tag.val();
                $('.search-tab').each(function(){
                    var tab = $(this);
                    if (tab.attr('id').indexOf(name) != -1) {
                        tab.show();
                    }
                    else {
                        tab.hide();
                    }
                });
                obj.notify('change');
                document.cookie = 'search_type=' + name + ';path=/';
            }
            return wrapped;
        }
        return wrapper(this);
    }
}

SearchTypeSwitchWidget.prototype = new Observable();
