var Timeout = null;
var txtBusqueda = null;
var botonBuscar = null;
var divSugerencias = null;
var ProductosSugeridos = null;
var EnlaceSeleccionado = -1;

function TextoCambiado()
{
	if (Timeout!=null) clearTimeout(Timeout);
	Timeout = setTimeout("LeerSugerencias()", 100);
}

function LeerSugerencias()
{
	if (txtBusqueda.value.length>2)
	{
		botonBuscar.src = "imagenes/ajaxload.gif";
		new Ajax.Request("buscarjson.php?busqueda=" + txtBusqueda.value, {method: 'get', onSuccess: function(transport) {MostrarSugerencias(transport);}, onComplete: function() {botonBuscar.src = "imagenes/btn_lupa.gif";}});
	}
	else OcultarSugerencias();
}

function MostrarSugerencias(transport)
{
	Productos = transport.responseText.evalJSON().productos;
	var NumProductos = Productos.length-1;

	if (NumProductos>0)
	{
		var Enlace;
		
		if (divSugerencias==null)
		{
			divSugerencias = document.createElement("div");
			divSugerencias.className = "divAutoSugerencias";
			document.body.appendChild(divSugerencias);
			var posicion = txtBusqueda.cumulativeOffset();
			divSugerencias.style.left = posicion.left + "px";
			divSugerencias.style.top = (posicion.top + txtBusqueda.offsetHeight) + "px";
		}
		BorrarSugerencias();

		for (var i=0; i<NumProductos; i++)
		{
			Enlace = document.createElement("a");
			Enlace.href = Productos[i].url;
			Enlace.innerHTML = Productos[i].nombre + "<span class=\"categoria\">" + Productos[i].categoria + "</span>";
			Event.observe (Enlace, "mouseover", function() {QuitarSeleccionSugerencia();});
			divSugerencias.appendChild(Enlace);
		}
		divSugerencias.style.visibility = "visible";
		Event.observe (document, "mouseup", DocumentoClick);
	}
	else OcultarSugerencias();
}

function OcultarSugerencias()
{
	Event.stopObserving (document, "mouseup", DocumentoClick);
	if (divSugerencias!=null) divSugerencias.style.visibility = "hidden";	
	BorrarSugerencias();
}

function BorrarSugerencias()
{
	if (divSugerencias!=null)
	{
		var NumElementos = divSugerencias.childNodes.length;
		for (var i=0; i<NumElementos; i++) divSugerencias.removeChild(divSugerencias.lastChild);
	}
	EnlaceSeleccionado = -1;
}

function SeleccionarSugerencia (Desplazamiento)
{
	if (divSugerencias!=null)
	{
		var NumSugerencias = divSugerencias.childNodes.length;
		if (NumSugerencias>0)
		{
			QuitarSeleccionSugerencia();
			EnlaceSeleccionado += Desplazamiento;
			if (EnlaceSeleccionado<0) EnlaceSeleccionado=0;
			else if(EnlaceSeleccionado>NumSugerencias-1) EnlaceSeleccionado = NumSugerencias-1;
			divSugerencias.childNodes[EnlaceSeleccionado].className="seleccionado";
		}
	}
}

function QuitarSeleccionSugerencia ()
{
	if (EnlaceSeleccionado>=0) divSugerencias.childNodes[EnlaceSeleccionado].className="";
}

function Navegar()
{
	if (EnlaceSeleccionado>=0) window.location = divSugerencias.childNodes[EnlaceSeleccionado].href;
	else if (txtBusqueda.value.length > 0) window.location = "buscar-" + txtBusqueda.value;
}

function DocumentoClick (event)
{
	if (!Event.element(event).descendantOf (divSugerencias)) OcultarSugerencias ();
}

function BuscarOnKeyDown(e)
{
	var tecla = (window.event) ? event.keyCode : e.which;
	if (tecla==13) Navegar();
	else if (tecla==27) OcultarSugerencias();
	else if (tecla==38) SeleccionarSugerencia(-1);
	else if (tecla==40) SeleccionarSugerencia(1);
	else TextoCambiado();
}
	
function InicializarBuscador()
{
	txtBusqueda = document.getElementById("caja_buscar");
	botonBuscar = document.getElementById("botonBuscar");
	Event.observe (txtBusqueda, "keydown", function(event) {BuscarOnKeyDown(event);});
	//Event.observe (txtBusqueda, "blur", function() {setTimeout("OcultarSugerencias()", 50);});
}

Event.observe (window, "load", function() {InicializarBuscador();});
