
    function gPopupDivs()
    {
        var MainOverlay = document.getElementById('MainOverlay');
        var MainPopup   = document.getElementById('MainPopup');
        // Added IE 8 compatibility mode check (documentMode)
        var fIE         = (document.documentMode && document.documentMode == 7) || false /*@cc_on || @_jscript_version <= 5.7 @*/;
        var fIE6        = false /*@cc_on || @_jscript_version < 5.7 @*/;

        if (!MainOverlay) {
            MainOverlay           = document.createElement('div');
            MainOverlay.id        = 'MainOverlay';
            MainOverlay.innerHTML = '&nbsp;'

            if (fIE) {
                if (fIE6) {
                    MainOverlay.style.position = 'absolute';
                }

                gPopupResize(MainOverlay);
            }

            MainOverlay.style.filter     = 'alpha(opacity=0)';
            MainOverlay.style.mozOpacity = '0';
            MainOverlay.style.opacity    = '0';
            document.body.appendChild(MainOverlay);
        }

        if (!MainPopup) {
            MainPopup           = document.createElement('div');
            MainPopup.id        = 'MainPopup';
            MainPopup.innerHTML = '<div id="MainPopupPad">&nbsp;</div>';

            if (fIE) {
                if (fIE6) {
                    MainPopup.style.position = 'absolute';
                }

                gPopupResize(MainPopup);
            }

            document.body.appendChild(MainPopup);
        }

        delete fIE;
        delete fIE6;
        return {overlay: MainOverlay, popup: MainPopup };
    }

    function gPopupResize(pElement)
    {
        var fWidth;
        var fHeight;

        if (pElement) {
            if (pElement.id == 'MainOverlay') {
                pElement.style.width  = document.body.clientWidth + 'px';
                pElement.style.height = document.body.clientHeight + 'px';
            } else if (pElement.id == 'MainPopup') {
                fWidth                = document.documentElement.clientWidth || document.body.clientWidth;
                //fHeight               = document.documentElement.clientHeight || document.body.clientHeight;
                fHeight = '350';
                //pElement.style.width  = 840 + 'px';
                pElement.style.height = (fHeight < 31 ? 31 : fHeight) - 30 + 'px';
            } else if (pElement.id == 'MainPopupDiv') {
                fWidth                = document.documentElement.clientWidth || document.body.clientWidth;
                fHeight               = document.documentElement.clientHeight || document.body.clientHeight;
                fHeight = '376';                
                //pElement.style.width  = 838 + 'px';
                pElement.style.height = (fHeight < 67 ? 67 : fHeight) - 66 + 'px';
            }
        }

        delete fWidth;
        delete fHeight;
    }

    function gToggleIframe(pToggle, pURL, pTitle)
    {
        var fCloseDiv;
        var fDivs;
        var fIframe;
        var fTitleDiv;
        var fTransitions = [];

        if (window.ToggleTutorial) {
            ToggleTutorial(0);
        }

        fDivs = gPopupDivs();

        if (fDivs.overlay && fDivs.popup) {
            if (pToggle) {
                fDivs.popup.childNodes[0].style.display = 'block';

                fTransitions = [
                    new Transition(fDivs.popup.childNodes[0]),
                    new Transition(fDivs.overlay)
                ]

                fDivs.popup.style.display = 'block';
                fTransitions[0].options.callBack = function() {
                    fDivs.overlay.style.display = 'block';
                    fTransitions[1].options.callBack = function() {
                        var fCloseDiv  = document.createElement('div');
                        var fTitleDiv  = document.createElement('div');
                        var fIframeDiv = document.createElement('div');
                        // Added IE 8 compatibility mode check (documentMode)
                        var fIE        = (document.documentMode && document.documentMode == 7) || false /*@cc_on || @_jscript_version <= 5.7 @*/;
                        var fIE6       = false /*@cc_on || @_jscript_version < 5.7 @*/;

                        fCloseDiv.id         = 'MainPopupClose';
                        fCloseDiv.innerHTML  = '<div id="MainPopupCloseLink"><a href="#" onclick="gToggleIframe(0); return false;">Close</a></div>';
                        fTitleDiv.id         = 'MainPopupTitle';
                        fTitleDiv.innerHTML  = (pTitle)?pTitle:'&nbsp;';
                        fIframeDiv.id        = 'MainPopupDiv';
                        fIframeDiv.innerHTML = '<iframe id="MainPopupIframe" src="' + pURL + '" frameborder="0"></iframe>';

                        if (fIE) {
                            if (fIE6) {
                                fCloseDiv.style.position  = 'absolute';
                                fIframeDiv.style.position = 'absolute';
                            }

                            gPopupResize(fIframeDiv);
                        }

                        fCloseDiv.appendChild(fTitleDiv);
                        document.body.appendChild(fCloseDiv);
                        document.body.appendChild(fIframeDiv);

                        delete fCloseDiv;
                        delete fIframeDiv;
                        delete fIE;
                        delete fIE6;
                    }
                    fTransitions[1].start([{name: 'opacity', stateFrom: 1, stateTo: 82}]);
                };
                fTransitions[0].start(
                    [
                        {name: 'opacity', stateFrom: 1, stateTo: 100},
                        {name: 'width',   stateFrom: Math.round(fDivs.popup.childNodes[0].offsetWidth / fDivs.popup.offsetWidth), stateTo: 100, unit: 'Percent' },
                        {name: 'height',  stateFrom: Math.round(fDivs.popup.childNodes[0].offsetHeight / fDivs.popup.offsetHeight), stateTo: 100, unit: 'Percent' }
                    ]
                );
            } else {
                fCloseDiv = document.getElementById('MainPopupClose');
                fIframe   = document.getElementById('MainPopupDiv');

                if (fIframe) {
                    fIframe.style.display = 'none';
                    document.body.removeChild(fIframe);
                }

                if (fCloseDiv) {
                    fCloseDiv.style.display = 'none';
                    document.body.removeChild(fCloseDiv);
                }
                
                fTransitions = [
                    new Transition(fDivs.overlay),
                    new Transition(fDivs.popup.childNodes[0])
                ]
                fTransitions[0].options.callBack = function()
                {
                    fDivs.overlay.style.display = 'none';
                    fTransitions[1].options.callBack = function()
                    {
                       fDivs.popup.childNodes[0].style.display = 'none';
                       fDivs.popup.style.display               = 'none';
                    };
                    fTransitions[1].start(
                        [
                            {name: 'opacity', stateFrom: 100, stateTo: 0},
                            {name: 'width',   stateFrom: 100, stateTo: Math.round(5 / fDivs.popup.offsetWidth), unit: 'Percent' },
                            {name: 'height',  stateFrom: 100, stateTo: Math.round(5 / fDivs.popup.offsetHeight), unit: 'Percent' }
                        ]
                    );
                };
                fTransitions[0].start([{name: 'opacity', stateFrom: 75, stateTo: 0}]);
            }
        }

        delete fCloseDiv;
        delete fTitleDiv;
        delete fDivs;
        delete fIframe;
        delete fTransitions;
    }

    function Transition(pSubject)
    {
        this.options    = { duration: 500, framerate: 50, callBack: null };
        this.properties = [];
        this.subject    = pSubject;
        this.time       = 0;
        this.timer;

        this.complete = function()
        {
            if (this.stopTimer()) {
                if (typeof(this.options.callBack) == 'function') {
                    this.options.callBack();
                }
            }
        }

        this.calculate = function(pFrom, pTo, pDelta)
        {
            return (pTo - pFrom) * pDelta + pFrom;
        }

        this.start = function(pProperties)
        {
            if (this.subject) {
                this.properties = pProperties;
                this.time       = 0;
                this.startTimer();
            }
        };

        this.startTimer = function()
        {
            var fCallback
            var fInstance = this;

            fCallback = function()
            {
                fInstance.step();
            };

            this.time  = (+new Date()) - this.time;
            this.timer = setInterval(fCallback, Math.round(1000 / this.options.framerate));
        };

        this.step = function()
        {
            var fDelta;
            var fTime = (+new Date());

            if (fTime < this.time + this.options.duration){
                fDelta = this.transition((fTime - this.time) / this.options.duration);
                this.setStyles(fDelta);
            } else {
                this.setStyles(1);
                this.complete();
            }
        };

        this.stopTimer = function()
        {
            if (this.timer)  {
                this.time = (+new Date()) - this.time;
                clearTimeout(this.timer);
                clearInterval(this.timer);
                this.timer = null;
                return true;
            } else {
                return false;
            }
        };

        this.setStyles = function(pDelta)
        {
            var fProperty;
            var a = 0;
            var z = 0;

            if (typeof(this.properties) == 'object') {
                for (a = 0, z = this.properties.length; a < z; a++) {
                    fProperty = this.properties[a];
                    this.setStyle(fProperty.name, this.calculate(fProperty.stateFrom, fProperty.stateTo, pDelta), fProperty.unit);
                }
            }

            delete fProperty;
            delete a;
            delete z;
        }

        this.setStyle = function(pProperty, pValue, pUnit)
        {
            if (this.subject) {
                if (this.subject.style) {
                    switch(pProperty) {
                    case ('height'):
                    case ('width'):
                        this.subject.style[pProperty] = pValue + (pUnit == 'Percent' ? '%' : 'px');
                        break;
                    case ('opacity'):
                        this.subject.style.opacity    = (pValue / 100);
                        this.subject.style.mozOpacity = (pValue / 100);
                        this.subject.style.filter     = 'alpha(opacity=' + pValue + ')';
                    default:
                        break;
                    }
                }
            }
        };

        this.transition = function(pValue)
        {
            return -(Math.cos(Math.PI * pValue) - 1) / 2;
        };
    }
    
    function gPopupUpdateTitle (pTitle){

      fDivs = gPopupDivs();
      if (fDivs.overlay && fDivs.popup) {    
        var fTitleDiv = document.getElementById('MainPopupTitle');
        if(fTitleDiv){fTitleDiv.innerHTML  = (pTitle)?pTitle:'&nbsp;';}
        delete fTitleDiv;
      }    
    }
