var jogos = new loadXMLDoc('/futebol/gestor/jogos2.xml');
jogos.run("countQtjDatas()");

daysWeek = new Array("Domingo","Segunda-feira","Terça-feira","Quarta-feira","Quinta-feira","Sexta-feira","Sábado");	

function removeAcentos(texto)
{
  var tam = texto.length; // tamanho texto

  var i; // contador for
    
  for(i=0; i<tam; i++)
  {
    texto = texto.replace(/á/i, "a");
    texto = texto.replace(/é/i, "e");
    texto = texto.replace(/í/i, "i");
    texto = texto.replace(/ó/i, "o");
    texto = texto.replace(/ú/i, "u");
    texto = texto.replace(/â/i, "a");
    texto = texto.replace(/ê/i, "e");
    texto = texto.replace(/î/i, "i");
    texto = texto.replace(/ô/i, "o");
    texto = texto.replace(/û/i, "u");
    texto = texto.replace(/ç/i, "c");
    texto = texto.replace(/ã/i, "a");
    texto = texto.replace(/õ/i, "o");
    texto = texto.replace(/ /, "");

  }

  return texto.toLowerCase();

}

function getDayWeek(d){
    d=d.split("/");
    dayWeek=new Date();
    dayWeek.setDate(d[0]);
    dayWeek.setMonth(d[1]-1);
    dayWeek.setYear(d[2]);
    return daysWeek[dayWeek.getDay()];
}

var datasQtJogos = new Array();
function countQtjDatas(){
    jCount=jogos.xmlhttp.responseXML.getElementsByTagName('jogo').length;
    function dataC(s){x=s.split("/"); return x[2]+x[1]+((x[0].length==1)?"0"+x[0]:x[0]);}
    for(var i=0;i<jCount;i++){
        iJogo=jogos.xmlhttp.responseXML.getElementsByTagName('jogo')[i];
        if(!datasQtJogos[dataC(iJogo.getElementsByTagName('data')[0].childNodes[0].nodeValue)]) datasQtJogos[dataC(iJogo.getElementsByTagName('data')[0].childNodes[0].nodeValue)]=1;
        else datasQtJogos[dataC(iJogo.getElementsByTagName('data')[0].childNodes[0].nodeValue)]++;
    }
}

