$(document).ready(function(){

	$('.buy').text('VISIT SITE +');
	$(".buy").mouseover(function () {
		var text = $(this).attr('id').split('_');
		$(this).replaceWith('<a class="buy" href="/products/go/?id=' + text[0] +  '">VISIT SITE +</a>');
	});

	$("#s").autocomplete({
		minLength: 3,
		select: function(event, ui) {
			$("#s").val(ui.item.label);
			$("#searchform").submit();
		},
		source: function (request, response) {
			$.ajax({
				url: "/products/autocomplete",
				data: {
					search: request.term,
				},
				dataType: "json",
				success: function(data) {
					response($.map(data.products, function(el, index) {
						return {
							value: el.title,
							title: el.title,
							local_image: el.local_image
						};
					}));
				}
			});
		}
	}).data("autocomplete")._renderItem = function (ul, item) {
		return $("<li />")
			.data("item.autocomplete", item)
			.append("<a><img width='25' src='/thumbs/" + item.local_image + "' /> " + item.title + "</a>")
			.appendTo(ul);
	};

});

