

function CreateRotator(rotatorBaseDivId, numbersBaseDivId, ActiveIndex)
{
    var retVal = 
    {
        'RotatorDivs' : $('#' + rotatorBaseDivId + ' div'),
        'NumberDivs' : $('#' + numbersBaseDivId + ' div'),
        'Count' : function()
            {
                return this.RotatorDivs.length;
            },
        'Interval' : 5000,
        'FadeTime' : 200,
        'NumberColor' : '#fff',
        'Numberbackground' : '#9b958e',
        'NumberColorHover' : '#fff',
        'NumberbackgroundHover' : '#b19f78',
        'ActiveIndex' : ActiveIndex,
        'IsActive' : false,
        'IsBusy' : false,
        'CurrentIntervalId' : null,
        'CurrentTimeoutId' : null,
        'SetNumberColorHover' : function(previous, active)
            {
                this.NumberDivs[previous].style.color = this.NumberColor;
                //this.NumberDivs[previous].style.backgroundColor = this.Numberbackground;
                this.NumberDivs[previous].className = 'featuredOfferNumbers';
            
                this.NumberDivs[active].style.color = this.NumberColorHover;
                //this.NumberDivs[active].style.backgroundColor = this.NumberbackgroundHover;
                this.NumberDivs[active].className = 'featuredOfferNumbers selected';
            },
        'GetPreviousIndex' : function()
            {
                if (this.ActiveIndex > 0)
                    return this.ActiveIndex - 1;
                else
                    return this.Count()-1;
            },
        'GetNextIndex' : function()
            {
                if (this.ActiveIndex == this.Count() - 1)
                    return 0;
                else
                    return this.ActiveIndex+1;
            },
        'Stop' : function()
            {
                if (this.CurrentIntervalId)
                    clearInterval(this.CurrentIntervalId);
            },
        'Start' : function()
            {
                var me = this;
                
                if (this.CurrentIntervalId)
                    clearInterval(this.CurrentIntervalId);
                
                if (!this.IsActive)
                    this.Next();
                
                
                this.CurrentIntervalId = setInterval(function(){ me.Next(); }, me.Interval);
            },
        'Next' : function()
            {
                if (!this.IsBusy)
                {
                    if (this.IsActive)
                    {
                        var parent = this;
                        
                        this.ActiveIndex = this.GetNextIndex();
                        this.IsBusy = true;
                        
                        this.SetNumberColorHover(this.GetPreviousIndex(), this.ActiveIndex);
                        
                        $(this.RotatorDivs[this.GetPreviousIndex()]).fadeOut(this.FadeTime, function()
                            {
                                $(parent.RotatorDivs[parent.ActiveIndex]).fadeIn(parent.FadeTime, function()
                                {
                                    parent.IsBusy = false;
                                }); 
                            });
                    }
                    else
                    {
                        this.SetNumberColorHover(this.GetPreviousIndex(), this.ActiveIndex);
                        
                        $(this.RotatorDivs[this.ActiveIndex]).fadeIn(this.FadeTime);
                        this.IsActive = true;
                    }
                }
                else
                {
                    var me = this;
                    
                    if (this.CurrentTimeoutId)
                        clearTimeout(this.CurrentTimeoutId);
                    
                    this.CurrentTimeoutId = setTimeout(function(){me.Next();}, 250);
                }
            },
        'ShowOffer' : function(index)
            {
                if (!this.IsActive || this.ActiveIndex != index)
                {
                    if (!this.IsBusy)
                    {
                        this.Stop();
                        var parent = this;
                        
                        this.IsBusy = true;
                        
                        if (this.IsActive)
                        {
                            this.SetNumberColorHover(this.ActiveIndex, index);
                            $(this.RotatorDivs[this.ActiveIndex]).fadeOut(this.FadeTime, function()
                            {
                                $(parent.RotatorDivs[index]).fadeIn(parent.FadeTime, function()
                                {
                                    parent.IsBusy = false;
                                }); 
                            });
                            
                            this.ActiveIndex = index;
                        }
                        else
                        {   
                            this.SetNumberColorHover(this.ActiveIndex, index);
                            $(this.RotatorDivs[index]).fadeIn(this.FadeTime, function()
                            {
                                parent.IsBusy = false;
                            }); 
                            
                            this.ActiveIndex = index;
                            this.IsActive = true;
                        }
                    }
                    else
                    {
                        var me = this;
                        
                        if (this.CurrentTimeoutId)
                            clearTimeout(this.CurrentTimeoutId);
                        
                        this.CurrentTimeoutId = setTimeout(function(){me.ShowOffer(index);}, 250);
                    }
                }
            }
    };
 
    return retVal;
        
}

