/*
 * The starting part of all "ViartShop Russian Edition" JavaScript possibilities.
 * Developed by Makarychev Yurii (http://makarychev.ru/) aka iSlayter (http://islayter.com/)
 */
$().ready(function() {
	/*
	 * Instantiate autocomplete
	 */
     initAutoComplete();
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 
/*                The next functions used for make all of this stuff to work                       */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Auto complete function.
 */
function initAutoComplete() {
	$("#productSearchTerm").blur(function() { sform(0, 'productSearchTerm', 'autosuggest'); });
	$("#productSearchTerm").focus(function() { sform(1, 'productSearchTerm', 'autosuggest'); });
    $("#productSearchTerm").autocomplete('products_search_autocomplete.php', {
        minChars: 2,
        width: 411,
        extraParams: {
            categoryId: function() {
                return $("#productSearchCategory").val();
            }
        },
        formatItem: function(formatItem) {
            return formatItem[1];
        },
        formatResult: function(formatResult) {
            return formatResult[1];
        }
    }).result(function(event, resultItem) {
        $('#productSearchSubmitBtn').attr('disabled', 'disabled')
        location.href = 'product_details.php?item_id=' + resultItem[0];
    });
    	    
    $("#productSearchCategory").click(function(){ $('.ac_results').css('display','none'); $("#productSearchTerm").val(''); });
    $("#productSearchCategory").change(function(){ $('.ac_results').css('display','none'); });
}

/*
 * Function for changing text at input fields.
 * Developed by Makarychev Yurii (http://makarychev.ru/) aka iSlayter (http://islayter.com/)
 */
function sform(show, id, keyword) {
	el = $('#' + id);
	if (show == 1 && el.val() == keyword) {       
		el.val('');
	} else if (el.val() == '') {
		el.val(keyword);
	}
}