Resultados = {
  "monthActive" : null,
  "months" : [],

  "table" : null,
  "tableHistory" : null,
  
  "length" : null,
  
  "setTable" : function( table )
  {
    this.insertSpans( table );
    this.table = table;
    this.tableHistory = table.cloneNode(true);
    this.length = table.getElementsByTagName("table").length;
  },
  
  "insertSpans" : function( table )
  {
    var span;
    var h3 = table.getElementsByTagName("h3");
    
    for( var i=0; i<h3.length; i++ )
    {
      if( h3[i].getElementsByTagName("span").length === 0 )
      {
        span = document.createElement("span");
        for( var j=0; j<h3[i].childNodes.length; j++ )
        {
          span.appendChild( h3[i].childNodes[0] );
        }
        h3[i].appendChild(span);
      }
    }
  },
  
  "isNextGames" : function()
  {
    return document.getElementById("corpo").className === "proximos-jogos";
  },
  
  "getFirstMonthToOpen" : function()
  {
    return this.isNextGames()?0:this.length-1;
  },
  
  // função para mostrar determinadas linhas de uma tabela , conforme o campeonato
  "filter" : function(obj)
  {
    this.table.innerHTML = "";
  	this.table.innerHTML = this.tableHistory.innerHTML;
  	var idDiv = obj.id;
  	var mes, tables = this.table.getElementsByTagName('table');
    
    var numLi = document.getElementById(idDiv).parentNode.parentNode.getElementsByTagName('li');
  	for (var i = 0 ; i < numLi.length; i++){
  		numLi[i].className = "";
  	}
    
  	if (idDiv != 'todos') {
  		var padrao = new RegExp(idDiv);			
  		var trs = this.table.getElementsByTagName('tr');	
  		
  		for (var i = trs.length-1; i >= 0 ; i--){
  			var classNome = trs[i].className;
  			//var teste = padrao.test(classNome);
  			var teste = trs[i].getElementsByTagName("td")[ 2 ].innerHTML.replace( /<[^>]+>/g, "" ) === document.getElementById(idDiv).innerHTML;
        if (!teste && (trs[i].parentNode.nodeName.toLowerCase() != 'thead') ) {
  	 		 trs[i].parentNode.removeChild(trs[i]);
  			}
  		}

      // apaga tabelas vazias
      for(var i=0; i<tables.length; i++){
        if( tables[i].getElementsByTagName("tr").length <= 1 ){
          mes = tables[i].parentNode;
          mes.parentNode.removeChild(mes);
          i--;
        }
      }
  	}
  	else
  	{
      this.reset();
    }
  	
  	
  	
  	clubes.intercalaCor(idTabela);
  	document.getElementById(idDiv).parentNode.className = "ativo";
  },
  
  "isMonthElement" : function(obj)
  {
    return obj.className.match( /(^| )mes($| )/ )?true:false;
  },
  
  "getMonths" : function(month)
  {
    var divs;
    
    if( this.months.length === 0 )
    {
      divs = document.getElementsByTagName("div");
      
      for( var i=0; i<divs.length; i++ )
      {
        if( this.isMonthElement(divs[i]) )
        {
          this.months.push( divs[i] );
        }
      }
    }
  
    return (typeof month).toLowerCase()!=="undefined"?this.months[month]:this.months;
  },
  
  "hide" : function(month)
  {
    this.getMonths( month ).style.display = "none";
    this.monthActive = null;
  },
  
  "hideAll" : function()
  {
    var months;
    
    if( this.monthActive )
    {
      this.hide( this.monthActive );
    }
    else
    {
      months = this.getMonths();
      
      for( var i in months )
      {
        this.hide( i );
      }
    }
  },
  
  "createButton" : function( add, label )
  {
    label = (typeof label).toLowerCase()!=="undefined"?label:(add===1?"Proximo":"Anterior");
    
    var parent = this;
    var a = document.createElement("a");
    a.href = "javascript:void(0)";
    a.className = label.toLowerCase();
    
    this.events.add( a, "click", function(evt)
    {
      parent.events.preventDefault(evt);
      parent.active( parent.monthActive+add );
    } );
    
    return a;
  },
  
  "setButtons" : function( month )
  {
    var h3 = this.getMonths(month).getElementsByTagName("h3")[0];
        
    if( h3.className.match( /(^| )botoes($| )/ ) === null )
    { 
      // Anterior
      switch( month )
      {
        case 0: break;
        default:
          h3.insertBefore(this.createButton(-1), h3.firstChild);
      }
      
      // Próximo
      switch( month )
      {
        case this.months.length-1: break;
        default:
          h3.appendChild(this.createButton(1));
      }
      
      h3.className += " botoes";
    }
  },
  
  "show" : function( month )
  {
    this.getMonths( month ).style.display = "block";
    this.monthActive = month;
  },
  
  "active" : function( month )
  {
    this.hideAll();
    this.show( month );
    this.setButtons( month );
  },
  
  "events" : {
  
    /**
     * Retorna false para os eventos padroes
     * @param {Object} e Evento
     */
    preventDefault : function(e){
      if(e.preventDefault) e.preventDefault();
      else e.returnValue = false; 
    }, 
    
    /**
     * Adicionar eventos
     * @param {Object} elem Elemento HTML
     * @param {String} event Evento (click, mouseover, mouseout, ...)
     * @param {Function} fun Função adicionada ao evento
     */
    add : function(elem,event,fun){
      if(document.attachEvent)
        elem.attachEvent('on'+event,fun);
      else if(document.addEventListener)
        elem.addEventListener(event,fun,true);
    },
    
    /**
     * Remover eventos
     * @param {Object} elem Elemento HTML
     * @param {String} event Evento (click, mouseover, mouseout, ...)
     * @param {Function} fun Função adicionada ao evento
     */
    remove : function(elem,event,fun){
      if(document.detachEvent)
        elem.detachEvent('on'+event,fun);
      else if(document.removeEventListener)
        elem.removeEventListener(event,fun,true);
    }
  
  },
  
  "reset" : function()
  {
    this.monthActive = null;
    this.months = [];
    //this.active(0);
    this.active( this.getFirstMonthToOpen() );
  },
  
  "init" : function(idTabela)
  {
    var parent = this;
    
    var listener = function(evt)
    {
      parent.setTable( document.getElementById(idTabela) );
      
      parent.reset();
      parent.events.remove( window, "load", listener );
      clubes.intercalaCor(idTabela);
      document.getElementById("todos").parentNode.className = "ativo";
    }
    
    this.events.add( window, "load", listener );
  }
};
Resultados.init(idTabela);
