function initResearch() {
	$("#palavraBusca").keyup(function(e){
		
		var isNovaBusca = this.value.length > 3
		
		if((this.value.length == 0 && $('#resultadoBusca').css('display')!='none') || getKey(e) == 27) {
			$("#resultadoBusca").hide("slow")
			buscaShowCloseButton().hide()
		}	

		if(!isCharNum(e)) return;

		if(isNovaBusca) {
		   $("#imgLoading").show()

			var text = $.ajax({
			   type: "GET",
			   url: "/ajax/ControllerBuscaProduto.ajax.php/search",
			   data: "palavra="+this.value,

			   complete:function(request, settings){
					$("#resultadoBusca").show("slow")
					$("#conteudoResultadoBusca").html(request.responseText)
					buscaShowCloseButton()
			   }
			})
		} else {
			$("#resultadoBusca").hide("slow")
			buscaShowCloseButton().hide()
		}
	})		
}

function buscaShowCloseButton() {
	$("#imgLoading").hide()
	$("#imgClose").show()
	$("#imgClose").click(function(){
		$("#palavraBusca").val('');
		$("#resultadoBusca").hide()
		$(this).hide()
	})
	return $("#imgClose")
}	

function getKey (event) {
	return event ? (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode)) : null;
}

function isCharNum(event) {
	var key = getKey(event)
	if (key==13 || key == 44) 
		return true

	if (/[0-9A-Za-z]/.test(String.fromCharCode(key))) 
		return true

	return false
}