// JavaScript Document
function gebi(element_id){
	return document.getElementById(element_id);
}

function AjaxSearchSuggest(){
	this.initChars = 1;
	this.mainWord = '';
	this.suggestions={};
	this.scriptFile = "/temp/word-list.php?terminos_busqueda=";
	this.divContainer = "sugerencias_busqueda";
	this.url = "";
	
	this.setMainWord = function (word){
		this.mainWord = word;
		if(word.length>=this.initChars) this.loadWordList();
		else this.hideSuggestionList();
	}
	
	this.hideSuggestionList = function(){
		div = gebi(this.divContainer);
		div.style.display="none";
	}
	
	this.showSuggestionList = function(){
		div = gebi(this.divContainer);
		div.style.display="block";
	}
	
	this.refreshSuggestionList = function(){
		div = gebi(this.divContainer);
		code = "";
	//	if(this.suggestions.lenght>0){
			for(var i in this.suggestions){
				code += "<a class=\"palabra_sugerida\" href=\""+this.url+this.suggestions[i].fancy+"\" title=\"traducir "+this.suggestions[i].termino+"\">"+this.suggestions[i].termino+"</a>";
			}
			div.innerHTML = code;
			this.showSuggestionList();
	//	}
		//else this.hideSuggestionList();
	}
	
	this.loadWordList = function(){
		var listurl = this.scriptFile+this.mainWord;
		this.suggestions = null;

		// Cargar por AJAX el array de todos los objetos
		xmlHttp.open("GET", listurl, true);	// async
		xmlHttp.onreadystatechange = function () {
			if (xmlHttp.readyState == 4) {
				eval(xmlHttp.responseText);
				SearchSuggest.refreshSuggestionList();		
			}
		};
		window.setTimeout(function(){xmlHttp.send(null);}, 10);
	}
	
}
	