Autocomplete.prototype.suggest = function() {
	if (this.suggestions.length === 0) {
		this.hide();
		return;
	}
	var content = [];
	var re = new RegExp('\\b' + this.currentValue.match(/\w+/g).join('|\\b'), 'gi');
	this.suggestions.each(function(value, i) {
		content.push((this.selectedIndex === i ? '<div class="selected"' : '<div'), ' title="', value[0], '" onclick="Autocomplete.instances[', this.instanceId, '].select(', i, ');" onmouseover="Autocomplete.instances[', this.instanceId, '].activate(', i, ');">', Autocomplete.highlight(value[0], re), '<div class="hits">' + (i == 0 ? 'ilość ' : '') + value[1] + '</div></div>');
	} .bind(this));
	this.enabled = true;
	this.container.update(content.join('')).show();
};

Autocomplete.prototype.select = function(i) {
	var selectedValue = this.suggestions[i][0];
	if (selectedValue) {
		this.el.value = selectedValue;
		if (this.options.autoSubmit && this.el.form) {
			this.el.form.submit();
		}
		this.ignoreValueChange = true;
		this.hide();
		this.onSelect(i);
	}
};

Autocomplete.prototype.adjustScroll = function(i) {
	var container = this.container;
	var activeItem = this.activate(i);
	var offsetTop = activeItem.offsetTop;
	var upperBound = container.scrollTop;
	var lowerBound = upperBound + this.options.maxHeight - 25;
	if (offsetTop < upperBound) {
		container.scrollTop = offsetTop;
	} else if (offsetTop > lowerBound) {
		container.scrollTop = offsetTop - this.options.maxHeight + 25;
	}
	this.el.value = this.suggestions[i][0];
};
	
Autocomplete.attach = function(id, serviceUrl, width, container) {
	new Autocomplete(id, {
		serviceUrl: serviceUrl,
		minChars:1,
		maxHeight:400,
		width:width,
		container: container,
		onSelect: new Function("a", "b", "$('"+id+"').value = a[0]")
	});
}

