/*
* 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
			var outrosSlides = slides.not(slides.eq(0));
			
			outrosSlides.css({ 'display': 'none', '_display' : '', '_visibility' : 'hidden' });
			titles.not(slides.eq(0)).css({ 'display': 'none', '_display' : '', '_visibility' : 'hidden' });
			
			if (opts.hDirection == "left") {
				outrosSlides.css({ right: 0 });
			} else {
				outrosSlides.css({ left: 0 });
			}
			
			if (opts.vDirection == "bottom") {
				outrosSlides.css({ top: 0 });
			} else {
				outrosSlides.css({ bottom: 0 });
			}
			
			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(0);
			}
			
			
			var $slide = $(slides[currItem]);
			var $slideImg = $slide.find("img");
			var width = ($slideImg.length) ? $slideImg.innerWidth() : $slide.innerWidth();
			var height = ($slideImg.length) ? $slideImg.innerHeight() : $slide.innerHeight();
			
			var minWidth = mainWidth - width;
			var minHeight = mainHeight - height;
			
			
			$slide.css({ left: (opts.hDirection == "left") ? 0 : Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px"), top: (opts.vDirection == "bottom") ? Math.min(0, minHeight) + ((Math.min(0, minHeight) == 0) ? "" : "px") : 0, visibility: 'visible', display: 'block' })
			.animate({ opacity: "show" }, opts.animationTimeIn, "linear", function() {
				$(this).animate({ left: (opts.hDirection == "left") ? Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px") : 0 }, { queue: false, duration: Math.round(opts.animationTime * .75) }, "linear", function() { $(this).animate({ left: (opts.hDirection == "left") ? 0 : Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px") }, { queue: true, duration: Math.round(opts.animationTime * .5) }, "linear") })
				.animate({ top: (opts.vDirection == "bottom") ? 0 : 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]);
							var $slideImg2 = $slide2.find("img");
							var width = ($slideImg2.length) ? $slideImg2.innerWidth() : $slide2.innerWidth();
							var height = ($slideImg2.length) ? $slideImg2.innerHeight() : $slide2.innerHeight();
							
							
							$slide2.css({ left: (opts.hDirection == "left") ? 0 : Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px"), top: (opts.vDirection == "bottom") ? 0 : Math.min(0, minHeight) + ((Math.min(0, minHeight) == 0) ? "" : "px"), visibility: 'visible', display: 'block' })
							.animate({ opacity: "show" }, opts.animationTimeIn, "linear", function() {
								$(this).animate({ left: (opts.hDirection == "left") ? Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px") : 0 }, { queue: false, duration: Math.round(opts.animationTime * .75) }, "linear", function() { $(this).animate({ left: (opts.hDirection == "left") ? 0 : Math.min(0, minWidth) + ((Math.min(0, minWidth) == 0) ? "" : "px") }, { queue: true, duration: Math.round(opts.animationTime * .5) }, "linear") })
								.animate({ top: (opts.vDirection == "bottom") ? Math.min(0, minHeight) + ((Math.min(0, minHeight) == 0) ? "" : "px") : 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,
		hDirection: "right",
		vDirection: "top"
	};
})(jQuery)

//$(document).ready(function(){
	//$(".jSlideShow2").jSlideShow2();
	//$(".jSlideShow2").jSlideShow2({ width: 640, height: 300, animationTime: 8000, animationTimeIn: 2000, startTime: 5000, titleTime: 2000, fadeColor: "#FFFFFF", hDirection: "left", vDirection: "bottom"});
//});
