/*
 * jQuery EZ Rotator
 *
 * Iterates through an unordered list of items 
 * to create a simple slideshow
 *
 * Copyright (c) 2009 Frederick King
 *
 */

(function($){
	
    var ezrotator;
    if (!ezrotator) 
        ezrotator = function(){};
    //Initiaize defaults
    $.fn.ezrotator = function(options){
        var defaults = {
            interval: 5000,
			speed: "slow",
			transition: ""
        };
        var opts = $.extend(defaults, options);
        return this.each(function(){
            var element = $(this);
            element.children().hide();
            var numChildren = ezrotator.countChildren(element);
            element.children().eq(0).show();
            ezrotator.rotate(element, numChildren, 0, opts.interval, opts.speed);
        });
    };
    //Helper function
	ezrotator.countChildren = function(element){
        var numChildren = element.children().size();
        return numChildren;
    };
    //Call rotate function
	ezrotator.rotate = function(element, numChildren, counter, interval, speed){
        var elem = element;
        var numChild = numChildren;
        var count = counter;
        if (count == numChild - 1) 
            count = 0;
        else 
            count++;
        $(element).children("li").eq(counter).fadeOut(speed);
        $(element).children("li").eq(count).fadeIn(speed);
		//Set time function to create delay between transitions
        setTimeout(function(){
			//Recursive call to rotate function
            ezrotator.rotate(elem, numChild, count, interval, speed);
        }, interval);
    };
})(jQuery);
