(function () {
    var search_form = {};
    
    search_form.all_msg = 'Любая';
    
    search_form.modeli_dict = {}
    
    search_form.mark_change = function(e) {
        lib.combo_from_dict('#id_model', $(this).val(), probegauto.models, 'Модель');
        $('#id_is_foreign')[0].checked = false;
    };
    
    search_form.submit = function(e) {
        e.stopImmediatePropagation();
        e.stopPropagation();
        var year_from = $('#id_year_from').val();
        var price_from = parseInt($('#id_price_from').val());
        var price_to = parseInt($('#id_price_to').val());
    
        var new_location = '/';
        var akpp = $('#id_akpp')[0].checked;
        if (akpp) {
            new_location = '/akpp/';
        }
        if (year_from != '0000' || price_to > 0 || price_from > 0) {
            new_location = '/'+year_from+'/'+price_from+'-'+price_to+new_location;
        }
    
        var mark_option = $('#id_mark option:selected');
        var model_option = $('#id_model option:selected');
        if (mark_option.val() > 1) {
            if (model_option.val() > 1) {
                new_location = '/'+model_option.text().replace(' ','+')+new_location
            }
            new_location = '/'+mark_option.text().replace(' ','+')+new_location
        }
        if (!probegauto.vehicle_type_slug) {
            probegauto.vehicle_type_slug = probegauto.mark_types[mark_option.attr('value')];
        }
        var _foreign_part = '';
        if ($('#id_is_foreign')[0].checked && mark_option.val() == 1) {
            _foreign_part = '/inom';
        }
        new_location = '/' + probegauto.vehicle_type_slug + _foreign_part + new_location
        window.location.replace(new_location.toLowerCase());
    };
    
    search_form.set_marks = function(is_foreign) {
        var _select = $('#id_mark');
        _select.empty();
        _select.append(lib.opt('1', 'Марка'));
        var _items = [];
        if (!probegauto.vehicle_type_slug) {
            _items = probegauto.marks[1].concat(probegauto.marks[2],probegauto.marks[3]);
            if (is_foreign) {
                _items = $.grep(_items, function(row, inx){return row[2];});
            }
        } else {
            _items = probegauto.marks[lib.vtype[probegauto.vehicle_type_slug]];
            if (is_foreign) {
                _items = $.grep(_items, function(row, inx){return row[2];});
            }
        }
        $(_items).each(function (inx, item) {_select.append(lib.opt(item[0], item[1]));});
        lib.combo_from_dict('#id_model', $(this).val(), probegauto.models, 'Модель');
    };
    
    search_form.foreign_click = function(e) {
        search_form.set_marks(this.checked);
    };
    
    search_form.show_more_results = function(e) {
        e.preventDefault();
        probegauto.page += 1;
        var _url = probegauto.full_path+'page/'+probegauto.page+'/?top='+probegauto.top_lot;
        if (probegauto.order) {
            _url += '&order=' + probegauto.order;
        }
        $.ajax({
            url: _url,
            cache: false,
            success: function(data) {
                $('div.pg6_autos_top table tbody').append(data);
                document.probegauto_attach_favorites();
                $('a.send_sms, a.colorize, a.fasten').unbind().click(lib.show_dialog);
            },
            error: function() {
                alert('Ошибка при ajax запросе!');
            }
        });
        probegauto.remain = probegauto.remain - probegauto.search_rows;
        if (probegauto.remain < 0) {
            probegauto.remain = 0;
        }
        $('#id_remain_count').text(probegauto.remain);
        if (probegauto.page >= probegauto.num_pages) {
            $('#id_show_more_results').unbind();
            $('#id_show_more_results').remove();
        }
    };
    
    search_form.init = function () {
        $('#id_mark').change(search_form.mark_change);
        $('#submit-search').click(search_form.submit);
        $('#id_is_foreign').click(search_form.foreign_click);
        var _foreigns_checked = $('#id_is_foreign')[0].checked;
        if (_foreigns_checked && $('#id_mark option:selected').val() == 1) {
            search_form.set_marks(_foreigns_checked);
        }
        $('#id_show_more_results').click(search_form.show_more_results);
        $('#id_go_top').click(function(e) {
            e.preventDefault();
            $(window).scrollTop(0);
        });
        $('a.send_sms, a.colorize, a.fasten').unbind().click(lib.show_dialog);
    };
    
    $().ready(search_form.init);
}) ( );

