
/**
 * Un control de caja de texto Autosugerencias.
 * @clase
 * @alcance público
 */
function AutoSuggestControl(oTextbox /*:ElementoInputHTML*/, 
                            oProvider /*:ProveedorSugerencias*/,
							oForm /*forulario de entrada*/,
							oCapaCargando /*capa de espera de resultados*/,
							oImagenDiv) /*Imagen que se muestra*/{
    
    /**
     * Las sugerencias actualmente seleccionadas.
     * @alcance privado
     */   
    this.cur /*:int*/ = -1;

    /**
     * La capa de la lista desplegable.
     * @alcance privado
     */
    this.layer = null;
	
	/*
		Array con los textos limpios de la lista desplegable
	*/
	this.textos = new Array();
    
    /**
     * El proveedro de sugerencias para la característica "autosuggest".
     * @alcance privado.
     */
    this.provider /*:Proveedor de Sugerencias*/ = oProvider;
    
    /**
     * La caja de texto a capturar.
     * @alcance privado
     */
    this.textbox /*:ElementoInputHTML*/ = oTextbox;
    
    /**
     * Timeout ID para escritura rápida.
     * @alcance privado
     */
    this.timeoutId /*:int*/ = null;

    /**
     * El texto que el usuario ha escrito.
     * @alcance privado
     */
    this.userText /*:String*/ = oTextbox.value;
	
	this.userForm = oForm;
	this.CapaCargando = oCapaCargando;
	this.ImagenDiv = oImagenDiv;
	
	this.autosuggest = autosuggest;
    
    //inicializa el control
    this.init();
    
}

/**
 * Autosugiere una o más sugerencias para lo que el usuario ha escrito.
 * Si no son pasadas sugerencias, entonces la autosugerencia no tiene lugar.
 * @alcance privado
 * @param aSuggestions Un array de strings de sugerencias.
 * @param bTypeAhead Si el control debe proveer una sugerencia "type ahead".
 */
function autosuggest (aSuggestions /*:Array*/, bTypeAhead /*:boolean*/) {
    
    //re-inicializa el indicador a la sugerencia actual
    this.cur = -1;
    //alert ("Autosuggest: "+aSuggestions.length);
    //se asegura que haya almenos una sugerencia
    if (aSuggestions.length > 0) {
        if (bTypeAhead) {
           this.typeAhead(aSuggestions[0]);
        }
        
        this.showSuggestions(aSuggestions);
    } else {
        this.hideSuggestions();

    }
};

/**
 * Crea la capa desplegable para mostrar múltiples sugerencias.
 * @alcance privado
 */
AutoSuggestControl.prototype.createDropDown = function () {

    //crea una capa con los estilos para mostrar los resultados sugeridos
    this.layer = document.createElement("div");
    this.layer.className = "suggestions";
    this.layer.style.visibility = "hidden";
    this.layer.style.width = this.textbox.offsetWidth;
	
    document.body.appendChild(this.layer);    
    
    //cuando el usuario pulsa en una sugerencia, obtiene el texto(innerHTML)
    //y lo coloca en la caja de texto
    var oThis = this;
    this.layer.onmousedown = 
    this.layer.onmouseup = 
    this.layer.onmouseover = function (oEvent) {
        oEvent = oEvent || window.event;
        oTarget = oEvent.target || oEvent.srcElement;
        if (oEvent.type == "mousedown") {
			if (oTarget.firstChild.nodeValue!="Buscar")
			{
				//oThis.textbox.value = oTarget.firstChild.nodeValue;
				if ((oThis.textos[oThis.cur] != null) || (oThis.textos[oThis.cur] != undefined))
				oThis.textbox.value = oThis.textos[oThis.cur];
			}
			else 
				oThis.textbox.value = oThis.textbox.value+'*';
				
			oThis.hideSuggestions();
//Alicia
			oThis.CapaCargando.style.visibility="visible";
			oThis.ImagenDiv.style.visibility="visible";
//
			oThis.userForm.action = "search.php";
			oThis.userForm.submit(); 
				
        } else if (oEvent.type == "mouseover") {
            oThis.highlightSuggestion(oTarget);
        } else {
            oThis.textbox.focus();
        }
    };
    
};

/**
 * Obtiene la coordenada izquierda de la caja de texto.
 * @alcance privado
 * @devuelve la coordenada izquierda de la caja de texto en píxeles.
 */
AutoSuggestControl.prototype.getLeft = function () /*:int*/ {

    var oNode = this.textbox;
    var iLeft = 0;
    
    while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;        
    }
    
    return iLeft;
};

/**
 * Obtiene la coordenada top de la caja de texto.
 * @alcance privado
 * @devuelve la coordenada Top de la caja de texto en píxeles.
 */
