﻿// Script used by client-side version of the Modal Popup

window.onresize = HandlePopup;
window.onscroll = HandlePopup;
var PopupIsShowing = false;

function ShowPopup()
{
    PopupIsShowing = true;
    try
    {
        document.getElementById("PopupBackground").style.display = "block";
        document.getElementById("PopupContainer").style.display = "block";
    } catch(err) { }
    HandlePopup();
}
function HidePopup()
{
    PopupIsShowing = false;
    try
    {
        document.getElementById("PopupBackground").style.display = "none";
        document.getElementById("PopupContainer").style.display = "none";
    } catch(err) { }
}
function HandlePopup()
{
    if(PopupIsShowing)
    {
        var IEVersion = navigator.appVersion.match(/MSIE \d/);
        if(IEVersion)
        {
            if(Number(((new String(IEVersion)).split(" "))[1]) < 7)
            {
                // Simulate CSS fixed position for IE6 & older by adding scroll offset values when repositioning the popup
                var windowInterface = getWindowState();
                var x = windowInterface.getScrollX();
                var y = windowInterface.getScrollY();
                RepositionPopup(x, y);
                
                // Set popup background height & width
                try
                {
                    document.getElementById("PopupBackground").style.width = document.body.offsetWidth + "px";
                    document.getElementById("PopupBackground").style.height = document.body.offsetHeight + "px";
                } catch(err) { }
            }
            else // IE7
            {
                RepositionPopup(0, 0);
            }
        }
        else // other browser
        {
            RepositionPopup(0, 0);
        }
    }
}
function RepositionPopup(OffsetX, OffsetY)
{
    var AvailableWidth, AvailableHeight;
    if(window.innerHeight && window.innerWidth)
    {
        // Most browsers, not IE
        AvailableHeight = window.innerHeight;
        AvailableWidth = window.innerWidth;
    }
    else
    {
        // IE
        var windowInterface = getWindowState();
        AvailableWidth = windowInterface.getWidth();
        AvailableHeight = windowInterface.getHeight();
    }
    
    try
    {
        var PopupWidth = document.getElementById("PopupContainer").offsetWidth;
        var PopupHeight = document.getElementById("PopupContainer").offsetHeight;
        var PopupX = (AvailableWidth / 2) - (PopupWidth / 2) + OffsetX;
        var PopupY = (AvailableHeight / 2) - (PopupHeight / 2) + OffsetY;
        document.getElementById("PopupContainer").style.left = PopupX + "px";
        document.getElementById("PopupContainer").style.top = PopupY + "px";
    } catch(err) { }
}

