/* ==================================================================
*
*	ROTATOR PLUGIN
* 
*	DATE: 				NOVEMBER 27, 2009
*	AUTHOR: 			AMY HAYWOOD
* 	URL: 				WWW.AMYHAYWOOD.COM
*
*	REQUIREMENTS: 		jQuery
*						Be sure to include the jQuery library before including this script
* 	IMPLEMENTATION:
*		Within the $(document).ready(); all that you really need to include is:
*		|	$('#DIV_THAT_WRAPS_ALL_ROTATING_ITEMS').rotator();
*	
* 		However, you can change the amount of time that it takes ot rotate:
* 		|	$('#DIV_THAT_WRAPS_ALL_ROTATING_ITEMS').rotator({
*		|		amount_of_time: 5000
*		|	});
*
*		Within your HTML, you must have a div that wraps all rotator items with a unique id.
*		Each individual rotator item must be wrapped in a div
*		|	<div id="DIV_THAT_WRAPS_ALL_ROTATING_ITEMS">
*		|		<div>My Rotator Item #1</div>
*		|		<div>My Rotator Item #2</div>
*		|		<div>My Rotator Item #3</div>
*		|	</div>
*
===================================================================== */

(function($) {
	$.fn.rotator = function(options){

		// DEFINE OPTIONS
		var defaults = {

			amount_of_time: 5000					// AMOUNT OF TIME BEFORE CHANGING IN MILLISECONDS
		};
	
		var settings = $.extend(defaults, options);
	
		return this.each(function() {
			var $element = $(this);
		
			// CHANGES THE PRODUCT
			// SHOULD BE THE NEXT ITEM LISTED, UNLESS IT IS THE LAST ITEM, IN WHICH CASE IT GOES BACK TO THE FIRST ITEM
			ChangeProduct = function() {				
				// FIND THE CURRENT PRODUCT
				var currentlyVisible = $element.children('div:visible');
				
				// FADE OUT THE CURRENT PRODUCT AND FADE IN AND SHOW THE NEXT ITEM
				currentlyVisible.fadeOut(function() {

					if (currentlyVisible.next().length < 1) {
						DisplayTheFirstProduct($element);
					} else {
						currentlyVisible.next().fadeIn();
					}

				});

				return;
			};

			// DISPLAYS THE FIRST PRODUCT LISTED
			DisplayTheFirstProduct = function($element) {
				$element.children('div:first').fadeIn();
				return;
			};
			
			
			// INITIALIZE THE ROTATOR
			$element.children('div').hide();		// HIDE ALL THE PRODUCTS
			DisplayTheFirstProduct($element);		// SET THE CURRENT PRODUCT
			
			var myTimer = setInterval(ChangeProduct, settings.amount_of_time);		
			
		});
	};
})(jQuery);


