var Buscador = Class.create({
  initialize: function(url, respParams, respFunc) {
  	this.url=url;
  	this.enProceso=false;
  	this.params=null;
  	this.ordenadoPor=null;
  	 	
    this.frmBusquedas = respParams.frmBusquedas;
    this.divListado = respParams.divListado;
    this.respFunc = respFunc;

  	if (typeof(this.frmBusquedas) == 'string') {
  		this.frmBusquedas = $(this.frmBusquedas);
  	}
  	if (typeof(this.divListado) == 'string') {
  		this.divListado = $(this.divListado);
  	}
  	this.tbListado = this.divListado.firstDescendant();
  },
  
  buscar: function(params, noVaciar) {
    if (this.enProceso) {
      return;
    }
    this.enProceso=true;
    if (this.params == null) {
      params=this.frmBusquedas.serialize();
      this.params=params;
      this.ordenadoPor=null;
    }
    inhabilitarControles([this.frmBusquedas, this.divListado]);
    if (noVaciar == null || noVaciar == false) {
      vaciarTabla(this.tbListado);
    }
    // Cabecera
    var row = this.tbListado.insertRow(-1);
    row.className = "cab";
    this.respFunc.crearCabecera(row);
    // Cuerpo
    var miBuscador=this;
    var options = {
       method: 'post',
       parameters: params,
       onComplete: function(transport) {
		 var result = transport.responseText.evalJSON(true);

		 // Crear el titular
		 if (result.titular != null && miBuscador.respFunc.crearTitular != null) {
		   miBuscador.respFunc.crearTitular(result.titular);
		 }
         // Crea el paginado
		 var par = false;
         if (result.listado.length > 0) {
           var saltoCab=0;
           result.listado.each(function(obj, index) {
             var row = miBuscador.tbListado.insertRow(-1);
             row.className = (par)?"par":"impar";
             miBuscador.respFunc.crearContenido(row, obj);
             par = !par;
             saltoCab++;
             if (saltoCab == 20) {
             	 saltoCab=0;
             	 // Cabecera
               var row = miBuscador.tbListado.insertRow(-1);
               row.className = "cab";
               miBuscador.respFunc.crearCabecera(row);
             }
           });
         } else {
         	 var row = miBuscador.tbListado.insertRow(-1);
         	 row.className = "impar";
         	 miBuscador.respFunc.crearSinContenido(row);
         }
         
         // Pie
         var row = miBuscador.tbListado.insertRow(-1);
         row.className = "cab";
		 miBuscador.respFunc.crearPie(row, result.paginado);
		 // Fin
		 habilitarControles([miBuscador.frmBusquedas, miBuscador.divListado]);
		 if (miBuscador.divListado.scrollTop > 0) {
		     miBuscador.divListado.scrollTop =
		        miBuscador.divListado.scrollTop 
		        + miBuscador.divListado.clientHeight 
		        - miBuscador.tbListado.rows[0].clientHeight;
		   }
		   miBuscador.enProceso=false;
		 },
       onFailure: function(transport) {
         alert('Problemas al realizar la consulta.' + transport.responseText);
       }
    }
    var myAjax = new Ajax.Request(this.url, options);    
  	
  },
  
  continuarBusqueda: function(desde) {
    //if (this.tbListado.rows.length > 0) {
    //  this.tbListado.deleteRow(this.tbListado.rows.length -1);
    //}
    var params = this.params;
    if (this.ordenadoPor != null) {
      params += "&ordenar_por=" + this.ordenadoPor;
    }
    this.buscar(params + "&registro_inicial=" + desde, false);
  },
  
  ordenarBusqueda: function(ordenarPor) {
    this.ordenadoPor = ordenarPor;
    this.buscar(this.params + "&ordenar_por=" + ordenarPor);
  }
});

