/// <reference path="jquery-1.3.2-vsdoc2.js" />

$(function() {
    var id = "#placares";
    
    $.getJSON(
        '/2010/json/gethppartidas.asp',
        function(result) {
            var campeonatos = result.campeonatos;
        
            // Apagando conteúdo
            $(id).empty();
            
            function createHtmlJogos(jogos) {
                var last = "";
                var ant = "";
                
                for (var x = 0; x < jogos.length; x++) {
                    var jogo = jogos[x];
                    
                    if (last == "" || last != jogo.data + " " + jogo.dayOfWeek) {
                        last = jogo.data + " " + jogo.dayOfWeek;
                        ant += "<span class='placar_dia_da_semana' style='font-weight: bold; color: #000080;'>";
                        ant += jogo.dayOfWeek + "&nbsp;(" + jogo.data + ")";
                        ant += "</span><br />";
                    }
                    
                    ant += "<a class='placar_jogo linkp' href='/2010/partida.asp?id=" + jogo.id + "' style='font-size: 10px;'>";
                    ant += jogo.hora + "&nbsp;-&nbsp;";
                    ant += jogo.jogo;
                    ant += "</a>";
                    ant += "<br />";
                }
                
                return ant;
            }
            
            // Preenchendo os campeonatos
            var all_html = "";
            for (var i = 0; i < campeonatos.length; i++) {
                var campeonato = campeonatos[i];
                var anteriores = campeonato.anteriores;
                var proximos = campeonato.proximos;
                var nome = campeonato.nome;
                
                if (nome.toLowerCase().indexOf("campeonato cearense") != -1) {
                    nome = campeonato.categoria;
                }
                
                var ant = createHtmlJogos(anteriores);
                var prox = createHtmlJogos(proximos);
                
                if (ant != "") {
                    ant = "<span style='color: #203c02; font-weight: bold;'>Jogos Anteriores</span><br />" + ant + "<br />";
                }
                
                if (prox != "") {
                    prox = "<span style='color: #203c02; font-weight: bold;'>Próximos Jogos</span><br />" + prox + "<br />";
                }
                
                var html = "" +
                        "<h3 style='font-size: 10px; font-family: arial; font-weight: bold; text-transform: uppercase;'><a href='#'>" + nome + "</a></h3>" +
                        "<div class='texto' style='font-size: 10px; font-family: arial; background: none; padding: 1em 1em;'>" + 
                            ant + 
                            prox + 
                        "</div>";
                
                // $(id).append(html);
                all_html = all_html + html;
            }
            
            $(id).append("<div id='accordion'>" + all_html + "</div>");
            $("#accordion").accordion({ clearStyle: true, collapsible: true });
        });
    
});