$(function() {

	// Configurações padrões
	// ------------------------------------------------------------------------
	var quant = 0;
	var cur		= 0;
	var pageCurrent = 1;
	var speed = 5000;
	var mover = null;


	// Funções de inicilização
	// ------------------------------------------------------------------------
	function _IniciarSlide() {
		$("div.contaba").hide();
		$("div.contaba:first").show();
		//$("#tab-abas a:first").addClass("selected");
		$("#tab-abas a:first").addClass("over");

		$("#tab-abas a").each(function(){
			var $_id		= $(this).attr("href");
			var $_this	= $($_id).find('.showfotos');
			var $_obj		= $_this.find('.files');					//$(".files");
			var quant		= $_this.find('.files').size();		//$(".files").size();//$(".files").children().size();
			$_this.find('.files').hide(); //$(".files").hide();
			$_this.find('.files:first').show(); //$(".files:first").show();
		})


		// ini: ação no click da aba --------------------------------------------
		$("#tab-abas a").click(function(){			
			$("div.contaba").hide();
			$("#tab-abas a").removeClass("over");
			$(this).addClass("over");
			$($(this).attr("href")).show();

			var $_id = $(this).attr("href");
			quant				= $($_id).find('.files').size();
			cur = 0;			
			
			//_Reset();
			_stopPlay();
			if(quant > 1) _autoPlay( $(this) );
			_exibir( $(this) );
			return false;
		});// end: ação no click da aba


		// ini: início automático -----------------------------------------------
		var $_first		= $("#tab-abas a:first");
		var $_this		= $($_first.attr("href")).find('.showfotos')
		var $_obj			= $_this.find('.files');
		quant					= $_this.find('.files').size();

		if(quant > 1) _autoPlay( $_first );
//		 _mouseOver();	
	}// end : _Carregar


	// Resetando : para não se misturar com as outras animações
	// ------------------------------------------------------------------------
	function _Reset(){
		var cur		= -1;
		var quant = 0;
		var mover = null;
		clearInterval(mover);
	}// _mouseOver


	// Açõs no mouseOver - não está sendo utilizada 
	// ------------------------------------------------------------------------
	function _mouseOver(){
//		$pImagem.mouseover(function(){ _stopPlay(); return false; });
//		$pImagem.mouseout(function(){ _autoPlay(); return false; });
//		$pTitulo.mouseover(function(){ _stopPlay(); return false; });
//		$pTitulo.mouseout(function(){ _autoPlay(); return false; });
//		$pages.mouseover(function(){ _stopPlay(); return false; });
//		$pages.mouseout(function(){ _autoPlay(); return false; });
	}// _mouseOver


	// Ir para o próximo item
	// ------------------------------------------------------------------------
	function _proximo(){
		$("#proximo").click(function(){
			if( cur < ( tot - 1) ) {
				pageCurrent++;
				cur++; 
			} else {
				pageCurrent = 1;
				cur = 0;
			}
			_exibir();
			/*
			$pImagem.fadeTo("fast", 0.1, function(){
				$pImagem.html( im.imagem[cur] ).fadeTo("fast", 1);
			});
			$pTitulo = $("#destaque1-esq-desc-txt");
			$pTitulo.fadeTo("fast", 0.1,function(){
				$pTitulo.html("<h1>"+  im.titulo[cur] +"</h1><p>" + im.legenda[cur] +"</p>").fadeTo("fast", 1);
			});
			$pages.html( pageCurrent +"/"+ tot);
			*/
			return false;
		});// end : proximo : click
		$("#proximo").find('img').mouseover(function(){
			$(this).attr("src", "imgs/seta_direita_o.gif");
			_stopPlay();
			return false;
		});
		$("#proximo").find('img').mouseout(function(){
			$(this).attr("src", "imgs/seta_direita.gif");
			_autoPlay();
			return false;
		});	
	}// _proximo


	// Voltar para o item anterior
	// ------------------------------------------------------------------------
	function _anterior(){
		$("#anterior").click(function(){
			if( cur <= 0 ) {
				cur = tot - 1;
				pageCurrent = tot; 
			} else {
				pageCurrent--;
				cur--;
			}
			_exibir();
		});// end : anterior : click
		$("#anterior").find('img').mouseover(function(){
			$(this).attr("src", "imgs/seta_esquerda_o.gif");
			_stopPlay();
			return false;
		});
		$("#anterior").find('img').mouseout(function(){
			$(this).attr("src", "imgs/seta_esq_branca.gif");
			_autoPlay();
			return false;
		});	
	}// _anterior


	// Exibir o item atual
	// ------------------------------------------------------------------------
	function _exibir(_t){
		var $_id		= _t.attr("href");
		var $_this	= $($_id).find('.showfotos');
		$_this.find('.files').hide();

		var $_obj = $_this.find('.files');
		var $next = $_obj.filter('.files:eq('+ (cur) +')');

		$next.show();
    $next.css({opacity: 0.0})
			.animate({opacity: 1.0}, 2000, function() {
				//$active.removeClass('active last-active');
			});
		return false;	
	}// _exibir


	// Auto Play - fica alternando automaticamente
	// ------------------------------------------------------------------------
	function _autoPlay(_t){
		if(quant<=1) {
			_stopPlay();
			return false;
		}

		clearInterval(mover); 
		mover = setInterval(function(){ 
			if( cur < ( quant - 1) ) {
				cur++; 
			} else {
				cur = 0;
			}
			_exibir(_t);
		},speed);
	}// _autoPlay


	// Stop Play - parar a animação atual
	// ------------------------------------------------------------------------
	function _stopPlay(){
		clearInterval(mover);
	}// _stopPlay


	// Chamando o início de tudo
	// ------------------------------------------------------------------------
	_IniciarSlide();
	
});
