$().ready(init);

var maxResults = 7;

function init()
{
	$.ajaxSetup({
		type: "POST"
		,contentType: "application/json; charset=utf-8"
		,dataType: "json"
	});

	$("#searchInput").autocomplete("/net/webservice/ProxyService.asmx/GetProductList", { 
		align: "right",
		cacheLength: 1,
		matchSubset: false,
		max: 7,
		xOffset: 3,
		delay: 500,
		width: 305,
		selectFirst: false,
		scrollHeight: 500,
		scroll: false,
		parse: parseData,
		formatItem: formatItem
	});

	$("#searchInput").result(function (event, item)
	{
	    if (item.LastItem)
	    {
	        location.href = item.Url_ProductPage + $("#searchInput").val();
	    }
	    else
	    {
	        location.href = item.Url_ProductPage;
	    }
	});
	
	
	$("#searchInput").focus(handleSearchFocus);
	$("#searchInput").blur(handleSearchBlur);
}

function parseData(data)
{
	var jsonData = jQuery.parseJSON(data.d) || jQuery.parseJSON(data);
	var rows = [];

	var searchUrl = $("#searchUrl").val();
	//var searchUrl = "http://tstweb01:8407";


	rows.push({ data: { NewSearchBanner: true, ModelName: "VIEW ALL RESULTS", Url_ProductPage: searchUrl + "?query=" + $("#searchInput").val()} })

	$.each(jsonData, function (index)
	{
	
		var name = this.ModelName;
		var thumb = this.Url_ProductThumbnail;
		var description = this.Description;
		var url = this.Url_ProductPage;
		
		/* rows.push({name: name, thumb: thumb, description: description, url: url}); */
//		if (index < maxResults)
			rows.push({ data: this, value:name, result:name });
	});



	//if (rows.length > 0)
	//{
	    //rows.push({data: {LastItem: true, ModelName: "VIEW ALL RESULTS", Url_ProductPage: "http://search.amx.com/search?restrict=&btnG=Search&ie=&site=AMX_collection&output=xml_no_dtd&client=AMX_collection&lr=&proxystylesheet=AMX_collection&oe=&q="}})
	//    rows.push({ data: { LastItem: true, ModelName: "VIEW ALL RESULTS", Url_ProductPage: searchUrl + "?query=" + name} })
	//}
	//else
	//{
	    //rows.push({data: {LastItem: true, ModelName: "MORE SEARCH RESULTS!", Url_ProductPage: "http://search.amx.com/search?restrict=&btnG=Search&ie=&site=AMX_collection&output=xml_no_dtd&client=AMX_collection&lr=&proxystylesheet=AMX_collection&oe=&q="}})
	//    rows.push({ data: { LastItem: true, ModelName: "MORE SEARCH RESULTS!", Url_ProductPage: searchUrl + "?query=" + name} })
	//}

    return rows;
}

function formatItem(row)
{
    if (row.NewSearchBanner)
    {
        var NewSearchText = "<div><img src='/imgs/header/282x58_AMXsearch.png' style='width:282px;' /></div> ";
        return NewSearchText;
    }

	if (row.LastItem)
	{
		return "<div id='ac_lastItem'>" + row.ModelName + "</div>";
	}
	else
	{
		var img = "<img src='" + row.Url_ProductThumbnail +"' class='ac_resultsImg'/>";
		var txt = "<div class='ac_resultsTxt'><span style='font-size: 12px;'>" + row.ModelName + "</span> <br/> <p style='font-size: 9px; line-height: 1.5em;'>" + row.Description + "</p></div>"
		return img + " " + txt;
	}
}


function handleSearchFocus()
{
	if ($("#searchInput").val() == $("#searchInput").attr("title"))
		$("#searchInput").val("");
}

function handleSearchBlur()
{
	if ($("#searchInput").val() == "")
		$("#searchInput").val($("#searchInput").attr("title"));
}
