var mouse=false;
$(document).ready(function(){
	// Loading
	$('#B86-imagem a').css('display','block');

	$("#B86carousel-list li").animate({"opacity": 0.4},1);
	$("#B86carousel-list li.active").animate({"opacity": 1},1);

	
	function altera_imagem(src, link){
		/*
		  Retirada para a transição ficar mais suave. Antes a imagem era trocada com tudo. Agora ficou suave.
		*/
		//$("#B86-imagem img").remove();
		img = new Image();
		$(img)
			.load(function () {
				$(this).hide();
				$('#B86-imagem a').append(this);
				$('#B86-imagem a').attr("href", link);
				$(this).fadeIn(1000);
			})
		.error(function () {})
		.attr('src', src);
	}
	/*function altera_texto(titulo, descricao, link){
		//Não está sendo usado
		$("#B86-descricao h3 a").text(titulo);
		$("#B86-descricao p a").text(descricao);
		$("#B86-descricao a").attr("href", link)
	}*/

	$.interval(function(){	
		// plugin timeout
		// se o usuário não estiver com o mouse sob algum li
		if(mouse==false){
			// objeto recebe o elemento li ativo
			$curr = $("#B86carousel-list li.active");
			// verifica se o elemento corrente é o último
			// afim de corrigir a reinicialização
			if($curr.html() == $("#B86carousel-list li:last").html()){
				$next = $("#B86carousel-list li:first");
			}else{
				$next = $("#B86carousel-list li.active").next();
			}
			// variavel recebe o elemento src da proxima imagem
			next_src = $next.find("img").attr("src");
			next_link = $next.find("a").attr("href");
			next_titulo = $next.find("a").text();
			next_descricao = $next.find("a").attr("title");
			altera_imagem(next_src, next_link);
			//altera_texto(next_titulo, next_descricao, next_link);
			$curr.toggleClass("active").animate({"opacity": 0.3},1000);
			$next.toggleClass("active").animate({"opacity": 1},1000);
			
			//$curr.toggleClass("active").css("background","none");
			//$next.toggleClass("active").css("background","red");
		}
	}, 10000);

	$("#B86carousel-list li").mouseover(function(){
		mouse=true;
		$("#B86carousel-list li").removeClass("active");
		$curr = $(this);
		next_src = $curr.find("img").attr("src");
		next_titulo = $curr.find("a").text();
		next_descricao = $curr.find("a").attr("title");
		next_link = $curr.find("a").attr("href");
		altera_imagem(next_src, next_link);
		//altera_texto(next_titulo, next_descricao, next_link);
		$curr.addClass("active").animate({"opacity": 1},1000);
		//$curr.addClass("teste");
	});
	$("#B86carousel-list li").mouseout(function(){
		mouse=false;
		$curr = $(this);
		//$curr.removeClass("teste");
		$curr.animate({"opacity": 0.3},500);
	});
	$("#B86carousel a").mouseover(function(){
		mouse=true;
	});
	$("#B86carousel a").mouseout(function(){
		mouse=false;
	});
});
