// JavaScript Document

$(document).ready(function(){

	function sell_format_item(item, i, total)
	{
		if (item.last)
		{
			return 'and ' + item.total + ' more...';
		}
		else
		{
			if (item.image !== '' && item.image != false)
			{
				//product_string = '<div style="float:left;"><img src="' + item.image +'" width="30" height="30" style="margin-right:5px;" /></div>' + item.name;
				/*product_string = '<div class="row odd"><img src="'+base_url+'public/green/images/thumb-mobile.jpg" alt="" border="0" />' +
                                 '<h3><a href="#">'+item.name+'</a></h3>' +
                                 '(Cell Phones)<a href="#"><img src="'+base_url+'public/green/images/btn-sell-now.png" alt="" border="0" /></a></div>';*/
                if( item.name != 'Were sorry, but your search returned no results.')
                {
                    product_string = '<img src="'+item.image+'" width="30" height="30" alt="" border="0" />'+
                                     '<h3><a href="'+item.url+'">'+item.name+'</a></h3>'+
                                     '<a href="'+item.url+'"><img src="'+base_url+'public/green/images/btn-sell-now.png" alt="" border="0" /></a>';
                }
                else
                {
                    product_string = '<img src="'+item.image+'" width="30" height="30" alt="" border="0" />'+
                                     '<a href="'+item.url+'" style="font-weight:bold;text-decoration:none;color:#5086C6;font-size:13px;">'+item.name+'</a>';
                }
				
				
				if ($.trim(item.category) != '')
				{
					product_string += ' <span> - (' + item.category + ')</span>';
				}
				
				return product_string;
			}
			else
			{
				product_string = item.name;
				
				if ($.trim(item.category) != '')
				{
					product_string += '<span> - (' + item.category + ')</span>';
				}
				
				return product_string;	
			}
		}		
	}
	
	function sell_parse_response(data)
	{
		return $.map(data, function(row) {
					return {
							data: row,
							value: row.name,
							result: row.name
							}
			   });
	}
	
	function sell_result_callback(e, item) 
	{
		$('#sell_search_upc').val('');
		
		if (item.last)
		{			
			$('#frm_sell_search').submit();
		}
		else
		{
			location.href = item.url;
		}
	}
	
	function sell_upc_result_callback(e, item) 
	{
		$('#sell_search_query').val('');
		
		if (item.last)
		{
			$('#frm_sell_search').submit();
		}
		else
		{
			$('#sell_search_upc').val(item.upcs);
			location.href = item.url;
		}
	}	
	
	front_search_options = {
			width: 300,
			selectFirst: false,
			resultsClass: "results",
			rowEvenClass: 'row even',
			rowOddClass: 'row odd',
			max: 13,
			cacheLength: 0,
			minChars: 1,
			scroll: false,
			dataType: "json",
			parse: sell_parse_response,
			formatItem: sell_format_item,
			loadingClass: 'ac_loading_front'
	};
	
	$('#category_id').change(function(){
		update_url();
	});
	
	//search_url_sell = site_url('catalogsearch/autocomplete_sell/sell/' + $('#category_id').val());
	//search_url_upc = site_url('catalogsearch/autocomplete_sell/upc/' + $('#category_id').val());
	
	search_url_sell = site_url('catalogsearch/autocomplete');
	
	$("#search_query").autocomplete(search_url_sell, front_search_options).result(sell_result_callback);
	//$("#sell_search_upc").autocomplete(search_url_upc, front_search_options).result(sell_upc_result_callback);
});
