aJogadores = new Array();
aBusca = new Array();
nJog1 = 0;
nJog2 = 0;
firstTime = true;

function BuscaJogador()
{
	var busca1 = document.FrmCompara.jogador1.value;
	var busca2 = document.FrmCompara.jogador2.value;

	if ((busca1.length <= 2) || (busca2.length <= 2))
	{
		alert("Ambos jogadores devem conter no mímino 2 caracteres");
		return;
	}

	doBuscando();

	var buscar = new ServerRequest();
	buscar.onComplete = function(responseText,responseXML)
	{
		aBusca = eval('(' + responseText + ')');
		
		montaResultados();
	}
	buscar.submit("/futebol/gestor/quemequem/statsindividual_106_atletas.js");
}

function montaResultados()
{
	document.getElementById("buscando").style.display = 'none';
	
	var busca1 = document.FrmCompara.jogador1.value.toLowerCase();
	var busca2 = document.FrmCompara.jogador2.value.toLowerCase();
	
	
	var aResults = new Array();
	aResults[1] = new Array();
	aResults[2] = new Array();
	
	for (id in aBusca)
	{
		eval("nome = aBusca."+id);
		
			
		if (nome.unescapeHTML().toLowerCase().replaceAccent().indexOf(busca1.replaceAccent()) != -1)
		{
			size = aResults[1].length;
			
			aResults[1][size] = new Object;
			aResults[1][size].id = id.replace("id_","");
			aResults[1][size].nome = nome.unescapeHTML();
		}

		if (nome.unescapeHTML().toLowerCase().replaceAccent().indexOf(busca2.replaceAccent()) != -1)
		{
			size = aResults[2].length;
			
			aResults[2][size] = new Object;
			aResults[2][size].id = id.replace("id_","");
			aResults[2][size].nome = nome.unescapeHTML();
		}
	}
	
	var RESULT1 = "";
	RESULT1  += "<select id=select_lista name=result1 size=5>";
	for (index in aResults[1])
		RESULT1 += "<option value='"+aResults[1][index].id+"' "+((aResults[1].length==1) ? 'selected': '')+">"+aResults[1][index].nome+"</option>";
	RESULT1 += "</select>";

	var RESULT2 = "";
	RESULT2  += "<select id=select_lista name=result2 size=5>";
	for (index in aResults[2])
		RESULT2 += "<option value='"+aResults[2][index].id+"' "+((aResults[2].length==1) ? 'selected': '')+">"+aResults[2][index].nome+"</option>";
	RESULT2 += "</select>";
	
	if ((aResults[1].length == 1) && (aResults[2].length == 1))
	{
		comparaJogadores(aResults[1][0].id,aResults[2][0].id);
		if (!firstTime)
		{
			document.getElementById("resultado1").innerHTML = RESULT1;
			document.getElementById("resultado2").innerHTML = RESULT2;
		}
	}
	else
	{
		document.getElementById("resultado1").innerHTML = RESULT1;
		document.getElementById("resultado2").innerHTML = RESULT2;
  	
		document.getElementById("resultados_busca").style.display = '';	
		
		firstTime = false;
	}
} 	

function doBuscando()
{
	if (firstTime)
	{
		document.getElementById("buscando").style.display = '';
	}
	else
	{
		var RESULT1 = "<select id=select_lista name=result1 size=5 disabled>";
		RESULT1 += "<option value=0>Buscando...</option>";
		RESULT1 += "</select>";
		var RESULT2 = "<select id=select_lista name=result1 size=5 disabled>";
		RESULT2 += "<option value=0>Buscando...</option>";
		RESULT2 += "</select>";
		document.getElementById("resultado1").innerHTML = RESULT1;
		document.getElementById("resultado2").innerHTML = RESULT2;
	}
}

