
$().ready(function() {

	
	var default_search = 'Click & start typing and find a recipe!';
	
	$("#recipeAutoFill").focus(function(){
		if ($(this).val() == default_search) {
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '') {
			$(this).val(default_search);
		}
	}).trigger('blur');
	 
	function findValueCallback(event, data, formatted) {
		if(data == undefined){
			//alert("searching:" + $("#recipeAutoFill").val());
			window.location = "/preparation/recipe/" + $("#recipeAutoFill").val();
			
		}else{
			var newdata = data.toString();
			data = newdata.substr(newdata.lastIndexOf(",")+1);
			//alert("redirecting:" + data);
			window.location = "/preparation/recipe/" + data;
		}
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	
	$("#recipeAutoFillForm").submit(function(event){						 
			
			window.location = "/preparation/recipe/" + $("#recipeAutoFill").val().replace(" ",":");		
			return false;
	});
			
	
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		//return row[1];
		//alert(row[0] + " - " + row[1])
		return row[0].replace(/(<.+?>)/gi, '');
	}
	
	
	$("#recipeAutoFill").autocomplete("/preparation/recipe_cards/", {
		max: 5,
		width: 576,
		scrollHeight: 500,
		selectFirst: false
	});		
	
	$(":text, textarea").result(findValueCallback).next().click(function() {
		$(this).prev().search();
	});
	$("#recipeAutoFill").result(function(event, data, formatted) {
		if (data)
			$(this).parent().next().find("input").val(data[1]);
	});	});
 

 
 
function changeToMonths(){
	$("#suggest1")
		// clear existing data
		.val("")
		// change the local data to months
		.setOptions({data: months})
		// get the label tag
		.prev()
		// update the label tag
		.text("Month (local):");
}
