
$(document).ready(function()
{
	function liFormat (main_text, row, i, num) 
	{
		var result = "<a href='/product/prod" + row[1] + ".html'>" + main_text + "</a>";

		return result;
	}

	function priceFormat (row) 
	{
		var result = "<span class='price'>" + row[2] + "</span> руб.";

		return result;
	}


	function imgFormat (row) 
	{
		var result = "<div class='img'><img src='" + row[3] + "' width='" + row[4] + "' height='" + row[5] + "' border='0'></div>";

		return result;
	}

	function closerFormat (dataLength, maxItemsToShow) 
	{
		var matchesStr = (dataLength == 1) ? " товар" : "";
		matchesStr = (dataLength > 1) && (dataLength < 5) ? " товара" : matchesStr;
		matchesStr = (dataLength > 4) ? " товаров" : matchesStr;

		if (dataLength > maxItemsToShow)
		{
			matchesStr = ", показано " + maxItemsToShow + matchesStr;
		}

		var result = "<table><tr><td>Найдено " + dataLength + matchesStr + "</td><td><a href='#'>Закрыть</a></td></tr></table>";

		return result;
	}

	function selectItem(li) 
	{
		if (li.extra[1]) window.location = "/product/prod" + li.extra[0] + ".html";;
	}

	$("#example").autocomplete("/service/autocomplete.php", 
	{
		width:576,
		delay:10,
		minChars:3,
		matchSubset:1,
		autoFill:false,
		matchContains:1,
		cacheLength:8,
		selectFirst:false,
		formatItem:liFormat,
		formatImage:imgFormat,
		formatCount:priceFormat,
		maxItemsToShow:8,
		onItemSelect:selectItem,
		resultsClass:"results",
		inputClass:"ac_input",
		loadingClass:"ac_loading",
		overClass:"over",
		overMouseClass:"over_mouse",
		//priceOverClass:"price_a",
		priceTag:"span",
		formatCloser:closerFormat
	}); 
});
