/**********************************************************************
# COPYRIGHT NOTICE
# Copyright (C) 2008 by Summersault, LLC.  All rights reserved.
# Summersault / 914 East Main Street, Richmond, IN 47374
# Web: http://www.summersault.com/	E-mail: info@summersault.com
#*********************************************************************/

/*
* Scroll through a vertical group of elements with fancy animation.
* 
* Usage:
*   $('#containing_div').auto_scroll(3000, '.elements_to_scroll', 2);
*   $('#containing_div').auto_scroll(3000, '.elements_to_scroll', 2, false);
*
* Args:
*   interval: number of milliseconds to wait before scrolling
*   element_class: the class for the elements to scroll
*   elements_to_show: the number of elements to show at a time
*   slide_down: a boolean, true for a slide-down effect, which has the most
*      visual appeal, but that slighly disobeys the order of the elements.
*      False will perform an order-correct scroll, but has a less pleasing
*      slide-up effect. Optional, defaults to true.
*/
(function($) {
    $.fn.auto_scroll = function(interval, element_class, elements_to_show, slide_down) {
        // If we don't have a value here, default to slide_down = true
        if (slide_down == undefined) { slide_down = true; };

        // Hide the extra elements.
        $(this).find(element_class).slice(elements_to_show).hide();
        // Keep the container a constant height.
        $(this).attr('height', $(this).height());
	    
        // Create an inner function for the timer callback.
        var scroller = this;
        function rotate_elements() {
            var first   = $(scroller).find(element_class + ':first');
            var last    = $(scroller).find(element_class + ':last');

            if (slide_down) {
                // The slide-down is most visually appealing, but this promotes
                // the last item of the array, thereby making it out of order,
                // which is not ideal in cases where list ordering is essential.
                var to_hide = $(scroller).find(element_class).eq(elements_to_show - 1);
                
                to_hide.fadeOut("slow", function() {
                    $(last).insertBefore(first).slideDown("slow");
                });
            }
            else {
                // The slide-up is less visually appealing, but this does retain
                // the strict ordering of the list, so it's useful in some cases.
                var to_show = $(scroller).find(element_class).eq(elements_to_show);
                
                $(first).slideUp("slow", function() {
                    $(first).insertAfter(last);
                    to_show.fadeIn("slow");
                });
            }
        }
        
        // Set the timer.
        var interval_id = setInterval(rotate_elements, interval);
        
        // The scrolling should stop on hover.
        $(this).hover(
            function() { clearInterval(interval_id); },
            function() { interval_id = setInterval(rotate_elements, interval); }
        );
    }
})(jQuery);
