﻿function loadRounds(param)
{
	url = "/viewRounds.html";
	place =  "listRounds";

	document.getElementById(place).innerHTML="<div class=\"carregando\">Carregando...</div>";
  getHTML(url, param, place, reportError);
}

function showRounds(idtChampionship)
{
	url = "/viewRounds.html";
	param = "idtChampionship="+idtChampionship+"&maxPerPage=999";
	place =  "listRounds";
	
	document.getElementById(place).innerHTML="<div class=\"carregando\">Carregando...</div>";
  getHTML(url, param, place, reportError);
}

var idtRoundOLD=0;
var idNumberOLD=0;
function loadMatches(idtChampionship, idtRound, idNumber)
{
	if(idtRound != null)
	{

    url = "/viewMatches.html";
		param = "idtChampionship="+idtChampionship+"&idtChampRound="+idtRound;
		place = "round_"+idtRound;


    if(idtRoundOLD!=idtRound){
      if(document.getElementById('round_'+idtRoundOLD)){
        document.getElementById('rodada_tr_'+idNumberOLD).className=document.getElementById('rodada_tr_'+idNumberOLD).className.replace("aberto","fechado");
        if(document.getElementById('round_'+idtRoundOLD).getElementsByTagName('table').length > 0)
          document.getElementById('round_'+idtRoundOLD).removeChild(document.getElementById('round_'+idtRoundOLD).getElementsByTagName('table')[0]);
  		}
      document.getElementById('rodada_tr_'+idNumber).className=document.getElementById('rodada_tr_'+idNumber).className.replace("fechado","aberto");
  		document.getElementById(place).innerHTML="<div class=\"carregando\">Carregando...</div>";
      getHTML(url, param, place, reportError);
    }else if(idtRoundOLD==idtRound){
      if(document.getElementById('round_'+idtRoundOLD).getElementsByTagName('table').length > 0){
        document.getElementById('round_'+idtRoundOLD).removeChild(document.getElementById('round_'+idtRoundOLD).getElementsByTagName('table')[0]);
        document.getElementById('rodada_tr_'+idNumber).className=document.getElementById('rodada_tr_'+idNumber).className.replace("aberto","fechado");
        if(typeof(setHeight)!="undefined"){
          if(document.getElementById('corpo')) document.getElementById('corpo').style.height='';
          if(document.getElementById('col-esquerda')) document.getElementById('col-esquerda').style.height='';
          if(document.getElementById('col-centro')) document.getElementById('col-centro').style.height='';
          setTimeout("setHeight('corpo','col-esquerda','col-centro');",100);
        }
      }else{
    		document.getElementById('rodada_tr_'+idNumber).className=document.getElementById('rodada_tr_'+idNumber).className.replace("fechado","aberto");
        document.getElementById(place).innerHTML="<div class=\"carregando\">Carregando...</div>";
        getHTML(url, param, place, reportError);
      }
    }

	  idtRoundOLD=idtRound;
    idNumberOLD=idNumber;
  }
}

function getHTML(url, pars, placeholder, scriptFailure)
{
	var ajaxUpdater = new Ajax.Updater( {success: placeholder}, url, { method: 'get', parameters: pars, onFailure: scriptFailure });
}

function reportError(request)
{
	alert('Erro ao carregar p?gina');
}