//****************************************************************************************
// Search 
// Dennis Götting, 2005 Sinapsis 
//Usage: a = new searchbar({paso:'3', maxLength:'25', visibleitems:'10', formulario:'FORM',actual:'1',clase_on:'Font', clase_off:'F_off'});
//****************************************************************************************
function searchbar(arrayProps) {
	this.paso;
	this.maxLength;
	this.visibleitems;
        this.formulario;
	this.actual = 0;
	this.clase_on;
	this.clase_off;
	this.hide = false;
		
        //Insertamos los parametros a las variables locales
	for (i in arrayProps)	{
		if (i.charAt(0) != '_'){
			eval('this.'+i+' = arrayProps[\''+i+'\'];');
		}
	}	
}

searchbar.prototype.construct = function(){
	if(!this.hide){this._build();}
	else{
		this._makeForm();
		document.getElementById('googlebar').style.visibility='hidden';
	};
}

searchbar.prototype._build = function(){
    //Contruye la tabla completa
      var t=document.createElement('table');  	
      var tb=document.createElement('tbody'); 
      t.style.border='0px';
      t.style.width='100%';
    //una fila, 3 columnas
      var tr=document.createElement('tr');  	
      var tdl=document.createElement('td');
      tdl.id="searchbar_first"
      //tdl.width ='20%';
      tdl.style.textAlign='center';
      var tdc=document.createElement('td');
      //tdc.width ='60%';
      tdc.id="searchbar_paginas";
      tdc.style.textAlign='center';
      var tdr=document.createElement('td');
      tdr.id="searchbar_end"
      //tdr.width ='20%';
      tdr.style.textAlign='center';  
            
    //make formulario	
      this._makeForm();
     //celda izq anteriores
     //miramos si sea disponible(diferente de actual==0
     if(this.actual!=0){
                    var newLink=document.createElement('a');
                    newLink.setAttribute('href','javascript:document.getElementById("actual").value=0;  document.getElementById("formulario").submit();');
                    var linkText=document.createTextNode('<<');
                    newLink.className=this.clase_on; //Firefox
                    newLink.setAttribute('className', this.clase_on); //Iexplorer
                    newLink.appendChild(linkText);		
                    tdl.appendChild(newLink);
             }
            else
             {
                    var linkText=document.createTextNode('<<');		
                    tdl.setAttribute('className', this.clase_off);
                    tdl.className = this.clase_off;
                    tdl.appendChild(linkText);
             }
             //separador
             
     //para cada página poner un numero  
     //celda media numeros
    
      //for (index = 0; index < Math.ceil(this.maxLength/this.paso); index++) // Modificacion para controlar el numero de elementos
      items = 0;
      inicio = 0;
      if (parseInt(this.actual) + parseInt(this.visibleitems) > (Math.ceil(this.maxLength/this.paso))) {
            //Hemos sobre pasado el limite por lo que el inicio debe ser inferior al actual
                if(this.visibleitems>Math.ceil(this.maxLength/this.paso)){
                    inicio=0;
                }else{
                    inicio = Math.ceil(this.maxLength/this.paso) - parseInt(this.visibleitems);
                }
      } else {
            inicio = this.actual;
      }
      for (index = inicio; index < Math.ceil(this.maxLength/this.paso); index++){
            if (items==this.visibleitems) break;
            items++;
            var newLink=document.createElement('a');		
            newLink.setAttribute('href','javascript:document.getElementById("actual").value='+index+';  document.getElementById("formulario").submit();');
            newLink.setAttribute('className', this.clase_on); //Iexplorer
            newLink.className=this.clase_on; //Firefox
            //newLink.style.cssText 
            var linkText=document.createTextNode(parseInt(index)+1);		
            //si es la actual
            if(index==this.actual){
                    newLink.className = this.clase_off; newLink.setAttribute('className',this.clase_off);
            }
            var sep=document.createTextNode(' | ');
            tdc.appendChild(sep);		
            newLink.appendChild(linkText);				
            tdc.appendChild(newLink);
      }
      
      //celda derecha siguientes
      //miramos si es útlima
      if(this.actual<Math.ceil(this.maxLength/this.paso)-1){
            var newLink=document.createElement('a');	
            newLink.setAttribute('href','javascript:document.getElementById("actual").value='+(Math.ceil(this.maxLength/this.paso)-1)+';  document.getElementById("formulario").submit();');
            var linkText=document.createTextNode('>>');
            newLink.setAttribute('className', this.clase_on); //Iexplorer
            newLink.className=this.clase_on; //Firefox
            newLink.appendChild(linkText);	
        tdr.appendChild(newLink);
      }
      else{
        var linkText=document.createTextNode('>>');
            tdr.setAttribute('className', this.clase_off);
            tdr.className=this.clase_off;
            tdr.appendChild(linkText);
      }
            tr.appendChild(tdl); 
            tr.appendChild(tdc); 
            tr.appendChild(tdr);  
            tb.appendChild(tr);  
            t.appendChild(tb);
            
            //insertarlo en el div googlebar
            document.getElementById('googlebar').appendChild(t);				
}

searchbar.prototype._makeForm = function(){
  
  // creamos el formulario con campos necesarios
  //var f=document.createElement('form');  
  //f.setAttribute('name',this.formulario); 
  //f.setAttribute('id',this.formulario);
  //f.setAttribute('action','index.jsp');  
  //f.setAttribute('method','post');
  
   //ańadimos dos campos al formulario
  var ia = document.createElement('input');
  ia.setAttribute('id','actual');
  ia.setAttribute('name','actual');
  ia.setAttribute('type','hidden');
  ia.setAttribute('value',this.actual);
  
  var el = document.createElement('input');
  el.setAttribute('id','paso');
  el.setAttribute('name','paso');
  el.setAttribute('type','hidden');
  el.setAttribute('value',this.paso);  
  
  //f.appendChild(ia);
  //f.appendChild(el);
  
  document.getElementById('googlebar').appendChild(ia);
  document.getElementById('googlebar').appendChild(el);  

}


