﻿if(!Controls)
{
    var Controls = {};
}

if(!Controls.PopUpImage)
{
    Controls.PopUpImage = function(elmImage, elmHider, elmHide)
    {
        this._elmImage = elmImage;
        this._elmHider = elmHider;
        this._elmHide = elmHide;
        
        this._elmImage.style.visibility = "hidden";
        this._elmImage.style.left = "0px";
        this._elmImage.style.top = "0px";
        this._elmImage.style.position = "absolute";
        this._elmImage.style.display = "block";
        
        this._elmImage.style.zIndex = 210;
        
        this._elmHider.style.zIndex = 200;
        this._elmHider.style.position = "absolute";
        this._elmHider.style.backgroundColor = "#000000";
        
        var oCurrent = this;
    }
    
    Controls.PopUpImage.prototype.Hide = function()
    {
        this._elmHide.style.visibility = "hidden";
        
        var nWindowWidth = document.documentElement.clientWidth;
        var nWindowHeight = document.documentElement.clientHeight;
        
        var oWindowScroll = Controls.PopUpImage._getScrollPosition();
        var nOffsetLeft = oWindowScroll.x;
        var nOffsetTop = oWindowScroll.y;
        
        var oParallelAnimation = new AjaxControlToolkit.Animation.ParallelAnimation(this._elmImage, 0.5, 10);
        
        oParallelAnimation.add
        (
            new AjaxControlToolkit.Animation.ResizeAnimation(this._elmImage, 1.0, 20, 0, 0, "px")
        );
        oParallelAnimation.add
        (
            new AjaxControlToolkit.Animation.MoveAnimation(this._elmImage, 1.0, 20, nOffsetLeft + nWindowWidth / 2, nOffsetTop + nWindowHeight / 2, false, "px")
        );
        oParallelAnimation.add
        (
            new AjaxControlToolkit.Animation.FadeInAnimation(this._elmHider, 1.0, 20, 0.5, 0.0, true)
        );
        
        var oCurrent = this;
        
        oParallelAnimation.onEnd = function()
        {
            oCurrent._elmImage.style.display = "none";
            oCurrent._elmHider.style.display = "none";
            
            if(Controls.ImageRotator)
            {
                Controls.ImageRotator.StopAll = false;
            }
            if(Controls.Gallery)
            {
                Controls.Gallery.StopAll = false;
            }
        }
        
        oParallelAnimation.play();   
    }
    
    Controls.PopUpImage.prototype.Show = function()
    {
        if(Controls.ImageRotator)
        {
            Controls.ImageRotator.StopAll = true;
        }
        if(Controls.Gallery)
        {
            Controls.Gallery.StopAll = true;
        }
        
        //Safari doesn't report the offsetWidth until well after page onload has finished so we have to measure here
        var nWidth = this._elmImage.offsetWidth;
        var nHeight = this._elmImage.offsetHeight;
        
        this._elmImage.style.display = "none";
        this._elmImage.style.visibility = "visible";
        
        this._elmImage.style.overflow = "hidden";
    
        var nWindowWidth = document.documentElement.clientWidth;
        var nWindowHeight = document.documentElement.clientHeight;
        
        var oWindowScroll = Controls.PopUpImage._getScrollPosition();
        var nOffsetLeft = oWindowScroll.x;
        var nOffsetTop = oWindowScroll.y;
        
        var nX = nOffsetLeft + nWindowWidth / 2;
        var nY = nOffsetTop + nWindowHeight / 2;
       
        this._elmImage.style.left = nX + "px";
        this._elmImage.style.top = nY + "px";
        
        this._elmImage.style.width = "0px";
        this._elmImage.style.height = "0px";
        
        this._elmImage.style.display = "block";
        
        this._elmHider.style.left = "0px";
        this._elmHider.style.top = "0px";
        this._elmHider.style.width = nOffsetLeft + nWindowWidth + "px";
        this._elmHider.style.height = nOffsetTop + nWindowHeight + "px";
        
        var oParallelAnimation = new AjaxControlToolkit.Animation.ParallelAnimation(this._elmImage, 0.5, 10);
        
        oParallelAnimation.add
        (
            new AjaxControlToolkit.Animation.ResizeAnimation(this._elmImage, 1.0, 20, nWidth, nHeight, "px")
        );
        oParallelAnimation.add
        (
            new AjaxControlToolkit.Animation.MoveAnimation(this._elmImage, 1.0, 20, nX - nWidth / 2, nY - nHeight / 2, false, "px")
        );
        oParallelAnimation.add
        (
            new AjaxControlToolkit.Animation.FadeInAnimation(this._elmHider, 1.0, 20, 0.0, 0.5, true)
        );
        
        oParallelAnimation.play();
        
        this._elmHider.style.display = "block";
        this._elmHide.style.visibility = "visible";
    }
    
    Controls.PopUpImage._getScrollPosition = function()
    {
        var oHolder = {};
        
        oHolder.x = document.body.scrollLeft;
        if(oHolder.x == 0)
        {
            if(window.pageXOffset)
            {
                oHolder.x = window.pageXOffset;
            }
            else
            {
                oHolder.x = (document.body.parentElement ? document.body.parentElement.scrollLeft : 0);
            }
        }
        
        oHolder.y = document.body.scrollTop;
        if(oHolder.y == 0)
        {
            if(window.pageYOffset)
            {
                oHolder.y = window.pageYOffset;
            }
            else
            {
                oHolder.y = (document.body.parentElement ? document.body.parentElement.scrollTop : 0);
            }
        }
        
        return oHolder;
    }
}