AutoSuggestControl.prototype.getTop = function () /*:int*/ {

    var oNode = this.textbox;
    var iTop = 0;
    
    while(oNode.tagName != "BODY") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }
    
    return iTop;
};

/**
 * Remarca la sugerencia siguiente o previa en la lista desplegable y
 * coloca la sugerencia en la caja de texto.
 * @param iDiff bien un número positivo o negativo indicando si
 *              selecciona la sugerencia siguiente o previa, respectivamente.
 * @alcance privado
 */
AutoSuggestControl.prototype.goToSuggestion = function (iDiff /*:int*/) {
    var cSuggestionNodes = this.layer.childNodes;
    
    if (cSuggestionNodes.length > 0) {
        var oNode = null;
    
        if (iDiff > 0) {
            if (this.cur < cSuggestionNodes.length-1) {
                oNode = cSuggestionNodes[++this.cur];
            }        
        } else {
            if (this.cur > 0) {
                oNode = cSuggestionNodes[--this.cur];
            }    
        }
        
        if (oNode) {
            this.highlightSuggestion(oNode);
            //this.textbox.value = oNode.firstChild.nodeValue;
			if (this.cur < cSuggestionNodes.length-1)
				this.textbox.value = this.textos[this.cur];
        }
    }
};

/**
 * Maneja tres eventos de pulsación de teclado.
 * @alcance privado
 * @param oEvent el objeto evento para el evento pulsar tecla teclado.
 */
AutoSuggestControl.prototype.handleKeyDown = function (oEvent /*:Event*/) {
oThis=this;
    switch(oEvent.keyCode) {
        case 38: //flecha arriba
            this.goToSuggestion(-1);
            break;
        case 40: //flecha abajo 
            this.goToSuggestion(1);
            break;
        case 27: //escape
            this.textbox.value = this.userText;
            this.selectRange(this.userText.length, 0);
            /* a través de */
        case 13: //enter
            this.hideSuggestions();
//            oEvent.returnValue = false;
			oThis.CapaCargando.style.visibility="visible";
			oThis.ImagenDiv.style.visibility="visible";

			oThis.userForm.action = "search.php";
			oThis.userForm.submit() 
//            if (oEvent.preventDefault) {
//                oEvent.preventDefault();
//            }
            break;
    }

};

/**
 * Maneja eventos Soltar tecla.
 * @alcance privado
 * @param oEvent el objeto evento para el evento keyup.
 */
AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {

    var iKeyCode = oEvent.keyCode;
    var oThis = this;
    
    //obtiene el texto actualmente escrito
    this.userText = this.textbox.value;
    
    clearTimeout(this.timeoutId);

    //para retroceso (8) and borrar (46), muestra sugerencias sin typeahead
    if (iKeyCode == 8 || iKeyCode == 46) {
        
        this.timeoutId = setTimeout( function () {
            oThis.provider.requestSuggestions(oThis, false);
        }, 500);
        
    //se asegura que no interfiere con teclas no-carácter
    } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignora
    } else {
        //pide sugerencias al proveedor de sugerencias con typeahead
        this.timeoutId = setTimeout( function () {
            oThis.provider.requestSuggestions(oThis, true);
        }, 500);
    }
};

/**
 * Oculta la lista desplegable de sugerencias.
 * @alcance privado
 */
AutoSuggestControl.prototype.hideSuggestions = function () {
    this.layer.style.visibility = "hidden";
	//Alicia
   this.layer.innerHTML = "";  //limpiar contenidos de la capa

};

/**
 * Remarca el nodo dado en la lista desplegable de sugerencias.
 * @scope private
 * @param oSuggestionNode El nodo representando una sugerencia en la lista desplegable.
 */
AutoSuggestControl.prototype.highlightSuggestion = function (oSuggestionNode) {
    
    for (var i=0; i < this.layer.childNodes.length; i++) {
        var oNode = this.layer.childNodes[i];
        if (oNode == oSuggestionNode) {
			this.cur = i;
            oNode.className = "current"
        } else if (oNode.className == "current") {
            oNode.className = "";
        }
    }
};

/**
 * Inicializa la caja de texto con gestores de evento para
 * la funcionalidad auto sugerencia.
 * @alcance privado
 */
AutoSuggestControl.prototype.init = function () {

    //salva una referencia a este objeto
    var oThis = this;
    
    //asigna el gestor de evento onkeyup
    this.textbox.onkeyup = function (oEvent) {
    
        //comprueba la localización apropiada del objeto evento
        if (!oEvent) {
            oEvent = window.event;
        }    
        
        //llama al método handleKeyUp() con el objeto evento
        oThis.handleKeyUp(oEvent);
    };
    
    //asigna gestor de evento onkeydown
    this.textbox.onkeydown = function (oEvent) {

        //comprueba la localización apropiada del objeto evento
        if (!oEvent) {
            oEvent = window.event;
        }    
        
        //llama al método handleKeyDown() con el objeto evento
        oThis.handleKeyDown(oEvent);
    };
    
    //asigna el gestor de evento onblur (oculta sugerencias)    
    this.textbox.onblur = function () {
        oThis.hideSuggestions();
    };
    
    //crea la lista desplegable de sugerencias
    this.createDropDown();
};

