$(document).ready(function() {

	var $active = 1;
	var $pauseAction = 0;

	var stillTimer = 6000; //tempo de pausa na imagem
	var transTimer = 1000; //tempo de transição

	//Slider Function
	rotate = function(){
		$pauseAction = 1; //cancela ação do botao
		if ($active == 1) { //avança movimento
			var galeriaFaixaPosition = $(".galeria-faixa div:eq(0) img").width(); //Determines the distance the image reel needs to slide		//Slider Animation
			$(".galeria-faixa").animate({
				left: -galeriaFaixaPosition
			}, transTimer, function(){
				$(".galeria-faixa div:eq(0)").appendTo(".galeria-faixa");
				$('.galeria-faixa').css({"left":"0"});
				$pauseAction = 0; //libera ação do botão após animate
			});
		} else { //recua movimento
			var galeriaFaixaPosition = $(".galeria-faixa div:last img").width(); //Determines the distance the image reel needs to slide		//Slider Animation
			$(".galeria-faixa div:last").prependTo(".galeria-faixa");
			$('.galeria-faixa').css({"left": -galeriaFaixaPosition });
			$(".galeria-faixa").animate({
				"left": "0"
			}, transTimer, function(){
				$active = 1; //muda direção do movimento
				$pauseAction = 0; //libera ação do botão após animate
			});
		}
	}; 

	//Rotation and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer
			rotate(); //Trigger the paging and slider function
		}, stillTimer); //Timer speed in milliseconds
	};

	rotateSwitch(); //Run function on launch

	//On Hover image DIV
    $('.galeria-item').mouseenter(function(e) {
        $('a span',this).fadeIn(200);
		clearInterval(play); //Stop the rotation
    }).mouseleave(function(e) {
        $('a span',this).fadeOut(200);
		rotateSwitch(); //Resume rotation timer
    });
	
	//Nav Prev Next
	$("a.galeria-nav").click(function(){
		if ($pauseAction==0) {
			$active = $(this).attr('rel');
			clearInterval(play); //Stop the rotation
			rotate(); //Trigger rotation immediately
			rotateSwitch(); // Resume rotation timer
		}
		return false; //Prevent browser jump to link anchor
	});
	
});

