// initialise plugins
jQuery(function () {
    jQuery('ul.sf-menu').superfish({ 'delay': 10, 'speed': 0 });
});

//(function ($) {
//    $.widget("ui.combobox", {
//        _create: function () {
//            var self = this,
//					select = this.element.hide(),
//					selected = select.children(":selected"),
//					value = selected.val() ? selected.text() : "";
//            //var input = this.input = $("<input>").addClass("ui-widget ui-widget-content ui-corner-left");
//            var input = this.input = $("<input>")
//					.insertAfter(select)
//					.val(value)
//					.autocomplete({
//					    delay: 0,
//					    minLength: 0,
//					    source: function (request, response) {
//					        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
//					        response(select.children("option").map(function () {
//					            var text = $(this).text();
//					            if (this.value && (!request.term || matcher.test(text)))
//					                return {
//					                    label: text.replace(
//											new RegExp(
//												"(?![^&;]+;)(?!<[^<>]*)(" +
//												$.ui.autocomplete.escapeRegex(request.term) +
//												")(?![^<>]*>)(?![^&;]+;)", "gi"
//											), "<strong>$1</strong>"),
//					                    value: text,
//					                    option: this
//					                };
//					        }));
//					    },
//					    select: function (event, ui) {
//					        ui.item.option.selected = true;
//					        self._trigger("selected", event, {
//					            item: ui.item.option
//					        });
//					    },
//					    change: function (event, ui) {
//					        if (!ui.item) {
//					            var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
//									valid = false;
//					            select.children("option").each(function () {
//					                if ($(this).text().match(matcher)) {
//					                    this.selected = valid = true;
//					                    return false;
//					                }
//					            });
//					            if (!valid) {
//					                // remove invalid value, as it didn't match anything
//					                $(this).val("");
//					                select.val("");
//					                input.data("autocomplete").term = "";
//					                return false;
//					            }
//					        }
//					    }
//					})
//					.addClass("ui-widget ui-widget-content ui-corner-left");

//            input.data("autocomplete")._renderItem = function (ul, item) {
//                return $("<li></li>")
//						.data("item.autocomplete", item)
//						.append("<a>" + item.label + "</a>")
//						.appendTo(ul);
//            };

//            this.button = $("<button type='button'>&nbsp;</button>")
//					.attr("tabIndex", -1)
//					.attr("title", "Show All Items")
//					.insertAfter(input)
//					.button({
//					    icons: {
//					        primary: "ui-icon-triangle-1-s"
//					    },
//					    text: false
//					})
//					.removeClass("ui-corner-all")
//					.addClass("ui-corner-right ui-button-icon")
//					.click(function () {
//					    // close if already visible
//					    if (input.autocomplete("widget").is(":visible")) {
//					        input.autocomplete("close");
//					        return;
//					    }

//					    // work around a bug (likely same cause as #5265)
//					    $(this).blur();

//					    // pass empty string as value to search for, displaying all results
//					    input.autocomplete("search", "");
//					    input.focus();
//					});
//        },

//        destroy: function () {
//            this.input.remove();
//            this.button.remove();
//            this.element.show();
//            $.Widget.prototype.destroy.call(this);
//        }
//    });
//})(jQuery);

//$(function () {
//    $("select").combobox();
//    $("html").css('margin', '0');

//});

$(document).ready(function () {
    $('#svernut').click(function () {
        $('#podbor-content').animate(
			{ 'height': 'toggle' }
		);
        if ($(this).attr('class') == 'sv') {
            $(this).attr('class', 'rsv');
            $(this).html('<div>......</div>');
            $(this).html('<div>Развернуть</div>');
        } else {
            $(this).attr('class', 'sv');
            $(this).html('<div>......</div>');
            $(this).html('<div>Cвернуть</div>');
        }
    });
    $('.history-back').click(function () {
        history.back(1);
    });

    $('#loginpopup').click(function () {
        $('#login-form').fadeToggle(150);
        return false;
    });

    $('.radio-item label').click(function () {
        $(this).children().attr('checked', 'checked');
        $(this).children().change();
        return false;
    });

    $('input.form-radio').each(function () {
        $(this).change(function (msg) {
            $('input.form-radio:[name=' + $(this).attr('name') + ']').parent().removeClass('choise');
            $(this).parent().addClass('choise');
        });
    });

    var autohar = new Array();
    var widthauto = "100%";
    //пробегаем все селекты и применяем к ним селектменю.
    $("select").each(function () {
        menuW = $(this).width();
        autohar[$(this).attr('name')] = $(this).selectmenu({
            width: widthauto,
            menuWidth: menuW + 'px',
            open: function () {
                var butm = $(this).next().attr('id').replace('-button', '');
                $('#' + butm + '-menu').css({ 'width': $(this).next().width() + 'px' });
            }
        });
    });
});

