

function objetus() {
        try {
                objetus = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        objetus= new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                        objetus= false;
                }
        }
        if (!objetus && typeof XMLHttpRequest!='undefined') {
                objetus = new XMLHttpRequest();
        }
        return objetus
}

_objetus=objetus();

var caracMin=3;//número de caracteres que ha de haber para que inicie le búsqueda
var target="sugbus";//<div> que muestra las sugerencias
var _ruta="ajax/";//ubicación de la APP
var elemento=-1;
var teclapulsada=false;

function sugierebusqueda(){
	if(!teclapulsada){
		cadena=$("#artista").val();
		ok=false;
	if(cadena.length>=caracMin){
		ok=true;
	}

	if(ok){
		//$("#buscando").css("display", "block"); 
		//document.getElementById("buscando").src=_ruta+"buscando.gif";
		//detecto si pulsa las flechas o el enter
		document.onkeydown = checkKeycode
	
		//fin detectar flechas/enter
  		_values_send="cadena="+cadena
    
		 _URL_=_ruta+"sugerenciasbuscador.php?"
		_objetus.open("GET",_URL_+_values_send,true);
		//alert(_URL_+_values_send)
		//_objetus.open("GET",_URL_,true);
    	//una vez enviado los valores inmediatamente llamamos a la propiedad 
     	//onreadystatechange
    	_objetus.onreadystatechange=function() {
    	//alert(_objetus.readyState)
     	//dentro de la funcion comprobamos el estado del proceso
      	//si es 4 (terminado) pedimos lo que nos han mandado
    		if (_objetus.readyState==4){
    			//alert(_objetus.status)
           		if(_objetus.status==200){
        		//alert(_objetus.responseText)
          			if(_objetus.responseText!=""){
          				//document.getElementById("buscando").src=_ruta+"spacer.gif";
         				document.getElementById(target).style.visibility="visible";
        				document.getElementById(target).innerHTML=_objetus.responseText;
        				$("#buscando").css("display", "none"); 
        			}else{
        				ocultaSugerencia();
        			}
            	}
        	}
    	}
   		 _objetus.send(null);
	}else{
		ocultaSugerencia();
	}
	}
}

function ponSugerencia(cadena){
	//alert("me llaman");
$("#artista").val(cadena);

	ocultaSugerencia();
}

function ocultaSugerencia(){
	elemento=-1;
	document.getElementById(target).style.visibility="hidden";
	document.getElementById(target).innerHTML="";
}


function rollSugerencia(id){
		es=document.getElementById("su"+id)
		es.className='sugerenciaRoll';
		//es.style.color = "#000000";
}


function rouSugerencia(id){
		es=document.getElementById("su"+id)
		es.className='sugerencia';
		//es.style.color = "#FFFFFF";
}
function ejecutaBusqueda(){
if($("#artista").val()!=""){
	window.location.href="busca_video.php?artista="+$("#artista").val()
	}}

//**********************************  teclado
function setTeclapulsada(){
	teclapulsada=false;
}
function marca(id){
	
	//quito la marca todos
	for(i=0;i<hay;i++){
		rouSugerencia(i)
	}
	//marco el seleccionado
	
	 rollSugerencia(id)
	$("#artista").val(document.getElementById("valsu"+id).value);
}

//detecto que tecla pulsa
function checkKeycode(e) {
	//alert(e)
	hay=document.getElementById("sugtotal").value;
	var keycode;
	//13-> enter
	//38->arriba
	//40->enter
	if (window.event){
		keycode = window.event.keyCode;
	}else if (e){
		keycode = e.which;
	}

	if(keycode==38){
		teclapulsada=true;
		if(elemento>0){
			elemento=elemento-1;
			marca(elemento);			
		}
	}else if(keycode==40){
		teclapulsada=true;
		if(elemento<hay-1){
			elemento=elemento+1;
			marca(elemento);
			
		}
	}else if(keycode==13){
		//return false;
	ejecutaBusqueda();
	}
}
//detecto que tecla pulsa
function setEnter() {
var keycode;
	//13-> enter
	//38->arriba
	//40->enter
	if (window.event){
		keycode = window.event.keyCode;
	}else if (e){
		keycode = e.which;
	}
 if(keycode==13){

		ejecutaBusqueda();
	}
}
//***************************      fin teclado


