$().ready(function() {

	function log(event, data, formatted) {
		$("<li>").html( !data ? "Op&ccedil;&atilde;o inv&aacute;lida!" : "Selecionado: " + formatted).appendTo("#result");
	}
	
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	} 	  
	
	$("#singleBirdRemote").autocomplete("search.php", {
		minChars: 3,
		width: 200,
		selectFirst: true,
		scroll: true,
		scrollHeight: 300    
	});    

	$("#imageSearch").autocomplete("images.php", {
		width: 320,
		max: 4,
		highlight: false,
		scroll: true,
		scrollHeight: 300, 		
    //autoFill: true,
    autoFill: false, 
		formatItem: function(data, i, n, value) {
			return "<img src='images/" + value + "'/> " + value.split(".")[0]; 
		},
		formatResult: function(data, value) {
			return value.split(".")[0];
		}
	}); 
	//preenche a area Result com o dado selecionado
	$(":text, textarea").result(log).next().click(function() {
		$(this).prev().search();
	});
	$("#singleBirdRemote").result(function(event, data, formatted) { //preenche o Hidden input    
		if (data)
			$(this).parent().next().find("input").val(data[1]);
	});

	
	$("#clear").click(function() {   //o botao para remover os autocompletes
		$(":input").unautocomplete();
	});
});

function changeOptions(){
	var max = parseInt(window.prompt('Please type number of items to display:', jQuery.Autocompleter.defaults.max));
	if (max > 0) {
		$("#suggest1").setOptions({
			max: max
		});
	}
}


