// ============================================
// Client Rotator Suboptic Mode
//    image rotator for homepage
//    author: John Norton
//    date: 7/22/09
//    version: 0.1
// ============================================
(function($) {
    $.fn.ClientRotator = function(options) {
        //var obj = jQuery.extend({ obj: $('.logoGroup') }, { obj: $('.logoGroup') });
        var settings = jQuery.extend({
            containerType: 'div', //The stories to rotate (jQuery Selector)
			itemType: 'a',
			colorItemType: 'img',
            fadeTime: 1000, //Time it takes to fade between images. 1000 = 1s (Integer)
            waitTime: 9000, //Time to hold a story in place before it rotates again. 1000 = 1s (Integer)
			colorTime: 3000, //Time to hold a story in place before it rotates again. 1000 = 1s (Integer)
            waitToStart: 3000, //Initial wait time. 1000 = 1s (Integer)
            numberToShow: 1, //The number of items per loop (Integer)
            animation: 'fade', //Accepts all available animations even the ones provided in the jQuery.UI if present.
            //Possible values: 'blind', 'clip', 'drop', 'explode', 'fade', 'fold', 'highlight', 'slide', 'slide-v'.
            //If jQuery.UI is not present only 'fade' and 'slide-v' are available.
			colorAnimation: 'fade', //Accepts all available animations even the ones provided in the jQuery.UI if present.
            //Possible values: 'blind', 'clip', 'drop', 'explode', 'fade', 'fold', 'highlight', 'slide', 'slide-v'.
            //If jQuery.UI is not present only 'fade' and 'slide-v' are available.
            temp: null,
            obj: '.sponsors'
        }, options || {});

        $.fn.ClientRotator.FadeOut = function() {
            settings.temp = $(settings.obj).find('> ' + settings.containerType + ':visible').get(settings.numberToShow - 1);
            $.each($(settings.obj).find('> ' + settings.containerType + ':visible'), function() {
                o = $(this);
                if (settings.animation == 'fade')
                    $(o).fadeOut(settings.fadeTime);
                else if (settings.animation == 'slide-v')
                    $(o).slideUp(settings.fadeTime);
                else
                    $(o).hide(settings.animation, {}, settings.fadeTime);
            });
            setTimeout($.fn.ClientRotator.FadeIn, settings.fadeTime + 100);
        }
		
		$.fn.ClientRotator.Colorize = function() {
            $.each($(settings.obj).find('> ' + settings.containerType + ':visible ' + settings.itemType).find(settings.colorItemType + ':last'), function() {
                o = $(this);
                if (settings.colorAnimation == 'fade')
                    $(o).fadeOut(settings.fadeTime);
                else if (settings.animation == 'slide-v')
                    $(o).slideUp(settings.fadeTime);
                else
                    $(o).hide(settings.colorAnimation, {}, settings.fadeTime);
            });
        }

        $.fn.ClientRotator.FadeIn = function() {
            shown = settings.temp;
            $(settings.obj).find('> ' + settings.containerType + ' a').find('img:last').css('display', 'block');
			for (i = 0; i < settings.numberToShow; i++) {
                o = $(shown).next(settings.containerType);
                if ($(o).size() < 1) {
                    o = $(settings.obj).find('> ' + settings.containerType + ':first');
                }
                shown = o;
                if (settings.animation == 'fade')
                    $(o).fadeIn(settings.fadeTime);
                else if (settings.animation == 'slide-v')
                    $(o).slideDown(settings.fadeTime);
                else
                    $(o).show(settings.animation, {}, settings.fadeTime);
            }
			setTimeout($.fn.ClientRotator.Colorize, settings.colorTime + settings.fadeTime);
            setTimeout($.fn.ClientRotator.FadeOut, settings.waitTime + settings.fadeTime);
        }

        $(function() {
			/*p = $(settings.obj);
			c = 0;
			n = null;
			$.each($(settings.obj + ' ' + settings.itemType), function() {
					if(c == 0) n = $('<div/>').addClass("sponsorRoll");
					c++;
					n.append($(this));
					if(c == 4) {c = 0;p.append(n);n = '';}
			});
			p.append(n);*/
			$(settings.obj + ' ' + settings.containerType + ' ' + settings.itemType).find(settings.colorItemType + ':last').css({position: 'absolute', left:0, top:0, display: 'none'});
            setTimeout($.fn.ClientRotator.FadeOut, settings.waitToStart);
            $(settings.obj).find(settings.containerType + ':gt(' + (settings.numberToShow - 1) + ')').css('display', 'none');
        });
    };
})(jQuery);