/*
* jSlideShow 2 by RCDMK - Janeiro de 2011
* rcdmk@hotmail.com
* 
* Plugin para jQuery que gera um slideshow ou banner rotativo
*/
(function($) {
	$.fn.jSlideShow2 = function(options) {
		// Inicialização
		var $this = $(this);
		var slides = $this.find(".slide");
		var titles = [];
		var opts = $.extend({}, $.fn.jSlideShow2.defaults, options);
		var currItem = 0;
		var mouseOver = false;
		var transitionIn = true;
		var timer = null;
		var mainWidth = opts.width;
		var mainHeight = opts.height;
		
		titles = $this.find(".title");
		
		$this.css({ width: mainWidth + "px", height: mainHeight + "px", backgroundColor: opts.fadeColor });
		
		// Se tiver slides
		if (slides.length) {
			// Se tiver mais de um slide, inicia a animação
			slides.not(slides.eq(0)).css({ 'display': 'none', '_display' : '', '_visibility' : 'hidden' });
			titles.not(slides.eq(0)).css({ 'display': 'none', '_display' : '', '_visibility' : 'hidden' });
			
			setTimeout(transitionSlide, opts.startTime);
		}
		
		// Função que alterna os slides
		function transitionSlide() {
			// trocar slides
			if (opts.showTitle) {
				var $title = $(titles[currItem]);
				$title.css({ top: (mainHeight + 1) + "px", left: 0, display: "block" })
				.animate({ top: (mainHeight - $title.outerHeight()) + "px" }, Math.round(opts.animationTimeIn * .5), "linear", function() { if (slides.length > 1) $(this).delay(opts.animationTime).animate({ top: (mainHeight + 1) + "px" }, Math.round(opts.animationTimeIn * .5), "linear", function() { $(this).css({ display: "none" })}); });
			} else {
				//titles.hide();
			}
			
			
			var $slide = $(slides[currItem]);
			var width = $slide.innerWidth();
			var height = $slide.innerHeight();
			
			var minWidth = mainWidth - width;
			var minHeight = mainHeight - height;
			
			$slide.css({ left: Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px"), top: 0, visibility: 'visible' })
			.animate({ opacity: "show" }, opts.animationTimeIn, "linear", function() {
				$(this).animate({ left: 0 }, { queue: false, duration: Math.round(opts.animationTime * .75) }, "linear", function() { $(this).animate({ left: Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px") }, { queue: true, duration: Math.round(opts.animationTime * .5) }, "linear") })
				.animate({ top: Math.min(0, minHeight) + ((Math.min(0, minHeight) == 0) ? "" : "px") }, opts.animationTime, "linear", function() {
					if (slides.length > 1) {
						$(this).animate({ opacity: "hide" }, opts.animationTimeIn, "linear", function() {
							currItem++;
							currItem = (currItem == slides.length) ? 0 : currItem;
							
							if (opts.showTitle) {
								var $title2 = $(titles[currItem]);
								$title2.css({ top: (mainHeight + 1) + "px", left: 0, display: "block" })
								.animate({ top: (mainHeight - $title2.outerHeight()) + "px" }, Math.round(opts.animationTimeIn * .5), "linear", function() { if (slides.length > 1) $(this).delay(opts.animationTime).animate({ top: (mainHeight + 1) + "px" }, Math.round(opts.animationTimeIn * .5), "linear", function() { $(this).css({ display: "none" })}); });
							}
	
							var $slide2 = $(slides[currItem]);
							width = $slide2.outerWidth();
							height = $slide2.outerHeight();
							
							
							$slide2.css({ left: Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px"), top: Math.min(0, minHeight) + ((Math.min(0, minHeight) == 0) ? "" : "px"), visibility: 'visible' })
							.animate({ opacity: "show" }, opts.animationTimeIn, "linear", function() {
								$(this).animate({ left: 0 }, { queue: false, duration: Math.round(opts.animationTime * .75) }, "linear", function() { $(this).animate({ left: Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px") }, { queue: true, duration: Math.round(opts.animationTime * .5) }, "linear") })
								.animate({ top: 0 }, opts.animationTime, "linear", function() {
									$(this).animate({ opacity: "hide" }, opts.animationTimeIn, "linear", transitionSlide);
								});
							});
							currItem++;
							currItem = (currItem == slides.length) ? 0 : currItem;
						});
					}
				});
			});
		}

		return $this;
	}
	
	$.fn.jSlideShow2.defaults = {
		width: 720,
		height: 422,
		animationTime: 1000,
		animationTimeIn: 500,
		startTime: 500,
		titleTime: 1000,
		fadeColor: "#ffffff",
		showTitle: true
	};
})(jQuery)

//$(document).ready(function(){
	//$(".jSlideShow2").jSlideShow2();
	//$(".jSlideShow2").jSlideShow2({ width: 640, height: 300, animationTime: 8000, animationTimeIn: 2000, startTime: 5000, titleTime: 2000, fadeColor: "#FFFFFF"});
//});
