//------- REPOSITÓRIO DO OBJETO XML -------
var periodo= Array();
//-----------------------------------------
function atualizaComentarios(){
	var html="";
	$.ajax({
        url: config.SERVER_NAME+'ajax/aovivo.php',
        type: 'post',
        dataType:'xml',
        cache: false,
        async: false,
		data:{timestamp:$('#ultimaAlteracao').val(), idJogo:$('#idJogo').val()},
        error: function(XMLHttpRequest, textStatus, errorThrown){
            //alert('Ocorreu um problema, entre em contato com o setor de informática!');
        },
        success: function(response){
			//alert('teste');
			// CONTROLES ---------------------------------------------------------------------
			$('#timestamp').val($('timestamp',response).text());
			
			if(!$('jogo',response).text()){
				return false;
			}
			if($('inicioJogo',response)){
				$('#inicioJogo').val($('inicioJogo',response).text());
			}
			if($('ultimaAlteracao',response)){
				$('#ultimaAlteracao').val($('ultimaAlteracao',response).text());
			}
			//--------------------------------------------------------------------------------
			// GOLS --------------------------------------------------------------------------
			
			if(parseInt($('jogo',response).attr('golsRegularTime1'))){
				
				$('#golsRegularTime1').text($('jogo',response).attr('golsRegularTime1'));
			}else{
				
				$('#golsRegularTime1').text('0');
			}
			if(parseInt($('jogo',response).attr('golsRegularTime2'))){
				$('#golsRegularTime2').text($('jogo',response).attr('golsRegularTime2'));
			}else{
				$('#golsRegularTime2').text('0');
			}
			if(parseInt($('jogo',response).attr('golsPenalti1'))){
				$('#golsPenaltiTime1').text('('+$('jogo',response).attr('golsPenalti1')+')');
			}
			if(parseInt($('jogo',response).attr('golsPenalti2'))){
				$('#golsPenaltiTime2').text('('+$('jogo',response).attr('golsPenalti2')+')');
			}
			//-------------------------------------------------------------------------------
			// COMENTÁRIOS ------------------------------------------------------------------
			$('#ge-aovivo-comentarios').empty();
			var periodoAtualDesc='';
			$('periodo',response).each(function(){
					var html="";
					var p = this;
					if($('item',p).size()){
						html+='<li style="background-color:#E0E8EF; font:bold 12px Verdana, Arial, Helvetica, sans-serif;">'+$(p).children('descricao').text()+'</li>';				
						$('item',p).each(function(c){
							var i = this;
							c++;
							html+='<li  class="'+(c%2==0?'linha1':'linha2')+'">';
							html+='<span style="color:#004F00; font-weight:bold; font-size:10px;">'+parseInt($('minuto',i).text())+'m'+parseInt($('segundo',i).text())+'s: </span>';
							for(var y=0;y<this.attributes.length;y++){
								if(this.attributes[y].nodeValue =='on'){
									html+='<img src="'+config.SERVER_NAME+'imgs/aovivo/'+this.attributes[y].name+'.gif" width="15px" height="15px" />';
								}
							}
							html+='<span style="font:normal 12px Arial, Helvetica, sans-serif;">'+$('descricao',i).text()+'</span>';
							html+='</li>';
						});
						$('#ge-aovivo-comentarios').prepend(html);
						periodoAtualDesc = $(p).children('descricao').text();
						
					}
					
			});
			$('#periodoText').text(periodoAtualDesc);
			$('#ge-aovivo-comentarios').append(html);
			//-------------------------------------------------------------------------------
			// FICHA TÉCNICA
			html="";
			var arbitro = $('arbitro',response).children('nome').text();
			var arbitroNac = $('arbitro',response).children('nacionalidade').text();
			var local = $('local',response).text();
			
			html+='<div class="linha1" style="height:20px; padding:3px 0px 0px 5px; font:bold 12px Arial;"><span style="color:#0F487F;">Local:</span> '+local+'</div>';
			html+='<div class="linha1" style="height:20px; padding:0px 0px 0px 5px; font:bold 12px Arial;"><span style="color:#0F487F;">&Aacute;rbitro:</span> '+arbitro+' ('+arbitroNac+')</div>';
			
			$('equipe',response).each(function(i){
				var equipe = this;
				var tecnico = $('tecnico',equipe).text();
				if(i==0){ html+='<ul style="float:left; width:313px; border-right:1px solid #000;">'; 
					
				}else{ html+='<ul style="float:left; width:300px;">';}
				
				$('item',equipe).each(function(c){
					var j = this;
					//--
					var htmlSubs='';
					var htmlAttSubs="";
					$('substituto',j).each(function(){
						var s = this;
						for(var y=0;y < this.attributes.length;y++){
							if(this.attributes[y].nodeValue =='on'){
								htmlAttSubs+='<img src="'+config.SERVER_NAME+'imgs/aovivo/'+this.attributes[y].name+'.gif" style="margin-bottom:-3px;" width="15px" height="15px" />';
							}
						}
						htmlSubs+=' <img src="'+config.SERVER_NAME+'imgs/aovivo/substituicao.gif" style="margin-bottom:-3px;" /> <span>'+$(s).text()+htmlAttSubs+'</span>';
					});
					//--
					var htmlAttTitular = "";
					$('nome',j).each(function(){ // gambiarra por não retornar o objeto com os atributos
						for(var y=0;y < this.attributes.length;y++){
							if(this.attributes[y].nodeValue =='on'){
								htmlAttTitular+='<img src="'+config.SERVER_NAME+'imgs/aovivo/'+this.attributes[y].name+'.gif" width="15px" style="margin-bottom:-3px;" height="15px" />';
							}
						}
					});
					html+='<li class="linha'+(c%2)+'" style="font:bold 12px Arial;"><span style="font:bold 12px Arial; color:#0F487F; display:inline-block; width:15px;">'+(c+1)+'.</span> '+$('nome',j).text()+htmlAttTitular+htmlSubs+'</li>';
				});
				html+='<li class="linha1" style="font:bold 12px Arial;"><span style="color:#0F487F;">T&eacute;cnico:</span> '+tecnico+'</li>';
				html+='</ul>';
			});
			$('#ge-aovivo-ficha').empty().append(html);
			
			//===============================================================
			//alert($('jogo',response).attr('id'));
			
			/*$('#timestamp').val($('timestamp',response).text());
			if(!$('jogo',response).attr('id')){
				return false;
			}
			if($('ultimaAlteracao',response)){
				$('#ultimaAlteracao').val($('ultimaAlteracao',response).text());
			}
			if($('inicioJogo',response)){
				$('#inicioJogo').val($('inicioJogo',response).text());
			}
			//alert($('ultimaAlteracao',response).text());
			var periodos= Array('segundoTempoExtra','primeiroTempoExtra','segundoTempo','primeiroTempo');
			periodo['segundoTempo']=$('segundoTempo',response);
			periodo['primeiroTempo']=$('primeiroTempo',response);
			periodo['segundoTempoExtra']=$('segundoTempoExtra',response);
			periodo['primeiroTempoExtra']=$('primeiroTempoExtra',response);

			for(var i=0;i<4;i++){
				if($(periodo[periodos[i]]).children().size()){
					html+='<li style="background-color:#E0E8EF; font:bold 12px Verdana, Arial, Helvetica, sans-serif;">'+$(periodo[periodos[i]]).attr('descricao')+'</li>';
					$('comentario',periodo[periodos[i]]).each(function(c){ 	
						html+='<li  class="'+(c%2==0?'linha2':'linha1')+'">';
						html+='<span style="color:#004F00; font-weight:bold; font-size:10px;">'+$('minuto',this).text()+'m'+$('segundo',this).text()+'s: </span>';
						for(var y=0;y<this.attributes.length;y++){
							if(this.attributes[y].value=='on'){ // somente os habilitados
								html+='<img src="'+config.SERVER_NAME+'imgs/aovivo/'+this.attributes[y].name+'.gif" width="15px" height="15px" />';
							}
						}
						html+='<span style="font:normal 12px Arial, Helvetica, sans-serif;">'+$('descricao',this).text()+'</span>';
						html+='</li>';
					});
					   
				}
			}
			if(parseInt($('jogo',response).attr('golsRegularTime1'))){
				$('#golsRegularTime1').text($('jogo',response).attr('golsRegularTime1'));
			}else{
				$('#golsRegularTime1').text('0');
			}
			if(parseInt($('jogo',response).attr('golsRegularTime2'))){
				$('#golsRegularTime2').text($('jogo',response).attr('golsRegularTime2'));
			}else{
				$('#golsRegularTime2').text('0');
			}
			if(parseInt($('jogo',response).attr('golsPenalti1'))){
				$('#golsPenaltiTime1').text('('+$('jogo',response).attr('golsPenalti1')+')');
			}
			if(parseInt($('jogo',response).attr('golsPenalti2'))){
				$('#golsPenaltiTime2').text('('+$('jogo',response).attr('golsPenalti2')+')');
			}
			
			$('#ge-aovivo-comentarios').empty().append(html);*/
        }
    });
}
function atualizaFichaTecnica(){
	var html="";
	$.ajax({
        url: config.SERVER_NAME+'ajax/aovivo.php',
        type: 'post',
        dataType:'xml',
        cache: false,
        async: false,
		data:{timestamp:$('#ultimaAlteracao').val(), idJogo:$('#idJogo').val()},
        error: function(XMLHttpRequest, textStatus, errorThrown){
            //alert('Ocorreu um problema, entre em contato com o setor de informática!');
        },
        success: function(response){
			if(!response){
				return false;
			}
			$('#ultimaAlteracao').val($('ultimaAlteracao',response).text());
			$('#ge-aovivo-comentarios').empty().append(html);
        }
    });
}
//--
var minutosPassados;
function atualizaBarra(){
	if(parseInt($('#inicioJogo').val())){
		minutosPassados = Math.floor( (parseInt($('#timestamp').val()) - parseInt($('#inicioJogo').val()) )/60 );
		$('#minutos').val(minutosPassados);
		if(minutosPassados<45){
			$('#tempoNormal').width(minutosPassados*8);
			$('#tempoMinuto').text(minutosPassados+'m ');
		}else{
			$('#tempoExtra').show();
			$('#tempoNormal').width(385);
			$('#tempoMinuto').text('45m ');
		}
	}else{
		$('#tempoMinuto').text('00m ');
	}
}
//--
function reloadPage(){
	document.location.reload();
}
$(function(){
	//atualizaComentarios();
	//atualizaBarra();
	//var intervAtualiza = window.setInterval('atualizaComentarios()',5000);
	//var intervBarra    = window.setInterval('atualizaBarra()',1000);
	var _refreshAovivo = window.setInterval('reloadPage()',60000);

	//$('#ge-aovivo-comentarios').jScrollPane();
	$('#abaComentarios').click(function(){
		$(this).css({'background-color':'#E0E8EF','color':'#000'});
		$('#abaFicha').css({'background-color':'#000',"color":"#FFF"});
		$('#ge-aovivo-ficha').hide();
		$('#ge-aovivo-comentarios').show();
		
	});
	//	$('#periodoText').text('Primeiro Tempo');
	$('#abaFicha').click(function(){
		$(this).css({'background-color':'#E0E8EF','color':'#000'});
		$('#abaComentarios').css({'background-color':'#000',"color":"#FFF"});
		
		$('#ge-aovivo-ficha').show();
		$('#ge-aovivo-comentarios').hide();
	});
});
