/*
* jSlideShow4 by RCDMK - Maio de 2011
* rcdmk@hotmail.com
* 
* Plugin para jQuery que gera um slideshow ou banner rotativo
*/
(function($) {
	$.fn.jSlideShow4 = function(options) {
		// Inicialização
		var $this = $(this);
		var slides = $this.find(".slide");
		var opts = $.extend({}, $.fn.jSlideShow4.defaults, options);
		var currItem = 0;
		var mouseOver = false;
		var transitionIn = true;
		var timer = null;
		var buttons = null;
		var incluir = false;
		var over = false;
		var out = false;
		
		// Se tiver slides
		if (slides.length && slides.length > 1) {
			// Se tiver mais de um slide, inicia a animação
			
			slides.not(slides.eq(0)).css({ 'display': 'none' });
			
			// Criar botões de navegação
			if (opts.showButtons) {
				buttons = $this.find('.slideButtonsContainer');
				if (!buttons.length) incluir = true;
				
				if (incluir) buttons = $('<div class="slideButtonsContainer" />');
				
				
				for (var i = 0; i < slides.length; i++) {
					buttons.append('<div class="slideButton"><a style="cursor:pointer;" rel="' + (i + 1) + '">' + (i + 1) + '</a></div>');
				}
				
				// Ativar o click nos botões
				buttons.find(".slideButton").each(function() {
					$(this).click(function() {
						transitionSlide($(this).find('a').attr('rel'));
					});
				});
				
				if (incluir) $this.append(buttons);
			}
			
			slides.each(function() {
				$(this).hover(function() {
					over = true;
					out = false;
					
					if (opts.stopOnMouseOver) clearTimeout(timer);
					
				}, function() {
					over = false;
					out = true;
					
					if (opts.stopOnMouseOver) waitNext();
				});
			});
			
			activeButton(1);
			waitNext();
		}
		
		/*	Função que alterna os slides
		*	@num = número do slide para trocar
		* 	Se não passar um parâmetro, o próximo slide é chamado
		*/
		function transitionSlide(num) {
			// trocar slides
			over = false;
			out = false;
			
			clearTimeout(timer);
			if (num != '' && num != null && num!= undefined ) {
				
				num = parseInt(num) - 1;
				if (num != currItem) {
					$(slides[currItem]).animate(opts.animationOut, opts.animationTime, function() {
						$(slides[num]).animate(opts.animationIn, opts.animationTime, function() { waitNext() });
						currItem = num;
						activeButton(currItem + 1)
					});
				} else {
					waitNext();
					activeButton(1);
				}
				
			} else {
				
				$(slides[currItem]).animate(opts.animationOut, opts.animationTime, opts.easeOut, function() {
					currItem++;
					currItem = (currItem == slides.length)?0:currItem;
					activeButton(currItem + 1)
					$(slides[currItem]).animate(opts.animationIn, opts.animationTime, opts.easeIn, function() { waitNext() });
				});
			}
			
			
		}
		
		// Chamar a função após o tempo
		function waitNext() {
			clearTimeout(timer);
			if (!over) timer = setTimeout(function (){transitionSlide()}, (out) ? opts.interval/2 : opts.interval);
		}
		
		// Ativar o botão do slide atual
		function activeButton(currButton) {
			if (opts.showButtons) {
				buttons.find("a").each(function() {
					if ($(this).attr('rel') == String(currButton)) {
						$(this).addClass('slideButtonOn');
					} else {
						$(this).removeClass('slideButtonOn');
					}
				});
			}
		}
		
		return $this;
	}
	
	$.fn.jSlideShow4.defaults = {
		interval: 4000,
		animationIn: { opacity: 'show' },
		animationOut: { opacity: 'hide' },
		easeIn: "linear",
		easeOut: "linear",
		animationTime: 500,
		showButtons: true,
		stopOnMouseOver: true
	};
})(jQuery)

/*
$(document).ready(function(){
	$(".jSlideShow4").jSlideShow4();
	//$(".jSlideShow").jSlideShow({ interval: 2000, animationIn: { width: "show" }, animationOut: { width: "hide" }, easeIn:"bounce", easyOut:"bounce", showButtons: false });
});
*/