function comparaJogadores(nAtleta1,nAtleta2)
{
	if ((typeof(nAtleta1) != "undefined") && (typeof(nAtleta2) != "undefined"))
	{
		jog1 = nAtleta1;
		jog2 = nAtleta2;
	}
	else
	{
		if ((document.FrmCompara.result1.selectedIndex == -1) || (document.FrmCompara.result2.selectedIndex == -1))
		{
			alert("Escolha 2 jogadores para serem comparados");
			return;
		}
		
		jog1 = document.FrmCompara.result1.value;
		jog2 = document.FrmCompara.result2.value;
	}
	
	if ((nJog1 == jog1) && (nJog2 == jog2))
		return;

	nJog1 = jog1;
	nJog2 = jog2;
	
	jogador1 = new ServerRequest();
	jogador1.onComplete = function(responseText,responseXML)
	{
		aJogadores[1] = eval('(' + responseText + ')');

		jogador2 = new ServerRequest();
		jogador2.onComplete = function(responseText,responseXML)
		{
			aJogadores[2] = eval('(' + responseText + ')');
			montaComparacao();
		}
		jogador2.submit("/futebol/gestor/quemequem/statsindividual_106_"+jog2+"_detalhes.js");
	}
	jogador1.submit("/futebol/gestor/quemequem/statsindividual_106_"+jog1+"_detalhes.js");
}

