/**
* jquery.jrotator. A really simple rotator plugin for jQuery
* 
* Copyright (c) 2011 Liam Gooding
* http://www.fruitbowlmedia.com
*
* Dual licensed under MIT and GPL 3 licenses. So, you can use this in free or commercial applications, at no charge. 
* All I ask is that this copyright header stays intact.
*
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-3.0.html
*
* Launch  : April 2009
* Version : 0.3.0 - April 5th 2010
*/

/**
* create closure
**/
(function($) {
	
	/**
	* Main jQuery plugin definition
	**/
	$.fn.jrotator = function(options) 
	{
		
		// Extend our default options with those provided
		var opts = $.extend({}, $.fn.jrotator.defaults, options);
		var self = this;
		
		// Iterate over the entire collection and apply our plugin
		return this.each(function()
		{
			// our container element
			var $container = $(this);
			
			// our main timer
			var timer = null;
			
			// how many slides in this simpleshow
			var slideCount = 0;
			
			// only show the first active one
			var $first = $container.children('.active');
			
			if ( $first.length == 0 ) 
			{
				$first = $container.children(':first');
				$first.addClass('active');
			}
			
			// Presets all other children to hidden
			$container.children(':not(.active)').each(function(){
				slideCount++;
				$(this).hide(); // fixes a small bug where fadeIn() fails on some elements
			});
			
						
			// Pause on mousehover?. added 0.3.0
			if(opts.pauseOnHover)
			{
				$container.hover(function(){
					self.pause();
				},function(){
					self.start();
				});
			}
			
			$first.show();
			
			/**
			* Public API
			**/
			$.extend( self, { 
					start: function() {		
						// create our interval to repeat the slideSwitch function
						if(timer == null){
							timer = setInterval(function(){
								_switchSlide($container, opts);
							} , opts.speed );	
						}
						
						return $container;
					},
					
					pause: function($container) {
						clearInterval(timer);
						timer = null;
						
						return $container;
					}
					
				}
			);
			
			// should we start the slideshow?
			if(opts.autoPlay) self.start();
			
			return this;
		
		});
		
	};
	
	/**
	* the default options
	**/
	$.fn.jrotator.defaults = {
		speed: 4000,
		fadeSpeed: 400,
		pauseOnHover: false, // added 0.3.0
		autoPlay: true, // added 0.3.0
		effect: 'fade', // added 0.3.0
		api: false // added 0.3.0
	}
	
	/**
	* Changes the currently visible slide. Private
	**/
	function _switchSlide($cont, opts){
		
		// get the active slide
		var $active = $cont.children('.active');
		
		// the next slide that we're going to fade in
		var $next = null;
		
		
		// work out which one we're switching to
		$next =  ($active.next().length) ? $active.next() : $cont.children(':first');

		
		// fade out the active slide to 0 opacity	
		$active.fadeOut(opts.fadeSpeed, function(){
			$active.removeClass('active');
			
			// now begin to fade in our next slide
			$next.fadeIn(opts.fadeSpeed, function(){
					$next.addClass('active');
			});
		});
		
		
		
	}
	
	
/**
* End closure
**/
})(jQuery);
