﻿ var ModalBox = new function()
 {
    this.FadePercent = 90;
    this.FadeColor = '#F0EEE5';
    this._FadeLayerID = 'ModalBox_FadeLayer';
    this._ModalBoxID = 'ModalBox_Container';
    this._PerpetualPlacement = false;
    
    this.IsVisible = function()
    {
        return (document.getElementById(this._ModalBoxID)) ? 1 : 0;
    }; // IsVisible()
    this.CheckKeyPress = function(e)
    {
        var kC = (window.event) ? event.keyCode : e.keyCode;
        var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
        if (kC==Esc) { ModalBox.Clear(); }
    }; // CheckKeyPress(e)
    this.Show = function(content)
    {
        this.ShowFade();
        this.ShowBox();
        window.onresize = ModalBox.SetParameters;
        this.SetContent(content);
    }; // Show(content)
    this.ShowContents = function(objectID)
    {
        this.Show(document.getElementById(objectID).innerHTML);
    }; // ShowContents = function(objectID)
    this.Clear = function()
    {
        this.ClearFade();
        this.ClearBox();
        window.onresize = null;
        document.onkeypress = null;
    }; // Clear()
    
    this.ShowFade = function()
    {
        this.ClearFade();
        
        var wd = General.GetWindowDimensions();
        var c = document.createElement('div');
        c.setAttribute('id', this._FadeLayerID);
        
        if(!General.IsExplorer()) { c.setAttribute('style', "position: absolute; top: 0px; left: 0px; width: 10px; height: 10px; background-color: " + this.FadeColor + "; -khtml-opacity: " + (this.FadePercent/100) + "; -moz-opacity: " + (this.FadePercent/100) + "; opacity: " + (this.FadePercent/100) + ";"); }
        else
        {
            c.style.position = 'absolute';
            c.style.top = '0px';
            c.style.left = '0px';
            c.style.width = '10px';
            c.style.height = '10px';
            c.style.backgroundColor = this.FadeColor;
            c.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + this.FadePercent + ')';
            c.style.opacity = this.FadePercent;
        }
        
        document.body.appendChild(c);
        
        ModalBox.SetFadeDimensions();
    }; // ShowFade()
    this.ClearFade = function()
    {
        var d = document.getElementById(this._FadeLayerID);
        if(d) { document.body.removeChild(d); }
    }; // ClearFade()
    
    this.ShowBox = function()
    {
        this.ClearBox();
        
        var objContainer = document.createElement('div');
        objContainer.setAttribute('id', this._ModalBoxID);
        if(General.IsExplorer()) { objContainer.setAttribute('className', 'box'); }
        else { objContainer.setAttribute('class', 'box'); }

        objContainer.innerHTML = '<div class="border-top"><div class="border-right"><div class="border-bot"><div class="border-left"><div class="left-top-corner"><div class="right-top-corner"><div class="right-bot-corner"><div class="left-bot-corner"><div class="inner">{MODAL_CONTENT_GOES_HERE}<div class="alignright">ESC or <a href="javascript: ModalBox.Clear();">Close</a></div></div></div></div></div></div></div></div></div></div>';

        document.body.appendChild(objContainer);
        
        document.onkeypress = ModalBox.CheckKeyPress;
    }; // ShowBox()
    this.ClearBox = function()
    {
        var d = document.getElementById(this._ModalBoxID);
        if(d) { document.body.removeChild(d); }
    }; // ClearBox()
    
    this.SetParameters = function()
    {
        ModalBox.SetFadeDimensions();
        ModalBox.SetModalPosition();
    }; // SetParameters()
    this.SetFadeDimensions = function()
    {
        var obj = document.getElementById(this._FadeLayerID);
        if(obj)
        {
            var wd = General.GetWindowDimensions();
            var pw = obj.offsetWidth;
            var ph = obj.offsetHeight;
            
            obj.style.width = (document.body.offsetWidth > wd[0]) ? document.body.offsetWidth + 'px' : wd[0] + 'px';
            obj.style.width = (pw > obj.offsetWidth) ? pw + 'px' : obj.offsetWidth + 'px';

            obj.style.height = (document.body.offsetHeight > wd[1]) ? document.body.offsetHeight + 'px' : wd[1] + 'px';
            obj.style.height = (ph > obj.offsetHeight) ? ph + 'px' : obj.offsetHeight + 'px';
        }
    }; // SetFadeDimensions()
    this.SetModalPosition = function()
    {
        var obj = document.getElementById(this._ModalBoxID);
        if(obj)
        {
            var wd = General.GetWindowDimensions();
            var sp = General.GetScrollPositions();
            obj.style.top = (sp[1] + (wd[1] / 2)) - (obj.offsetHeight / 2) + 'px';
            obj.style.left = (sp[0] + (wd[0] / 2)) - (obj.offsetWidth / 2) + 'px';
            
            if(this._PerpetualPlacement) { setTimeout("ModalBox.SetModalPosition()", 100); }
        }
    }; // SetModalPosition()
    
    this.GetModalObject = function()
    {
        return document.getElementById(this._ModalBoxID);
    }; // GetModalObject()
    
    this.SetContent = function(content)
    {
        var obj = document.getElementById(this._ModalBoxID);
        obj.innerHTML = obj.innerHTML.replace(/{MODAL_CONTENT_GOES_HERE}/, content);
        
        this.SetParameters();
    }; // SetContent(content)
 };