﻿GoodNites.ChapterBanners = function() {
    this.Initialize();
};
GoodNites.ChapterBanners.prototype = {
    Initialize: function() {
        var options = {
            startIndex: 0,
            fadeInDuration: 850,
            fadeOutDuration: 850,
            rotationInterval: 12000,
            startDelay: 0
        };
    
        var rotator = function(containerSelector, hatSelector, options){
            var children = $(containerSelector).children();
            var startIndex = options.startIndex;
            var totalItems = children.length - 1; // this is so that we exclude the hat (the -1 part)
            
            $(children[startIndex]).show();
            setTimeout(function(){
                setInterval(function(){
                    $(hatSelector).fadeIn(options.fadeOutDuration, function(){
                        $(children[startIndex]).hide(function(){
                            // determine the actual start index by adding one, then taking the modulus
                            // this will give us the correct start index regardless of the number
                            // of items we have to work with.
                            startIndex = (startIndex + 1) % totalItems;
                            $(children[startIndex]).show(function(){
                                $(hatSelector).fadeOut(options.fadeInDuration);
                            });
                        });
                    });
                }, options.rotationInterval);
            }, options.startDelay);
        };
        
        // handle the middle banners
        options.startIndex = Math.floor(Math.random()*5);
        rotator('#mContainer', '#mContentHat', options);
        
        // handle the bottom banners
        options.startIndex = Math.floor(Math.random()*4);
        options.startDelay = 6000;
        rotator('#bContainer', '#bContentHat', options);
    }
};
GoodNites.Extend(GoodNites.ChapterBanners, GoodNites.Core);