// Object used to access properties of the window in all browsers (including IE)
// getWidth() = innerWidth
// getHeight() = innerHeight
// getScrollX() = scrollX
// getScrollY() = scrollY
function getWindowState()
{
    var global = this;
    var lastSt,lastSl,lastIW,lastIH,bodyRank = null,docElRank = null;
    var twoTestCount = 0;
    var readScroll = {scrollLeft:NaN,scrollTop:NaN};
    var readSizeC = {clientWidth:NaN,clientHeight:NaN};
    var readSizeI = {innerWidth:NaN,innerHeight:NaN};
    var readScrollX = 'scrollLeft';
    var readScrollY = 'scrollTop';
    var theInterface = {
        getScrollX:getScrlXMain,
        getScrollY:getScrlYMain,
        getWidth:function(){return initWidthHeight('getWidth');},
        getHeight:function(){return initWidthHeight('getHeight');}
    };
    function getScrlXTest(){hasChangedScroll();return getScrlXMain();}
    function getScrlYTest(){hasChangedScroll();return lastSl;}
    function getScrlXMain(){return readScroll[readScrollX];}
    function getScrlYMain(){return readScroll[readScrollY];}
    function getWidthI(){return readSizeI.innerWidth;}
    function getHeightI(){return readSizeI.innerHeight;}
    function getWidthC(){return readSizeC.clientWidth;}
    function getHeightC(){return readSizeC.clientHeight;}
    function getHeightSmart(){ return (hasChangedSize()?theInterface.getHeight:getHeightC()); }
    function getWidthSmart(){ return (hasChangedSize()?theInterface.getWidth:getWidthC()); }
    function setInnerWH(){
        theInterface.getWidth = getWidthI;
        theInterface.getHeight = getHeightI;
    }
    function setMinimumScroll(){
        theInterface.getScrollX = getScrlXMain;
        theInterface.getScrollY = getScrlYMain;
    }
    function hasChangedSize(){
        if((lastIH != (lastIH = getHeightI()))||(lastIW != (lastIW = getWidthI())))
        {
            threeObjectTest();
            return true;
        }
        return false;
    }
    function hasChangedScroll()
    {
        if((lastSl != (lastSl = getScrlYMain()))||(lastSt != (lastSt = getScrlXMain())))
        {
            threeObjectTest();
        }
    }

    function rankObj_inner(testObj, rankObj)
    {
        var dv,dh;
        if(
            ((dv = (global.innerHeight - testObj.clientHeight)) >= 0)&&
            ((dh = (global.innerWidth - testObj.clientWidth)) >= 0)&&
            (!(getScrlXMain()&&!dv))&&
            (!(getScrlYMain()&&!dh))&&
            (!((dh&&(rankObj.hDiff||(rankObj.hDiff = dh))&&
            (dh != rankObj.hDiff))||
            (dv&&(rankObj.vDiff||(rankObj.vDiff = dv))&&
            (dv != rankObj.vDiff)))))
        {
            if(dh == dv)
            { rankObj.rank = Number(Boolean(dh)); }
            else if((dh&&!dv)||(dv&&!dh))
            { rankObj.rank = (dh+dv); }
            else
            { rankObj.rank = NaN; }
        }
        else
        {
            rankObj.rank = NaN; 
        }
        return rankObj;
    }
    function propsAreNumbers(testObj)
    {
        for(var c = arguments.length;--c;)
        {
            if(typeof testObj[arguments[c]] != 'number')
            { return false; }
        }
        return true;
    }
    function rankObj(testObj, rankObj)
    {
        if(testObj&&propsAreNumbers(testObj, 'clientWidth','clientHeight'))
        { rankObj_inner(testObj, rankObj) }
        return rankObj;
    }
    function initWidthHeight(callOn)
    {
        lastIW = getWidthI();
        lastIH = getHeightI();
        lastSt = getScrlYMain();
        lastSl = getScrlXMain();
        bodyRank = {vDiff:0,hDiff:0,rank:NaN};
        docElRank = {vDiff:0,hDiff:0,rank:NaN};
        theInterface.getWidth = getWidthSmart;
        theInterface.getHeight = getHeightSmart;
        theInterface.getScrollX = getScrlXTest;
        theInterface.getScrollY = getScrlYTest;
        threeObjectTest();
        rankObj = rankObj_inner;
        return theInterface[callOn]();
    }
    function threeObjectTest()
    {
        var bodyNaN = isNaN(rankObj(document.body, bodyRank).rank);
        var docElNaN =
        isNaN(rankObj(document.documentElement, docElRank).rank);
        if(bodyNaN||docElNaN)
        {
            if(bodyNaN&&docElNaN)
            {
                setMinimumScroll();
                setInnerWH();
            }
            else
            {
                readSizeC = ((docElNaN)?document.body:
                document.documentElement);
                bodyRank = ((docElNaN)?bodyRank:docElRank);
                docElRank = null;
                threeObjectTest = twoObjectTest;
            }
        }
        else
        {
            readSizeC = ((bodyRank.rank < docElRank.rank)?
            document.body:
            document.documentElement);
        }
    }
    function twoObjectTest()
    {
        if(isNaN(rankObj(readSizeC, bodyRank).rank))
        {
            bodyRank = null;
            setMinimumScroll();
            setInnerWH();
        }
        else if((bodyRank.vDiff)&&(bodyRank.hDiff)&&(++twoTestCount > 2))
        {
            bodyRank = null;
            theInterface.getWidth = getWidthC;
            theInterface.getHeight = getHeightC;
            setMinimumScroll();
        }
    }

        if(!propsAreNumbers(global, 'innerHeight', 'innerWidth'))
        {
            readSizeC = compatModeTest(readSizeC);
            theInterface.getWidth = getWidthC;
            theInterface.getHeight = getHeightC;
        }
        else
        { readSizeI = global; }
        if(propsAreNumbers(global, 'pageYOffset', 'pageXOffset'))
        {
            readScroll = global;
            readScrollY = 'pageYOffset';
            readScrollX = 'pageXOffset';
        }
        else
        { readScroll = compatModeTest(readScroll); }
        return (getWindowState = function(){return theInterface;})();
    }

    function compatModeTest(obj)
    {
        if((document.compatMode)&&(document.compatMode.indexOf('CSS') != -1)&&(document.documentElement))
        {
            return (compatModeTest = function(){
                return document.documentElement;
            })((obj = null));
        }
        else if(document.body)
        {
            return (compatModeTest = function(){
                return document.body;
            })((obj = null));
        }
        else
        { return obj;
    }
}