$().ready(function() {
	var cur_game = $("#c_name").attr("gamehost");

	$("#c_name").keyup(function(){
		var $field = $(this);
		$field.val($field.val().replace(/[^a-zA-Z0-9\-\(\)\s\,]/g,""));

		return false;
	});

	$("#c_name").autocomplete(cur_game + "/car-autocomplete/",{
		width: 260,
		selectFirst: false,
		max: 100,
		minChars: 2,
		delay: 400,
		cacheLength: 10,
		// IE6, the POS that it is, didn't like floats & CSS, so I was forced into using tables
		formatItem: function(data, i, total) {
/*
			var ret = "<table width=\"92%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td colspan=\"2\">" + data[0] + "</td></tr><tr><td width=\"50%\">Year: "+ data[1] + "</td><td width=\"50%\" class=\"r\">";
			

*/
			var ret = data[0] + " (" + data[1] + ")<br>";
			
			if(data[2] > 1)
				ret += data[2] + " Setups";
			else if(data[2] == 1)
				ret += data[2] + " Setup";	
			else
				ret += 	"No Setups";
			
			ret += "</td></tr></table>";
			
			return ret;
		},
		// blank out the select since we're redirecting the user
		formatResult: function(data) {
			return " ";
		}
	});

	// redirect user to setups list if setups exist, or to the car info page if not
	$("#c_name").result(function(event,data,formatted) {
		var url = "";
		
		if(data[2] > 0)
			url = cur_game + "/setup-list/c_cid::" + data[3] + "/";
		else {
			url = cur_game + "/car-view/c_cid::" + data[3] + "/";
		}

		window.location = url;
		
		return false;
	});
});