﻿if(!Controls)
{
    var Controls = {};
}

if(!Controls.Gallery)
{
    Controls.Gallery = function(imgMain, elmGallery)
    {
        this._imgMain = imgMain;
        this._elmGallery = elmGallery;
        
        var arrImages = this._elmGallery.getElementsByTagName("img");
        this._arrImages = [];
        for(var nIndex = 0; nIndex < arrImages.length; ++nIndex)
        {
            if(arrImages[nIndex].className != "Preload")
            {
                this._arrImages.push(arrImages[nIndex]);
            }
        }
        
        if(this._arrImages.length > 0)
        {
            this._imgMain.src = this._arrImages[0].getAttribute("imageurl")
            this._imgMain.alt = this._arrImages[0].alt;
            this._imgMain.setAttribute("title", this._arrImages[0].alt);
            this._bRunning = false;
            this._nCurrentImage = 0;
            
            var oCurrent = this;
            
            this._nRotateIntervalID = window.setInterval
            (
                function()
                {
                    ++oCurrent._nCurrentImage;
                    oCurrent._nCurrentImage %= oCurrent._arrImages.length;
                    
                    oCurrent.Show(oCurrent._arrImages[oCurrent._nCurrentImage], true);
                },
                Controls.Gallery.Speed * 1000
            );
            
            for(var nIndex = 0; nIndex < this._arrImages.length; ++nIndex)
            {
                (function()
                {
                    var imgSource = oCurrent._arrImages[nIndex];
                
                    $addHandler
                    (
                        oCurrent._arrImages[nIndex],
                        "click",
                        function()
                        {
                            oCurrent.Show(imgSource);
                        }
                    );
                }
                )();
            }
        }
    }
    
    Controls.Gallery.prototype.Show = function(imgSource, bAutoClick)
    {
        if(this._nRotateIntervalID && !bAutoClick)
        {
            window.clearInterval(this._nRotateIntervalID);
            this._nRotateIntervalID = false;
        }
        
        if(!this._bRunning && !Controls.Gallery.StopAll)
        {
            this._bRunning = true;
            
            var oCurrent = this;
            
            var oFadeOut = new AjaxControlToolkit.Animation.FadeOutAnimation(this._imgMain, Controls.Gallery.FadeSpeed, 20, 0, 1);
            oFadeOut.onEnd = function()
            {
                oCurrent._imgMain.src = imgSource.getAttribute("imageurl");
                oCurrent._imgMain.alt = imgSource.alt;
                oCurrent._imgMain.setAttribute("title", imgSource.alt);
                
                var oFadeIn = new AjaxControlToolkit.Animation.FadeInAnimation(oCurrent._imgMain, Controls.Gallery.FadeSpeed, 20, 0, 1);
                oFadeIn.onEnd = function()
                {
                    oCurrent._bRunning = false;
                }
                oFadeIn.play();
            }
            oFadeOut.play();
        }
    }
    
    Controls.Gallery.StopAll = false;
    
    Controls.Gallery.Speed = 5;
    Controls.Gallery.FadeSpeed = 0.5;
}
