var $xml;
var $quantidade_depoimentos;
var $depoimento_atual;
var $proximo_depoimento;
var $depoimento_anterior;
function eventos(){	
	// Colocando no cache as imagens dos depoimentos =D
	imagemIsmael = new Image();
	imagemIsmael.src = 'fotos/depoimentos/ismael_zorzi.jpg';
	imagemMarcia = new Image();
	imagemMarcia.src = 'fotos/depoimentos/marcia_ferronato.jpg';
	imagemNeivete = new Image();
	imagemNeivete.src = 'fotos/depoimentos/neivete_possamai.jpg';
	imagemRodolfo = new Image();
	imagemRodolfo.src = 'fotos/depoimentos/rodolfo_costa.jpg';
	imagemTarcisio = new Image();
	imagemTarcisio.src = 'fotos/depoimentos/tarcisio_michelon.jpg';

	// Buscando os depoimentos
	$.ajax({
		type: "POST",
		cache: false,
		url: "xml/depoimentos.xml",
		success: function($retorno){
			chamar_depoimento_aleatorio($retorno);
		}
	});
	$('.proximo_depoimento').click(function(){
		if($proximo_depoimento){
			trocar_depoimento($proximo_depoimento);
		}
		return false;
	})
	$('.depoimento_anterior').click(function(){
		if($depoimento_anterior){
			trocar_depoimento($depoimento_anterior);
		}
		return false;
	})
}
function chamar_depoimento_aleatorio($retorno){
	$xml = $retorno;
	$quantidade_depoimentos = $xml.getElementsByTagName('depoimento').length;
	$id_depoimento = Math.ceil(Math.random()*$quantidade_depoimentos);
	trocar_depoimento($id_depoimento);
}
function trocar_depoimento($id_depoimento){
	$depoimento_atual = $id_depoimento;
	$raiz_busca = $xml.getElementsByTagName('depoimento');
	for($i=0;$i<$raiz_busca.length;$i++){
		$id_depoimento_atual = $raiz_busca[$i].getElementsByTagName('id_depoimento')[0].childNodes[0].nodeValue;
		if($id_depoimento_atual == $id_depoimento){
			$foto = $raiz_busca[$i].getElementsByTagName('foto')[0].childNodes[0].nodeValue;
			$nome = $raiz_busca[$i].getElementsByTagName('nome')[0].childNodes[0].nodeValue;
			$cargo = $raiz_busca[$i].getElementsByTagName('cargo')[0].childNodes[0].nodeValue;
			$descricao = $raiz_busca[$i].getElementsByTagName('descricao')[0].childNodes[0].nodeValue;
			$identification = '<strong>'+$nome+'</strong><br />'+$cargo+'<br />';
			$('.identification').hide();
			$('.testimonial').hide();
			$('.identification').attr('innerHTML', $identification);
			$('.testimonial').attr('innerHTML', $descricao);
			$('.identification').fadeIn('slow');
			$('.testimonial').fadeIn('slow');
			$('.image_testimonial').attr('src', $foto);
		}
	}
	if($id_depoimento == $quantidade_depoimentos){
		$('.proximo_depoimento').addClass('selected');
		$proximo_depoimento = false;
	} else{
		$('.proximo_depoimento').removeClass('selected');
		$proximo_depoimento = $id_depoimento+1;
	}
	
	if($id_depoimento == '1'){
		$('.depoimento_anterior').addClass('selected');
		$depoimento_anterior = false;
	} else{
		$('.depoimento_anterior').removeClass('selected');
		$depoimento_anterior = $id_depoimento-1;
	}
}
s.addListener(window, 'load', eventos);