/********************************************************************************************
*		Purpose: 
*		Created By:	Wesley Barnes
*		Date Created: 08/24/2010
*		Update History:
*		
*********************************************************************************************/
$(document).ready(function(){
	
	//08/24/2010
	//hide powered by in the title.
	//if($('title'))
	//{
	//	var title = $('title').html();
	//	title = title.replace("- (Powered by CubeCart)", "");
	//	 $('title').html(title);
		//console.log("title: " + title);
	//}
	//08/25/2010 - WB:  Add handlers for product search
	/*
	if($('.as-selections').length)
	{
		//handler for providing search suggestions
		$('.as-selections').autoSuggest("http://www.186mph.com/search/",{ 	
			minChars: 2, 
			matchCase: false,
			startText: "Product Name or Number",
			retrieveLimit:"10",
			searchObjProps:"value,name",
			selectedItemProp:"name",
			selectionLimit:6,
			selectionAdded: function(elem,data){ 
				//elem.fadeTo("slow", 0.33); 
				//console.log('elem: ' + data.value);
				//$('#searchStr').val(data.value);
				$('#searchStr').val(data.value);
				$("#product-search-form").submit();
			}
		});
	}//if($('as-selections').length)
	*/
	
	$(function() {
		function log(message) {
			$("<div/>").text(message).prependTo("#log");
			$("#log").attr("scrollTop", 0);
		}
		
		//Bind the autocomplete event to the search box
		$(".as-selections").autocomplete({
			source: function(request, response) {
				
				$.ajax({
					url: "/search/",
					dataType: "json",
					data: {
						limit: 10,
						q: request.term
					},
					success: function(data) {
						
						response($.map(data, function(item) {
							return {
								//label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
								//value: item.name
								label : item.name,
								value : item.name
							}
						}))
					}
				})
				
			},
			width: -1,
			minLength: 2,
			select: function(event, ui) {
				var searchValue = ui.item ? ui.item.label : this.value
				this.value = searchValue;
				//console.log("selected: " + searchValue);
				$('#product-search-form').submit();
				//log(ui.item ? ("Selected: " + ui.item.label) : "Nothing selected, input was " + this.value);
			},
			open: function() {
				$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
			},
			close: function() {
				$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
			}
		});
	});
	

});