function montaComparacao()
{
	//if (typeof(aJogadores[1].desarmes_total) == "undefined")
		//aJogadores[1] = {'dribles_certos':'0', 'dribles_errados':'0', 'dribles_total':'0', 'passes_certos':'0', 'passes_certos_defesa':'0', 'passes_certos_ataque':'0', 'passes_errados':'0', 'passes_errados_defesa':'0', 'passes_errados_ataque':'0', 								'passes_total':'0','passes_total_defesa':'0','passes_total_ataque':'0','desarmes_completos':'0','desarmes_incompletos':'0','desarmes_bolarecuperada':'0','desarmes_total':'0','faltas_recebidas':'0','faltas_cometidas':'0','bolas_perdidas':'0','recuos':'0','assistencias':'0','finalizacoes_certas':'0','finalizacoes_trave':'0','finalizacoes_erradas':'0','finalizacoes_total':'0','escanteios_cedidos':'0','escanteios_conquistados':'0','cruzamentos_certos':'0','cruzamentos_certos_intermediaria':'0','cruzamentos_certo_linhadefundo':'0','cruzamentos_errados':'0','cruzamentos_errados_intermediaria':'0','cruzamentos_errados_linhadefundo':'0','cruzamentos_total':'0','cruzamentos_total_intermediaria':'0','cruzamentos_total_linhadefundo':'0','lancamentos_certos':'0','lancamentos_errados':'0','lancamentos_total':'0','impedimentos':'0','bolasrecebidas':'0','jogadas_linhadefundo':'0','viradasdejogo':'0','golsmarcados':'0','cartoes_amarelos':'0','cartoes_vermelhos':'0','totaldeatuacoes':'0','tempodeparticipacao':'0'};

	//if (typeof(aJogadores[2].desarmes_total) == "undefined")
		//aJogadores[2] = {'dribles_certos':'0', 'dribles_errados':'0', 'dribles_total':'0', 'passes_certos':'0', 'passes_certos_defesa':'0', 'passes_certos_ataque':'0', 'passes_errados':'0', 'passes_errados_defesa':'0', 'passes_errados_ataque':'0', 								'passes_total':'0','passes_total_defesa':'0','passes_total_ataque':'0','desarmes_completos':'0','desarmes_incompletos':'0','desarmes_bolarecuperada':'0','desarmes_total':'0','faltas_recebidas':'0','faltas_cometidas':'0','bolas_perdidas':'0','recuos':'0','assistencias':'0','finalizacoes_certas':'0','finalizacoes_trave':'0','finalizacoes_erradas':'0','finalizacoes_total':'0','escanteios_cedidos':'0','escanteios_conquistados':'0','cruzamentos_certos':'0','cruzamentos_certos_intermediaria':'0','cruzamentos_certo_linhadefundo':'0','cruzamentos_errados':'0','cruzamentos_errados_intermediaria':'0','cruzamentos_errados_linhadefundo':'0','cruzamentos_total':'0','cruzamentos_total_intermediaria':'0','cruzamentos_total_linhadefundo':'0','lancamentos_certos':'0','lancamentos_errados':'0','lancamentos_total':'0','impedimentos':'0','bolasrecebidas':'0','jogadas_linhadefundo':'0','viradasdejogo':'0','golsmarcados':'0','cartoes_amarelos':'0','cartoes_vermelhos':'0','totaldeatuacoes':'0','tempodeparticipacao':'0'};
	
	
	HTML = '<div id="titBuscaC">MÉDIA DOS JOGOS DISPUTADOS</div>';
	HTML += '<div id="subtitBuscaC"><span id=nome1>'+aJogadores[1].apelido.unescapeHTML()+'</span><span id=nome2>'+aJogadores[2].apelido.unescapeHTML()+'</span></div>';
	HTML += '<table width=424 cellpadding=0 cellspacing=0 border=0 align=center id=tblComparacao>';
	HTML += '<tr>';
	HTML += '	<td colspan=2 rowspan=11 class=jogador1>';
	HTML += '		<img src="'+aJogadores[1].foto_p+'" title=Reuters><br>';
	HTML += '		<div id="jogador"></div>';
	HTML += '		<div id="time">'+aJogadores[1].nome_comum_equipe.unescapeHTML()+'</div>';
	HTML += '		<div id="time">'+aJogadores[1].posicao+'</div>';
	HTML += '	</td>';
	HTML += '	<th>'+aJogadores[1].numjogos+'</th>';
	HTML += '	<td class="fundamento">Jogos</td>';
	HTML += '	<th>'+aJogadores[2].numjogos+'</th>';
	HTML += '	<td colspan=2 rowspan=11 class=jogador2>';
	HTML += '		<img src="'+aJogadores[2].foto_p+'" title=Reuters><br>';
	HTML += '		<div id="time">'+aJogadores[2].nome_comum_equipe.unescapeHTML()+'</div>';
	HTML += '		<div id="time">'+aJogadores[2].posicao+'</div>';
	HTML += '	</td>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].desarmes_total)+'</th>';
	HTML += '	<td class="fundamento">Desarmes</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].desarmes_total)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].faltas_recebidas)+'</th>';
	HTML += '	<td class="fundamento">Faltas recebidas</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].faltas_recebidas)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].faltas_cometidas)+'</th>';
	HTML += '	<td class="fundamento">Faltas cometidas</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].faltas_cometidas)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].bolasrecebidas)+'</th>';
	HTML += '	<td class="fundamento">Bolas recebidas</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].bolasrecebidas)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].bolas_perdidas)+'</th>';
	HTML += '	<td class="fundamento">Bolas perdidas</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].bolas_perdidas)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].jogadas_linhadefundo)+'</th>';
	HTML += '	<td class="fundamento">Jogadas de linha de fundo</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].jogadas_linhadefundo)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].tempojogado)+'</th>';
	HTML += '	<td class="fundamento">Tempo jogado (min)</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].tempojogado)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].golstotal)+'</th>';
	HTML += '	<td class="fundamento">Gols marcados</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].golstotal)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].camarelo_total)+'</th>';
	HTML += '	<td class="fundamento">Cartões amarelos</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].camarelo_total)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<th>'+formatNum(aJogadores[1].cvermelho_total)+'</th>';
	HTML += '	<td class="fundamento">Cartões vermelhos</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].cvermelho_total)+'</th>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<td width="43">Errados</td>';
	HTML += '	<td width="43">Certos</td>';
	HTML += '	<th width="43">Total</th>';
	HTML += '	<td width="189" style="background:#C7D4DC; height:2em;" >&nbsp;</td>';
	HTML += '	<th width="43">Total</th>';
	HTML += '	<td width="43">Certos</td>';
	HTML += '	<td width="43">Errados</td>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<td>'+formatNum(aJogadores[1].finalizacoes_erradas)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[1].finalizacoes_certas)+'</td>';
	HTML += '	<th>'+formatNum(aJogadores[1].finalizacoes_total)+'</th>';
	HTML += '	<td class="fundamento">Finalizações</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].finalizacoes_total)+'</th>';
	HTML += '	<td>'+formatNum(aJogadores[2].finalizacoes_certas)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[2].finalizacoes_erradas)+'</td>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<td>'+formatNum(aJogadores[1].cruzamentos_errados )+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[1].cruzamentos_certos)+'</td>';
	HTML += '	<th>'+formatNum(aJogadores[1].cruzamentos_total)+'</th>';
	HTML += '	<td class="fundamento">Cruzamentos</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].cruzamentos_total)+'</th>';
	HTML += '	<td>'+formatNum(aJogadores[2].cruzamentos_certos)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[2].cruzamentos_errados)+'</td>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<td>'+formatNum(aJogadores[1].lancamentos_errados)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[1].lancamentos_certos)+'</td>';
	HTML += '	<th>'+formatNum(aJogadores[1].lancamentos_total)+'</th>';
	HTML += '	<td class="fundamento">Lançamentos</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].lancamentos_total)+'</th>';
	HTML += '	<td>'+formatNum(aJogadores[2].lancamentos_certos)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[2].lancamentos_errados)+'</td>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<td>'+formatNum(aJogadores[1].dribles_errados)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[1].dribles_certos)+'</td>';
	HTML += '	<th>'+formatNum(aJogadores[1].dribles_total)+'</th>';
	HTML += '	<td class="fundamento">Dribles</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].dribles_total)+'</th>';
	HTML += '	<td>'+formatNum(aJogadores[2].dribles_certos)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[2].dribles_errados)+'</td>';
	HTML += '</tr>';
	HTML += '<tr>';
	HTML += '	<td>'+formatNum(aJogadores[1].passes_errados)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[1].passes_certos)+'</td>';
	HTML += '	<th>'+formatNum(aJogadores[1].passes_total)+'</th>';
	HTML += '	<td class="fundamento">Passes</td>';
	HTML += '	<th>'+formatNum(aJogadores[2].passes_total)+'</th>';
	HTML += '	<td>'+formatNum(aJogadores[2].passes_certos)+'</td>';
	HTML += '	<td>'+formatNum(aJogadores[2].passes_errados)+'</td>';
	HTML += '</tr>';
	HTML += '</table>';
	
	document.getElementById("resultComparacao").innerHTML = HTML;
}