function calendario(q){
    function gE(s){return ((j.getElementsByTagName(s)[0].childNodes[0])?j.getElementsByTagName(s)[0].childNodes[0].nodeValue:"")}
    function dataC(s){x=s.split("/"); return x[2]+x[1]+((x[0].length==1)?"0"+x[0]:x[0]);}
    /*
    function troca(){
        for(var i=0;i<iJogo.childNodes.length;i++){
            tmp=iJogo.childNodes[i].childNodes[0].nodeValue;
            iJogo.childNodes[i].childNodes[0].nodeValue=jJogo.childNodes[i].childNodes[0].nodeValue;
            jJogo.childNodes[i].childNodes[0].nodeValue=tmp;
        }
    }
    */
    jCount=jogos.xmlhttp.responseXML.getElementsByTagName('jogo').length;
    /*
    for(var i=0;i<jCount;i++){
        iJogo=jogos.xmlhttp.responseXML.getElementsByTagName('jogo')[i];
        for(var j=0;j<jCount;j++){
            jJogo=jogos.xmlhttp.responseXML.getElementsByTagName('jogo')[j];
            if((dataC(iJogo.getElementsByTagName('data')[0].childNodes[0].nodeValue)+iJogo.getElementsByTagName('hora')[0].childNodes[0].nodeValue)<(dataC(jJogo.getElementsByTagName('data')[0].childNodes[0].nodeValue)+jJogo.getElementsByTagName('hora')[0].childNodes[0].nodeValue)) troca();
        }
    }
    */
    
    document.getElementById('jogosTabela-'+q).appendChild(document.createElement('tbody'));
    tb=document.getElementById('jogosTabela-'+q).getElementsByTagName('tBody')[0];
    dataAntiga=0;
    dataAntigaContador=0;
    for(var i=0;i<jCount;i++){
            j=jogos.xmlhttp.responseXML.getElementsByTagName('jogo')[i];
            if(gE('tipo')==q){
                H_TR=document.createElement('tr');
                if(gE('time1').toLowerCase()=="brasil" || gE('time2').toLowerCase()=="brasil") H_TR.className="linhaBrasil";
    
                if(dataAntiga!=gE('data')){
                    dataAntigaContador=1;
                    H_TD_DATA=document.createElement('td');
                    H_TD_DATA.className="colJogos1 div";
                    H_TD_DATA.appendChild(document.createTextNode(gE('data')+' '));
                    H_TD_DATA.appendChild(document.createElement("em"));
                    H_TD_DATA.getElementsByTagName('em')[0].appendChild(document.createTextNode(getDayWeek(gE('data'))));
                    H_TD_DATA.rowSpan=datasQtJogos[dataC(gE('data'))];
                }else{dataAntigaContador++;}
                
                divs=((dataAntigaContador==datasQtJogos[dataC(gE('data'))])?" div":"");
                
                H_TD_JOGON=document.createElement('td');
                H_TD_JOGON.className="colJogos2"+divs;
                H_TD_JOGON.appendChild(document.createTextNode( i+49 ));
    
                H_TD_HORA=document.createElement('td');
                H_TD_HORA.className="colJogos3"+divs;
                H_TD_HORA.appendChild(document.createTextNode(gE('hora')));
    
    
		H_TD_LOCAL=document.createElement('td');
            	H_TD_LOCAL_LINK = document.createElement('a');
            	H_TD_LOCAL_LINK.href='/copa/2006/sedes/'+ removeAcentos (gE('cidade') ) +'/';
           	 //H_TD_RESULTADO_LINK.target="_new";
            	H_TD_LOCAL_LINK.appendChild(document.createTextNode(gE('cidade')));
            	H_TD_LOCAL.className="colJogos4"+divs;
            	H_TD_LOCAL.appendChild(H_TD_LOCAL_LINK);

                //H_TD_LOCAL.appendChild(document.createTextNode(gE('cidade')));
    
                H_TD_TIME1=document.createElement('td');
                H_TD_TIME1.className="colJogos6"+divs;
                H_TD_TIME1.appendChild(document.createTextNode(gE('time1')));
    
                H_TD_GOLS=document.createElement('td');
                H_TD_GOLS.className="colJogos7"+divs;
                H_TD_GOLS.appendChild(document.createTextNode( ((gE('golstime1')=='n')?'':gE('golstime1')) +" x "+ ((gE('golstime2')=='n')?'':gE('golstime2')) ));
    
                H_TD_TIME2=document.createElement('td');
                H_TD_TIME2.className="colJogos8"+divs;
                H_TD_TIME2.appendChild(document.createTextNode(gE('time2')));

						H_TD_RELATO=document.createElement('td');
            H_TD_RELATO.className="colJogos9"+divs;
            if (gE('relato'))
            {
              H_TD_RELATO_LINK = document.createElement('a');
              H_TD_RELATO_LINK.href=gE('relato');
              H_TD_RELATO_IMG=document.createElement('img');
              H_TD_RELATO_IMG.src='http://img.uol.com.br/ico_ler.gif';
              H_TD_RELATO_IMG.alt='Ver relato';
              H_TD_RELATO_IMG.border=0;
              H_TD_RELATO_LINK.appendChild(H_TD_RELATO_IMG);
              H_TD_RELATO.appendChild(H_TD_RELATO_LINK);
            }
            else
            {
              H_TD_RELATO.appendChild(document.createTextNode(' '));
            }

    
                if(dataAntiga!=gE('data')) H_TR.appendChild(H_TD_DATA);
                H_TR.appendChild(H_TD_JOGON);
                H_TR.appendChild(H_TD_HORA);
                H_TR.appendChild(H_TD_LOCAL);
                H_TR.appendChild(H_TD_TIME1);
                H_TR.appendChild(H_TD_GOLS);
                H_TR.appendChild(H_TD_TIME2);
            H_TR.appendChild(H_TD_RELATO);
                
                tb.appendChild(H_TR);
            }
            dataAntiga=gE('data');
    }
}