function fillAutoBrends(model) {
    $.ajax({
        type: "POST",
        url: "/Search/GetAutoBrends",
        success: function (result) {
            var options = '';
            options += '<option value="-1" selected>-----</option>';
            $.each(result, function (id, option) {
                if (model != null && model.AutoBrendId == option.value) {
                    options += '<option value="' + option.value + '" selected="selected">' + option.name + '</option>';
                }
                else {
                    options += '<option value="' + option.value + '">' + option.name + '</option>';
                }
            });
            $('select#autobrendid').html(options).selectmenu();
            $('select#automodelid').html(options).selectmenu('disable');
            $('select#autoyearid').html(options).selectmenu('disable');
            $('select#automodid').html(options).selectmenu('disable');
            $('#search_auto_btn').attr("disabled", "disabled");
            fillAutoModels(event, options, model);
        },
        error: function (req, status, error) {
        }
    });
}

function fillAutoModels(event, options, model) {
    $.ajax({
        type: "POST",
        url: "/Search/GetAutoModels",
        data: "id=" + $('select#autobrendid').val(),
        success: function (result) {
            var options = '';
            options += '<option value="-1" selected>-----</option>';
            $.each(result, function (id, option) {
                if (model != null && model.AutoModelId == option.value) {
                    options += '<option value="' + option.value + '" selected="selected">' + option.name + '</option>';
                } else {
                    options += '<option value="' + option.value + '">' + option.name + '</option>';
                }
            });
            if ($('select#autobrendid').val() == "-1") {
                $('select#automodelid').html(options).selectmenu('disable');
            } else {
                $('select#automodelid').html(options).selectmenu();
            }
            $('select#autoyearid').html(options).selectmenu('disable');
            $('select#automodid').html(options).selectmenu('disable');
            $('#search_auto_btn').attr("disabled", "disabled");
            fillAutoYears(event, options, model);
        },
        error: function (req, status, error) {
        }
    });
}

function fillAutoYears(event, options, model) {
    $.ajax({
        type: "POST",
        url: "/Search/GetAutoYears",
        data: "id=" + $('select#automodelid').val(),
        success: function (result) {
            var options = '';
            options += '<option value="-1" selected>-----</option>';
            $.each(result, function (id, option) {
                if (model != null && model.AutoYearId == option.value) {
                    options += '<option value="' + option.value + '" selected="selected">' + option.name + '</option>';
                } else {
                    options += '<option value="' + option.value + '">' + option.name + '</option>';
                }
            });
            if ($('select#automodelid').val() == "-1") {
                $('select#autoyearid').html(options).selectmenu('disable');
            } else {
                $('select#autoyearid').html(options).selectmenu();
            }
            $('select#automodid').html(options).selectmenu('disable');
            $('#search_auto_btn').attr("disabled", "disabled");
            fillAutoMods(event, options, model);
        },
        error: function (req, status, error) {
        }
    });
}

function fillAutoMods(event, options, model) {
    $.ajax({
        type: "POST",
        url: "/Search/GetAutoMods",
        data: "id=" + $('select#autoyearid').val(),
        success: function (result) {
            var options = '';
            options += '<option value="-1" selected>-----</option>';
            $('#search_auto_btn').attr("disabled", "disabled");
            $.each(result, function (id, option) {
                if (model != null && model.AutoModId == option.value) {
                    options += '<option value="' + option.value + '" selected="selected">' + option.name + '</option>';
                    $('#search_auto_btn').removeAttr('disabled');
                } else {
                    options += '<option value="' + option.value + '">' + option.name + '</option>';
                }
            });
            if ($('select#autoyearid').val() == "-1") {
                $('select#automodid').html(options).selectmenu('disable');
            } else {
                $('select#automodid').html(options).selectmenu();
            }
        },
        error: function (req, status, error) {
        }
    });
}
