   var letters = new Array();
   var elementoSeleccionado=-1;
   var totalElementos=0;
   var CONTEXT_PATH='http://opinion.eluniversal.com/search'
   //var CONTEXT_PATH='http://localhost:6666/search'

   function Letter(letter,authors){
      this.letter=letter;
      this.authors=authors;
   }
   
   function Author(id,name,description){
   	this.id=id;
   	this.name=name;
   	this.description=description;
   }
   
   function getAuthor(event){
	   var keyCode = window.event ? event.keyCode : event.which;
	    if((keyCode == 40 || keyCode == 38 || keyCode==13)&&totalElementos>0){
	       updateSelected(keyCode);
	    }
	    else{
	     var elNombre=Trim(document.getElementById("_name").value);
	     var inicial;
	     if(elNombre==""){
	     	 clearList();
		     return false;
	     }
	     	
	     if(elNombre!=" " && elNombre.length>0)
	     	inicial=elNombre.substring(0,1).toUpperCase();
	     var find = false;
	     var hasta = letters.length;
	     var i=0;

	     while(!find && i<hasta){
	        if(letters[i].letter==inicial)
	        	find=true;
	        else
	        	i++;
	     }
	     
	     clearList();
	     
	     if(find){
			displayList(document.getElementById("_name").value,i);
	     }
	     else{
			dataObject='{name:'+elNombre+'}';
			evalServerCall('/do/searchAuthor?name='+elNombre,dataObject); 
	     }
	    }
   }
   
   function displayList(elNombre,i){
     	var valor="";
     	var t=0;
     	var top=0;
     	var j=0
     	var theName=""
     	var verificaExpresion=new RegExp("(^("+elNombre+")|( "+elNombre+"))", "i");
     	for(j=0; j<letters[i].authors.length && t<10; j++){
     	  if(verificaExpresion.test(letters[i].authors[j].name)){
     	  	  theName = letters[i].authors[j].name;
     	      theName = theName.replace(verificaExpresion, "<span class=blackLetter>$1</span>");     	      
     	      theName = theName.toUpperCase();
	     	  valor += "<div style='position:absolute; top: "+top+"px;left: 0px;width: 175;' class="+(t>0?"normal":"normalTop")+" idAuthor="+letters[i].authors[j].id+" id="+t+" onMouseOver=mouseDentro(this) onClick=clickLista(this) >"+theName+"</div>";
	     	  t++;
	     	  top+=12;
     	  }     	  
     	}
     	
     	more=false;
     	while(j<letters[i].authors.length && !more){
 	     	  if(letters[i].authors[j].name.substring(0,elNombre.length).toUpperCase()==elNombre.toUpperCase()){
 	     	  	more=true;
 	     	  }
 	     	  j++;
     	}
     	if(more)     	
     		valor += "<div style='position:absolute; top: "+top+"px;left: 0px;width: 175;' class=normalBottom><strong>...</strong></div>";
     	totalElementos=t--;
	    elementoSeleccionado=-1;
     	document.getElementById("_authors").innerHTML=valor;     	
     	if(t>=0){
	     	var clase="";
	     	if(t==0)
	     		clase="normalAll";
	     	else if(t>0)
	     		clase="normalBottom";
	     	document.getElementById(""+t).className=clase;
	    }
	    else
    	  noDisplayAuthors();
	    document.getElementById("_authors").style.display="block"; 
   }
   
   function clearList(){
     document.getElementById("_authors").innerHTML="";
     totalElementos=0;
	 elementoSeleccionado=-1;     
   }
   
   function getClaseNormal(){
	 return  elementoSeleccionado==0?"normalTop":elementoSeleccionado==1 && totalElementos==1?"normalAll":elementoSeleccionado==totalElementos-1?"normalBottom":"normal";
   }
   
   function getClaseResaltado(){
   	return  elementoSeleccionado==0 && totalElementos==1?"resaltadoAll":elementoSeleccionado==0?"resaltadoTop":elementoSeleccionado==totalElementos-1?"resaltadoBottom":"resaltado";
   }
   
   function updateSelected(keyCode){
   		//keyCode == 39 --> arrow right
   		//keyCode == 37 --> arrow left
	      if (keyCode == 40){
		  //arrow down
		  if(elementoSeleccionado>-1)
			  document.getElementById(elementoSeleccionado).className=getClaseNormal();
	      if(elementoSeleccionado<totalElementos-1)
		    elementoSeleccionado++;
	      else
	    	elementoSeleccionado=0;
		   if(elementoSeleccionado>-1)document.getElementById(elementoSeleccionado).className=getClaseResaltado();
		  }
		  else if (keyCode == 38){
		    //arrow up
		    if(elementoSeleccionado>-1)
			    document.getElementById(elementoSeleccionado).className=getClaseNormal();
		    if(elementoSeleccionado>0)
		    	elementoSeleccionado--;
		    else
		    	elementoSeleccionado=totalElementos-1;
		    if(elementoSeleccionado>-1)document.getElementById(elementoSeleccionado).className=getClaseResaltado();
		  }	
		  else if(keyCode==13){
		    document.getElementById("_name").focus();
		  	if(elementoSeleccionado>-1){
			  	clickLista(document.getElementById(elementoSeleccionado));
		  	}			    
		  }
   }
   
   function displayAuthors(obj){
     var o = new Object();
	 var aux="";
	 o.list = obj.detail;
	 var authors = new Array();
	 var valor="";
	 for(var i=0;i<o.list.length;i++){	
	 	authors[authors.length]=new Author(o.list[i].id,o.list[i].nombre,"");
	 }
	 var x = 0
	 if(i-1>0)
	 	x=i-1;
   	 letters[letters.length]=new Letter(o.list[x].nombre.substring(0,1).toUpperCase(),authors);
   	 displayList(document.getElementById("_name").value,letters.length-1);
   }
   
   function noDisplayAuthors(){
      clearList();
      document.getElementById("_authors").innerHTML="<div style='position:absolute; top: 0px;left: 0px;width: 175;' class=alerta>&nbsp;&nbsp;No hay coincidencias</div>";
      document.getElementById("_authors").style.display="";
      
   }
   
   function mouseFuera(){
	if(elementoSeleccionado!=-1)
		 document.getElementById(elementoSeleccionado).className=getClaseNormal(); 
	}
   
   function mouseDentro(elemento){
		mouseFuera();
		elementoSeleccionado=elemento.id;
		elemento.className=getClaseResaltado();
		
	}
	
	function clickLista(elemento){
		v=1;
		var valor=limpiaPalabra(elemento.innerHTML); 
		id=elemento.idAuthor;	
		if(valor!=""){
			document.getElementById("_name").value=valor;
			document.getElementById("_authors").style.display="none"; 
			//document.getElementById("_authors").style.visibility="hidden"; 
			elemento.className="normal";						
			window.document.location="http://www.eluniversal.com/indexSearch/do/index/show?code="+elemento.getAttribute("idAuthor");
		}
	}
	
	function limpiaPalabra(palabra){
		palabra=palabra.replace(/<span class=\"blackLetter\">/i, "");
		palabra=palabra.replace(/<span class=blackLetter>/i, "");		
		palabra=palabra.replace(/<\/span>/i, "");
		return palabra;
	}
	
	function Trim( str ) {
		var resultStr = "";
		resultStr = TrimLeft(str);
	    resultStr = TrimRight(resultStr);
	    return resultStr;
	}
	
	function TrimLeft( str ) {
		var resultStr = "";
		var i = len = 0;
		// Return immediately if an invalid value was passed in
		if (str+"" == "undefined" || str == null) 
		return null;
		// Make sure the argument is a string
		str += "";
		if (str.length == 0) 
		resultStr = "";
		else { 
		// Loop through string starting at the beginning as long as there
		// are spaces.
		// len = str.length - 1;
		len = str.length;
		
		while ((i <= len) && (str.charAt(i) == " "))
		i++;
		// When the loop is done, we're sitting at the first non-space char,
		// so return that char plus the remaining chars of the string.
		resultStr = str.substring(i, len);
		}
		return resultStr;
	}
	
	function TrimRight( str ) {
		var resultStr = "";
		var i = 0;
		// Return immediately if an invalid value was passed in
		if (str+"" == "undefined" || str == null) 
		return null;
		// Make sure the argument is a string
		str += "";
		
		if (str.length == 0) 
		resultStr = "";
		else {
		// Loop through string starting at the end as long as there
		// are spaces.
		i = str.length - 1;
		while ((i >= 0) && (str.charAt(i) == " "))
		i--;
		
		// When the loop is done, we're sitting at the last non-space char,
		// so return that char plus all previous chars of the string.
		resultStr = str.substring(0, i + 1);
		}
		
		return resultStr; 
	}
	
	
	