﻿(function(jQuery) {
    var params;
    var activeItemIndex = 0;    
    
    function SetupRotator(obj, index, forward) {
        var trueDisplay  = (forward) ? "" : "none";
        var falseDisplay = (forward) ? "none" : "";
        
        jQuery.each(obj.children(), function(i, val) {
            jQuery(this).css("display", i < index ? trueDisplay : falseDisplay);
        });  
        activeItemIndex  = (forward) ? 0 : index;
    }
    
    function move(obj, forward) {
        var childCount = obj.children().size();     
        var elemId = obj[0].id;  
        var newElemIndex, oldElemIndex;   
        
        oldElemIndex = (forward) ? activeItemIndex : (activeItemIndex + params.MessageCount - 1);      
        newElemIndex = (forward) ? (activeItemIndex + params.MessageCount) : (activeItemIndex - 1);     
               
        if (newElemIndex >= childCount) //you went too far past the end
        {                      
            if (params.AutoRotate) 
            { 
                activeItemIndex  = 0;
                jQuery(obj).moveFirst(); 
            }            
        }
        else if (newElemIndex < 0) //you went past the beginning
        {
            activeItemIndex  = 0;
        }
        else 
        {
            activeItemIndex += (forward) ? 1 : -1;
                      
            switch(params.RotationMethod)
            {
                case "fade"     :
                    jQuery("#" + elemId + " > *:eq(" + oldElemIndex + ")").fadeOut(params.Speed);
                    jQuery("#" + elemId + " > *:eq(" + newElemIndex + ")").fadeIn(params.Speed);   
                    break;  
                case "slide"    :
                    jQuery("#" + elemId + " > *:eq(" + oldElemIndex + ")").slideUp(params.Speed);
                    jQuery("#" + elemId + " > *:eq(" + newElemIndex + ")").slideDown(params.Speed);   
                    break; 
            }
            
            if (params.AutoRotate) 
            {
                setTimeout("jQuery('#" + elemId + "').moveNext();", params.RotationDelay);
            }
        }                       
    }
        
    jQuery.fn.rotate = function(settings) {
        params = jQuery.extend({
          MessageCount: 1,
          RotationMethod: 'slide',
          AutoRotate: true,
          Speed: 'normal',
          RotationDelay: 10000
        }, settings);

        //TODO: this will probably be breaking if there is more than one rotator on the page since 
        //I am not iterating through the children.
        jQuery(this).moveFirst();
    };
    
    jQuery.fn.moveNext = function () { move(this, true); };
    
    jQuery.fn.movePrevious = function () { move(this, false); };    

    jQuery.fn.moveFirst = function() { 
        SetupRotator(this, params.MessageCount, true); 
        
        if (this.children().size() > 1 && params.AutoRotate)
        {
            setTimeout("jQuery('#" + this[0].id + "').moveNext();", params.RotationDelay);
        }  
        
    };

    jQuery.fn.moveLast = function() {
        SetupRotator(this, (this.children().size() - params.MessageCount), false);
    };

})(jQuery); 