/**
 * Selecciona un rango de texto en la caja de texto.
 * @alcance público
 * @param iStart El índice de inicio (base 0) de la selección.
 * @param iEnd El índice final de la selección.
 */
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iEnd /*:int*/) {

    //usar rangos de texto para Internet Explorer
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange(); 
        oRange.moveStart("character", iStart); 
        oRange.moveEnd("character", iEnd - this.textbox.value.length);      
        oRange.select();
        
    //usar setSelectionRange() for Mozilla
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iEnd);
    }     

    //configurar focus de vuelta a la caja de texto
    this.textbox.focus();      
}; 

/**
 * Construye el contenido de la capa de sugerencias, lo coloca correctamente,
 * y muestra la capa.
 * @alcance privado
 * @param aSuggestions Un array de sugerencias para el control.
 */
AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/) {
    
    var oDiv = null;
	var safe,inicio,fin;
	var found;
    this.layer.innerHTML = "";  //limpiar contenidos de la capa
	this.textos = new Array();  //limpiar contenidos del array con los textos
	
    for (var i=0; i < aSuggestions.length; i++) {
        oDiv = document.createElement("div");
		safe = aSuggestions[i];
		if (found = safe.match("<b>"))
		{
			inicio = safe.split("<b>");
			resto = inicio[1].split("</b>");
		}
		oDiv.appendChild(document.createTextNode(inicio[0]));
		
		// Rellena el array con las sugerencias sin resaltar
		this.textos[i]=inicio[0]+resto[0]+resto[1];
		
		var bold = document.createElement("bold");
		bold.style.color = "#FF0000"
		bold.appendChild(document.createTextNode(resto[0]));
		oDiv.appendChild(bold);
		oDiv.appendChild(document.createTextNode(resto[1]));

        this.layer.appendChild(oDiv);
    }

    //Añade boton de busqueda en la lista, si hay alguna sugerencias
	if (aSuggestions.length > 0)
	{
		f = document.createElement("form");
		f.setAttribute("name", "f");
		pie = document.createElement("p");
		pie.setAttribute("class", "botonpie");
		f.appendChild(pie);
		b=document.createElement("button");
		b.setAttribute("type", "button");
		b.setAttribute("class", "botondentro");
		//b.style.color = "orange";
		b.appendChild(document.createTextNode("Buscar"));
		pie.appendChild(b);
		this.layer.appendChild(f);
		// Fin añadido boton
	}
    this.layer.style.left = this.getLeft() + "px";
    this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
    this.layer.style.visibility = "visible";
    this.layer.style.position = "absolute";
	this.layer.style.width = "628px";

};

AutoSuggestControl.prototype.iniciarBusqueda = function () {
         //comprueba la localización apropiada del objeto evento
		this.pulsado = true; 
};

/**
 * Inserta una sugerencia en la caja de texto, remarcando la 
 * parte sugerida del texto.
 * @alcance privado
 * @param sSuggestion La sugerencia para la caja de texto.
 */
AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) {

    //comprueba soporte para la funcionalidad typeahead
    if (this.textbox.createTextRange || this.textbox.setSelectionRange)
	{
        var iLen = this.textbox.value.length; 
        //this.textbox.value = sSuggestion; 
        this.selectRange(iLen, sSuggestion.length);
    }
};

/**
 * Provee sugerencias para nombres de Provincias.
 * @clase
 * @alcance público
 */
function SuggestionProvider() {
    this.http = zXmlHttp.createRequest();
}

/**
 * Pide sugerencias para el control de autosugerencias dado. 
 * @alcance protegido
 * @param oAutoSuggestControl El control autosuggest para el que proveer sugerencias.
 */
SuggestionProvider.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                            bTypeAhead /*:boolean*/) {

    var oHttp = this.http;
                                   
    //cancela cualquier petición activa                          
    if (oHttp.readyState != 0) {
        oHttp.abort();
    }                 
    
    //define los datos
    var oData = { 
        requesting: "texto", 
        text: oAutoSuggestControl.userText,
        limit: 30 
    };
    
    //abre conexión al servidor
    oHttp.open("post", "suggestions.php", true);
    oHttp.onreadystatechange = function () {
        if (oHttp.readyState == 4) {
            //evalúa el texto JavaScript devuelto(un array)
			if (oHttp.responseText)
			{
				var aSuggestions = JSON.parse(oHttp.responseText);
				//provee sugerencias al control
				oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);        
			}
        }
    };

    //envía la petición
    oHttp.send(JSON.stringify(oData));

};
