/*
*************************************************

SEBASTIAN NITU
jQuery imageRotate Plugin

Created by Sebastian Nitu
http://www.sebnitu.com

*************************************************
*/

(function($) {

/**
 * Plugin Definition
 */
$.fn.imageRotate = function(options) {

	// Extend our default options with those provided.
	var opts = $.extend({}, $.fn.imageRotate.defaults, options);

	return this.each(function() {
		
		// Save our object
  	var $this = $(this);
  	
  	// Build element specific options (syntax: o.optionName)
  	var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
		
		// Initialize Element Specific Variables
		var $images = $this.find('img').wrapAll('<div class="imageRotate-wrap">').css({ 
					position : 'absolute', top : '0', left : '0', 'z-index' : 1, 'opacity' : 0 
				}),
				$imageWrap = $this.find('.imageRotate-wrap').css({ 
					position : 'relative', cursor : 'pointer',
					margin : '0 auto'
				}),
				$imageWrapTopPadding = $this.find('.imageRotate-wrap').css('padding-top');
				$imageWrapLeftPadding = $this.find('.imageRotate-wrap').css('padding-left');
				$imageCount = $images.size(),
				$currentImage = 1;
				
		$images.filter(':first').css({ 'z-index' : 3, 'opacity' : 1 });
		$imageWrap.height( $images.filter(':first').outerHeight() ).width( $images.filter(':first').outerWidth() );
		
		if ($imageWrapTopPadding) {
			$images.css({ top : $imageWrapTopPadding })
		}
		if ($imageWrapLeftPadding) {
			$images.css({ left : $imageWrapLeftPadding })
		}
		
		/**
		 * Swap Image Function
		 */
		function imageSwap(image) {
			
			if ( $images.filter(':not(:animated)') ) {
			
				if( image < 1 ) {
					image = $imageCount;
				} else if ( image > $imageCount ) {
					image = 1;
				}
			
				// Pre animation stack order
				$images.css({ 'z-index' : 1 });
				$( $images[image - 1] ).css({ 'z-index' : 2 });
				$( $images[$currentImage - 1] ).css({ 'z-index' : 3 });
				
				// Resize the wrapper
				$imageWrap.animate({
					height : ( $($images[image - 1]).outerHeight() ),
					width : ( $($images[image - 1]).outerWidth() )
				}, 500 );
				
				// Animate the image transition
				$( $images[image - 1] ).animate({
					opacity: 1
				}, 500);
				$( $images[$currentImage - 1] ).animate({
			    opacity : 0
			  }, {
			    duration: 500,  
			    complete: function() {
			    									
						$currentImage = image;
												
						// Post animation stack order
						$images.css({ 'z-index' : 1 });
						$( $images[$currentImage - 1] ).css({ 'z-index' : 3 });
					
					}
			  });
			
			};	
			return false;
		}
		
		// Switch every couple seconds
		function autoScroll() {
	    intervalID = setInterval(function() {
	      imageSwap($currentImage + 1);
			}, o.autoRotateSpeed);
			return intervalID;
		}
		// Initiate Auto Scrolling
		var intervalID = autoScroll();
				
	});

};
	
/**
 * Plugin Defaults
 */
$.fn.imageRotate.defaults = {
	autoRotateSpeed : 3000
};
	
})(jQuery);

/*-------------------------------------------    
	Fin
---------------------------------------------*/