String.prototype.unescapeHTML = function() 
{
    var div = document.createElement('div');
    div.innerHTML = this.stripTags();
    return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
}
String.prototype.stripTags =  function() 
{
    return this.replace(/<\/?[^>]+>/gi, '').replace(/\&apos;/g,"'");
}

function formatNum(szNum)
{
	if (typeof(szNum) == "undefined") return 0;
	if (szNum == "") return 0;
	
	aNum = szNum.split(".");
	if (aNum[1] == 0)
		return aNum[0];
	else
		return szNum.replace(".",",");
}	

String.prototype.replaceAccent = function()
{
	// Transforma a acentuação
	Accent = /[ÂÁÀÃÊÉÈÎÍÌÔÓÒÕÛÚÙÜÇÑ]/g;
	reA = /[ÂÁÀÃ]/g;
	reE = /[ÊÉÈ]/g;
	reI = /[ÎÍÌ]/g;
	reO = /[ÔÓÒÕ]/g;
	reU = /[ÛÚÙÜ]/g;
	reC = /[Ç]/g;
	reN = /[Ñ]/g;
	
	//Substitui caracteres acentuados em "Seu Nome"
	upNome = this.toUpperCase();
	if(upNome.search(Accent)!=-1) 
	{
		upNome = upNome.replace(reA,"A");
		upNome = upNome.replace(reE,"E");
		upNome = upNome.replace(reI,"I");
		upNome = upNome.replace(reO,"O");
		upNome = upNome.replace(reU,"U");
		upNome = upNome.replace(reC,"C");
		upNome = upNome.replace(reN,"N");
	}
	
	return upNome;
}

function getParser(_szURL)
{
	d=_szURL.substring(_szURL.indexOf("?")+1);
	c=v=new Array();
	c=d.split("&");
	for(i=0;i<c.length&&c.length>0;i++) 
	{
		v=c[i].split("=");
		if(v.length>1)eval(v[0]+"='"+v[1]+"'");
	}
}
getParser(location.href);

onload = function()
{
	if ((atleta1 != 0) && (atleta2 != 0))
	{
		comparaJogadores(atleta1,atleta2);
	}
}