var indiceElementoSelecionado = -1;
var elementosEncontrados = 0;
var caixaSugestoes = "caixaSugestaoBusca";
var listaSugestoes = "autoSugestao";

function autocompletar(ROOT) {
	$('#textoEntrada').focusin().keyup(function(objEvento) {
		pesquisa($('#textoEntrada').val(), ROOT, objEvento);
	});
	$('#textoEntrada').focusin(function(){
		if($('#textoEntrada').val()!="")
			$('#'+caixaSugestoes).fadeIn();
	});
	$('#textoEntrada').focusout(function(){
		$('#'+caixaSugestoes).fadeOut();
	});
}

function codigoTecla(objEvento){
	if (objEvento == null){
		codTecla = event.keyCode;
		escapeKey = 27;
	} else {
		codTecla = objEvento.keyCode;
		escapeKey = objEvento.DOM_VK_ESCAPE;
	}
	return codTecla;
}

function pesquisa(textoEntrada,ROOT, objEvento) {
	codTecla = codigoTecla(objEvento);
	if(codTecla<37 || codTecla>40){
		if(textoEntrada.length == 0) {
			$('#'+caixaSugestoes).hide();
		} else {
			$.post(ROOT+"library/autocompletar.php", {palavra: ""+textoEntrada+""}, function(data){
				if(data.length >0) {
					$('#'+caixaSugestoes).show();
					$('#'+listaSugestoes).html(data);
				}else{
					$('#'+caixaSugestoes).hide();
					$('#'+listaSugestoes).html(data);
				}
				elementosEncontrados = 0;
				$('.sugestaoPesquisa').each(function(indice) {
					elementosEncontrados++;
				});
				indiceElementoSelecionado = -1;
				$('[id^=palavraSugerida]').focusin(function(){
					mudarClasse($(this).attr("id"),"sugestaoPesquisa itemEscolhido");
					$("#textoEntrada").attr("value",$(this).text());
				});
				$('[id^=palavraSugerida]').focusout(function(){
					mudarClasse($(this).attr("id"),"sugestaoPesquisa");
				});
				$('[id^=palavraSugerida]').mouseenter(function(){
					$(this).focusin();
					indiceElementoSelecionado = $(this).index();
				});
				$('[id^=palavraSugerida]').mouseleave(function(){
					$(this).focusout();
				});
			});
		}
	}else{
		navegarPeloTeclado(codTecla);
	}
}

function palavraSugerida(palavraSugerida) {
	$('#textoEntrada').attr("value",palavraSugerida);
	$('#'+caixaSugestoes).hide();
	document.forms["busca"].submit();
}

function desabilitarNavegacaoTeclado(){
	$(document).unbind();
}

function navegarPeloTeclado(codTecla){
	switch(codTecla){
		case 38:
			indiceElementoSelecionado = indiceElementoSelecionado<0?(elementosEncontrados-1):--indiceElementoSelecionado;
			break;
		case 40: indiceElementoSelecionado = indiceElementoSelecionado<0?0:++indiceElementoSelecionado; break;
	}
	
	indiceElementoSelecionado = indiceElementoSelecionado<0?(elementosEncontrados-1):indiceElementoSelecionado;
	indiceElementoSelecionado = indiceElementoSelecionado>(elementosEncontrados-1)?0:indiceElementoSelecionado;
	
	$('.sugestaoPesquisa').each(function(indice) {
		if(indice==indiceElementoSelecionado){
			$(this).focusin();
		}
	});
}

function mudarClasse(elemento,classe){
	$("[id^=palavraSugerida]").attr("class","sugestaoPesquisa");
	$("#"+elemento).attr("class",classe);
}
