/**
 * @author A.P.Borges / F.Shark
 */


function filtra(lnk){
	_ind.filter(lnk);
	_ind.response();
}

_rgx=new RegExp(/^\s*/);

/**
 * @author A.P.Borges / F.Shark
 * @class Description Classe para exibir, ordenar, listar e filtrar os links do índice
 */
function Indice(){
//fields:
		this.tamanho=0;					//numero total de itens
		this.tamanhoFiltered=0;			//numero atual de itens filtrados
		this.links=new Array();;		//conjunto dos itens do índice
		this.filtered=new Array();		//conjunto dos itens do índice filtrados
		this.strIndice=new String();	//string de saida (escrita pelo metodo "response")
		this.ID_DESTINO='listas';		//id onde será escrita a string de saida pelo pelo metodo "response"
		this.ID_ORIGEM='indice';		//elemento oculto com o conteúdo do índice
		this.TAG_ORIGEM='li';			//nome da tag oculta com cada entrada do índice
		
//methods:
		this.load=function(){
			//implementar caso deseje alguma sinalização que indique que está em processamento
			//exemplo:
			document.getElementById('load').style.display='block';
			return;
		}
		
		this.unload=function(){
			//deve anular a sinalização que indica o processamento
			//exemplo:
			document.getElementById('load').style.display='none';
			return;
		}
		
		this.filter=function(obj){
			this.load();
			filtro=obj.innerHTML.toUpperCase();
			
			var out=new Array();
			for (x=0;x<this.tamanho;x++){
			  
        if(this.links[x][2]==filtro || filtro.toUpperCase()=='LISTA COMPLETA'){
					out.push(this.links[x]);
				}
				
				if(filtro.toUpperCase()=='# - 0'){
				  c=["0","1","2","3","4","5","6","7","8","9","@","#","$","%","*","(","["];
				  for(_x=0;_x<=c.length;_x++){
				    filtroNum=c[_x];
            if(this.links[x][2]==filtroNum){
					     out.push(this.links[x]);
				    }
          }
				}
				
			}
			this.filtered=out;
			this.tamanhoFiltered=this.filtered.length;
			this.setColumns();
			this.unload();
			return false;
		}
		
		this.setLinks=function(){
			this.load();
			var itens;
			try{itens=document.getElementById(this.ID_ORIGEM).getElementsByTagName(this.TAG_ORIGEM);}catch(e){return false};
			this.tamanho=itens.length;
			this.tamanhoFiltered=this.tamanho;
			var tLinks=new Array;	
			for (x=0; x<itens.length; x++){
				tLinks[x]=new Array (
					itens[x].getElementsByTagName('a')[0].getElementsByTagName('span')[0].innerHTML.replace(_rgx,''),/*Conteúdo*/
					itens[x].innerHTML.replace(_rgx,''),/*HTML*/
					itens[x].getElementsByTagName('a')[0].getElementsByTagName('span')[0].innerHTML.replace(_rgx,'').substr(0,1).toUpperCase()/*Letra Inicial*/
				);
			}
			this.links=tLinks.sort();
			this.unload();
			return true;
		}
		
		this.setColumns=function(){
			this.load();
			var numColumns=3;
			var colunaAtual=numColumns;
			var empilhamento=0;
			var maximoPorColuna=parseInt((this.tamanhoFiltered)/numColumns,10)
			
			var resto=parseInt(this.tamanhoFiltered-(maximoPorColuna*numColumns),10);
			var itemAtual=0;
			var out=new String();
			
			
			
			colunaLen=[maximoPorColuna,maximoPorColuna,maximoPorColuna];
			
      total=this.tamanhoFiltered;
      if(total>3){
      
        if(resto%3==1){
          colunaLen[0]=maximoPorColuna+1;
        }else if(resto%3==2){
          colunaLen[0]=maximoPorColuna+1;
          colunaLen[1]=maximoPorColuna+1;
        }
        
      }else if(total!=0){
        colunaLen[0]=1
        colunaLen[1]=total==2?1:0;
        colunaLen[2]=total==3?1:0;
      }
      
			
			for (x=0,COL=0;x<=this.tamanhoFiltered;x++){
				if(COL<3){
          out+='<ul>';
  				  //out+="<li>coluna"+COL+"</li>";
  				  empilhamento+=colunaLen[COL];COL++
  				  
            for(;itemAtual<empilhamento;itemAtual++){
  				  	out+='<li>';
              out+=this.getFiltered()[itemAtual][1];
  						out+='</li>';
  					}	
            
  					
  				out+='</ul>';
  			}
			}
			this.strIndice=out;	
			this.unload();
			return true;			
		}
		
		this.response=function(){
			document.getElementById(this.ID_DESTINO).innerHTML=this.getStrIndice();
		}
		
//getters as setters:
		this.getLinks=function(){
			return this.links;
		}
		
		this.getTamanho=function(){
			return this.tamanho;
		}
		
		this.getFiltered=function(){
			return this.filtered;
		}
		
		this.getStrIndice=function(){
			return this.strIndice;
		}
				
//constructor:
		this.setLinks();
		this.filtered=this.getLinks();
		this.setColumns();
}

