$(function() {
    var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');
    $('#generalSearchForm input[name=q]').autocomplete({
        serviceUrl: '/tovary/',
        minChars: 2,
        maxHeight: 200,
        width: 389,
        zIndex: 20,
        deferRequestBy: 300,
        params: { action: 'autocomplete'},
        onSelect: function(data, value) { $('#generalSearchForm').submit(); },
        fnFormatResult: function(value, data, currentValue) {
            var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
            pattern = pattern.replace(/\s/gi, '|');
            return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
        }
    });
});

