﻿/*******************************************************************************
TITLE: 			jQuery Simple Rotator
	
AUTHORS:		Nathan Koch
	
VERSION:		1.0
	
REQUIRES:		jQuery v1.3.2
	
EXPECTS:		A container div (or ul, or ol etc) that contains a collection of touts
	
OPTIONS:		int speed - speed of rotation
int (or string) fadeSpeed = fade in/out speed
selector items - the items inside the tout rotator
					
CALLBACKS: 		None
					
DESCRIPTION:	Rotates through touts endlessly. Pauses on hover indefinitely.
	
*******************************************************************************/

(function($) {
    $.fn.extend({
        rotate: function(options) {
            // DEFAULTS
            var defaults = {
                $el: $(this),
                speed: 5000,
                fadeSpeed: 'slow',
                items: "li"
            };
            var options = $.extend(defaults, options);
            return this.each(function() {

                // VARIABLES
                var $items = $(this).find(options.items),
               	    $firstItem = $items.filter(":first"),
               	    $imgs = $(".touts li a"),
                    index = 0,
                    $currentItem = $items.eq(index),
                    hovered = false,
                    animating = false;

                // FUNCTIONS
                change = function() {
                    if (hovered) {
                        return false;
                    }
                    animating = true;
                    $currentItem.fadeOut(options.fadeSpeed, changeIn);
                };

                changeIn = function() {
                    animating = false;
                    if ($currentItem.next().length) {
                        index++;
                    } else {
                        index = 0;
                    }
                    $currentItem = $items.eq(index);
                    $currentItem.fadeIn(options.fadeSpeed);
                };

                up = function() {
                    if (animating == true) {
                        $currentItem.stop(true);
                        $currentItem.animate({ opacity: "1" }, { queue: false, duration: options.fadeSpeed });
                    }
                    $imgs.stop(true).animate({
                        paddingTop: "2px"
                    }, "fast");
                };

                dn = function() {
                    $imgs.stop(true).animate({
                        paddingTop: "15px"
                    }, "fast");
                };

                // EVENT LISTENERS
                $items.hover(
		            function() {
		                hovered = true;
		                up(); //todo, call on event from <a>
		            },
		            function() {
		                hovered = false;
		                dn(); //todo, call on event from <a>
		            }
	            );


                // FIRED ON PLUGIN LOAD
                window.setInterval(change, options.speed);
            });
        }
    });
})(jQuery);