﻿if(!Controls)
{
    var Controls = {};
}

if(!Controls.ImageRotator)
{
    Controls.ImageRotator = function(elmImageRotator)
    {
        this._arrImages = elmImageRotator.getElementsByTagName("div");
        
        if(this._arrImages.length > 0)
        {
            var nTop = this._arrImages[0].offsetTop;
            
            for(var nIndex = 0; nIndex < this._arrImages.length; ++nIndex)
            {
                this._arrImages[nIndex].style.top = nTop + "px";
            }
            
            this._nCurrentIndex = 0;
            
            var oCurrent = this;
            
            window.setInterval
            (
                function()
                {
                    if(!Controls.ImageRotator.StopAll)
                    {
                        var nPreviousIndex = oCurrent._nCurrentIndex;
                        
                        ++oCurrent._nCurrentIndex
                        oCurrent._nCurrentIndex %= oCurrent._arrImages.length;
                        
                        oCurrent._arrImages[nPreviousIndex].style.zIndex = 100;
                        oCurrent._arrImages[oCurrent._nCurrentIndex].style.zIndex = 90;
                        
                        var oFadeOut = new AjaxControlToolkit.Animation.FadeOutAnimation(oCurrent._arrImages[nPreviousIndex], Controls.ImageRotator.FadeSpeed, 20, 0, 100);
                        oFadeOut.onEnd = function()
                        {
                            oCurrent._arrImages[nPreviousIndex].style.display = "none";
                        }
                        oFadeOut.play();
                        
                        AjaxControlToolkit.Animation.FadeInAnimation.play(oCurrent._arrImages[oCurrent._nCurrentIndex], Controls.ImageRotator.FadeSpeed, 20, 0, 100);
                        oCurrent._arrImages[oCurrent._nCurrentIndex].style.display = "block";
                    }
                },
                Controls.ImageRotator.Speed * 1000
            );
        }
    }
    
    Controls.ImageRotator.StopAll = false;
    
    Controls.ImageRotator.Speed = 5;
    Controls.ImageRotator.FadeSpeed = 1;
}
