function initSearchField() {
    if (!Array.prototype.filter) {
        Array.prototype.filter = function (fun) {
            "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 thisArg = arguments.length >= 2 ? arguments[1] : void 0;
            for (var i = 0; i < len; i++) {
                if (i in t) {
                    var val = t[i];
                    if (fun.call(thisArg, val, i, t))
                        res.push(val);
                }
            }

            return res;
        };
    }

    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (elt) {
            var len = this.length >>> 0;

            var from = Number(arguments[1]) || 0;
            from = (from < 0)
                 ? Math.ceil(from)
                 : Math.floor(from);
            if (from < 0)
                from += len;

            for (; from < len; from++) {
                if (from in this &&
                    this[from] === elt)
                    return from;
            }
            return -1;
        };
    }
    
    Array.prototype.unique = function (a) {
        return function () { return this.filter(a) }
    }(function (a, b, c) { return c.indexOf(a, b + 1) < 0 });
    var renderedItems = [];
    var searchInput = $("div.searchField input");
    var noResultsLabel = "Нет результатов, удовлетворяющих критерию поиска";
    if (isUrlVar("typedSubService")) {
        searchInput.autocomplete({
            source: function (request, response) {
                var results = getRenderedItems($("div.searchField input[type=text]").val());
                if (!results.length) {
                    results = [noResultsLabel];
                }
                response(results);
            },
            select: function (event, ui) { onSelect(event, ui); },
            focus: function (event, ui) { onFocus(event, ui); }
        });
    } else {
        $("div.searchField input").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "../Service/GetSearchResult",
                    dataType: "json",
                    data: { filter: $("input.ui-autocomplete-input").val() },
                    success: function (data) {
                        if (!data.length) {
                            data = [noResultsLabel];
                        }
                        response(data);
                    },
                    error: function (msg) {
                    }
                });
            },
            select: function (event, ui) { onSelect(event, ui); },
            focus: function (event, ui) { onFocus(event, ui); }
        });
    }
    function onFocus(event, ui) {
        if (ui.item.label === noResultsLabel) {
            event.preventDefault();
        }
    }

    function onSelect(event, ui) {
        if (ui.item.label === noResultsLabel) {
            event.preventDefault();
        }
    }

    function getRenderedItems(filter) {

        var items = getItems();
        var searchedItems = [];
        for (var i = 0; i < items.length; i++) {
            if (items[i].toLowerCase().indexOf(filter.toLowerCase()) != -1) {
                searchedItems.push(items[i])
            }
        }
        return searchedItems.unique();
    }
    function getItems() {
        if (renderedItems.length == 0) {
            $('div.linkArticle a').each(function () {
                renderedItems.push($(this).text());
            });
        }
        return renderedItems;
    }
}
function isUrlVar(name) {
    var hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        if (hash[0] == name) {
            return true;
        }
        else return false;
    }
}

function clearSearchField() {
    $('#searchField').val('');
    $('ul.ui-autocomplete.ui-widget-content').hide();
}