/* вывод товаров в модульных системах */

function moduleCategory(slug, title) {
$.ajax({
	type: "GET",
	url: "/category/" + slug + "?view=noframe",
	success: function(html){
		setTimeout( function(){ $("#category_items").html(html); }, 2000);
	}	
});
}

function fastSearch() {
$.getJSON("/published/SC/html/scripts/ajax_search.php",
{
	q: $("#searchstring").val(), // строка поиска
	format: "json" }, 
	function(data) {

	/* всё стираем и выдаем результаты */
	var str = $("#searchstring").val();
	$(".cpt_maincontent").html("<h1>Результаты поиска: " + str + "</h1>");
	document.title = "Результаты поиска: " + str;
	
	if (data.result == 0) {
		$(".cpt_maincontent").append("<div class='error_block'>По вашему запросу ничего не найдено.</div>");
	} else {
		$(".cpt_maincontent").append("<div id='cart_checkout_btn'>Мы нашли столько результатов: "+data.result+"</div>");	
	}
	
	$(".cpt_maincontent").append("<div class='product-row'>");
	
	/* открываем темплейт */
	
	var tpl = "<div class='product_brief_block'><div class='prdbrief_name'><a href='/product/{item.product}/'>{item.name}</a></div><div class='prdbrief_thumbnail'><table width='100%' cellpadding='0' cellspacing='0'><tr><th colspan='2' class='img-preview'><a href='/product/{item.product}/'><img src='{item.image}'></a></th></tr><tr><td width='50%' align='right'><b>Цена:</b></td><td width='50%'><span class='tPrice'>{item.price} руб.</span></td></tr></table><div class='prdbrief_add2cart'><div class='text_buy' rel='{item.product}'><span><a href='/product/{item.product}/'>купить</a></span></div></div></div></div>";
	
    $.each(data.items, function(i,item){
	
	if ( item.price > 0 ) {
		price_html = "";
	}
	
	if ( i%3 == 0 ) { // если делится на три
		$(".cpt_maincontent").append("</div><div class='product-row'>");
	}

	/* заменяем переменные */
	
	tplr =  tpl.replace(/{item.name}/g, item.name);
	tplr = tplr.replace(/{item.product}/g, item.product);
	tplr = tplr.replace(/{item.price}/g, item.price);
	tplr = tplr.replace(/{item.image}/g, item.image);
	
	$(".cpt_maincontent").append(tplr);
	
	/* 100 результатов более чем достаточно */
	
	if ( i == 100 ) return false; 
    });
	
	$(".cpt_maincontent").append("</div>");
});
}

/* функции автопоиска */

function liFormat (row, i, num) {
	var result = row[1] + row[0] + '<p class=qnt>' + row[2] + '</p>';
	return result;
}

function selectItem(li) {
	if ( !!li.extra ) var sValue = li.extra[1];
	else var sValue = li.selectValue;
	window.location = li.extra[3];
}

$(document).ready(function(){

/* автопоиск */

$("#searchstring").autocomplete("/published/SC/html/scripts/autocomplete.php", {
	delay:15,
	minChars:2,
	matchSubset:10,
	autoFill:false,
	matchContains:1,
	cacheLength:10,
	selectFirst:false,
	formatItem:liFormat,
	maxItemsToShow:12,
	onItemSelect:selectItem
});

/* поиск по категориям из шапки */

$(".ajax_search").click(function(){
	var str = $(this).attr("rel");
	var tip = $(this).attr("tip");
	$("#search_form #searchstring").val(str);
	$("#button_search").click();
	return false;
});

/* поиск */

$("#button_search").mouseover(function() {
	$(this).attr("id", "button_search_hover");
}).mouseleave(function() {
	$(this).attr("id", "button_search");
}).click(function() {
	fastSearch();
	return false;
});   

/* перехватываем сабмит формы поиска */

$("#search_form").submit(function() {
	fastSearch();
	return false;	
});

});
