﻿// Modal Dialogue Box
// Todd Bennion
// 9/15/08

/* If you change the size of the loading image, change
these values to the width and height of the image, and the 
source. */
var modal_loadingwidth = 195;
var modal_loadingheight = 195;
var modal_loadingsrc = "/images/loading.gif";

var modal_screenwidth;
var modal_screenheight;
var modal_popupwidth;
var modal_popupheight;
var modal_popupsource;

var modal_boolOverlayVisible = false;
var modal_boolIframeVisible = false;

var modal_overlay;
var modal_workingImage;
var modal_container;
var modal_iframe;

// properties

function modal_Hello() 
{
    return "Hello";

}
function modal_getOverlay()
{
    try
    {
        if (modal_overlay)
            return modal_overlay;
        var element = document.getElementById("modal-overlay");
        if (element)
        {
            modal_overlay = element;
            return element;
        }
        else
        {
            var element = document.createElement('div');
            element.id = "modal-overlay";
            if (document.layers)
                document.body.appendChild(element);
            else
                document.getElementsByTagName("body")[0].appendChild(element);
            modal_overlay = element;
            return element;
        }
    }
    catch (Error)
    {
        alert(Error);
    }
}

function modal_getWorkingObject()
{
    if (modal_workingImage)
        return modal_workingImage;
    var element = document.getElementById("modal-loading");
    if (element)
    {
        modal_workingImage = element;
        return element;
    }
    else
    {
        modal_setWindowSizes();
        var element = document.createElement("div");
        element.id = "modal-loading";
        element.innerHTML = "<img alt=\"Loading....\" src=\"" + modal_loadingsrc + "\" style=\"width:" + modal_loadingwidth + "px; height:" + modal_loadingheight + "px; margin-top:" + ((modal_screenheight - modal_loadingheight) / 2) + "px; margin-left:" + ((modal_screenwidth - modal_loadingwidth) / 2) + "px;\" />";
        if (document.layers)
            document.body.appendChild(element);
        else
            document.getElementsByTagName("body")[0].appendChild(element);
        window.onresize = modal_eventResetWorkingImagePosition;
	    window.onscroll = modal_eventResetWorkingImagePosition;
	    modal_workingImage = element;
	    return element;
    }
}

function modal_getModalContainer()
{
    if (modal_container)
        return modal_container;
    
    var element = document.getElementById("modal-container");
    if (element)
    {
        modal_container = element;
        return element;
    }
    else
    {
        modal_setWindowSizes();
        var element = document.createElement("div");
        element.id = "modal-container";
        element.style.width = modal_popupwidth + "px";
        element.style.height = modal_popupheight + "px";
        element.style.marginTop = ((modal_screenheight - modal_popupheight) / 2) + "px";
        element.style.marginLeft = ((modal_screenwidth - modal_popupwidth) / 2) + "px";
        if (document.layers)
            document.body.appendChild(element);
        else
            document.getElementsByTagName("body")[0].appendChild(element);
        window.onresize = modal_eventResetIFramePosition;
	    window.onscroll = modal_eventResetIFramePosition;
	    modal_container = element;
        return element;
    }
}

function modal_getIframe()
{
    if (modal_iframe)
        return modal_iframe;
    var modalHolder = modal_getModalContainer();
    if (modalHolder.childNodes.length == 0)
    {
        modal_setWindowSizes();
        var element = document.createElement("iframe");
        element.style.width = modal_popupwidth + "px";
        element.style.height = modal_popupheight + "px";
        element.frameBorder = 0;
        element.allowTransparency = true;
        element.scrolling = "no";
        element.src = modal_popupsource;
        modalHolder.appendChild(element);
        modal_iframe = element;
        return element;
    }
    else
    {
        modal_iframe = modalHolder.childNodes[0];
        return modalHolder.childNodes[0];
    }
}
// internal functions

function modal_setWindowSizes()
{
    // compressed so I don't have to look at it :)
    if (window.innerWidth){modal_screenwidth=window.innerWidth;}else if (document.documentElement && document.documentElement.clientWidth){modal_screenwidth=document.documentElement.clientWidth;}else if (document.body){modal_screenwidth=document.body.clientWidth;}if (window.innerHeight){modal_screenheight=window.innerHeight;}else if (document.documentElement && document.documentElement.clientHeight){modal_screenheight=document.documentElement.clientHeight;}else if (document.body){modal_screenheight=document.body.clientHeight;}
}

// events
function modal_eventResetIFramePosition()
{
    try
    {
        if (modal_boolIframeVisible)
        {
            var modalContainer = modal_getModalContainer();
            modal_setWindowSizes();
            modal_overlay.style.height = modal_screenheight;
            modalContainer.style.marginTop = (modal_screenheight - modal_popupheight) / 2 + "px";
            modalContainer.style.marginLeft = (modal_screenwidth - modal_popupwidth) / 2 + "px";
        }
    }
    catch (Error) {}
}

function modal_eventResetWorkingImagePosition()
{
    try
    {
        if (modal_boolOverlayVisible)
        {
            var workingObject = modal_getWorkingObject();;
            modal_setWindowSizes();
            modal_overlay.style.height = modal_screenheight;
            workingObject.style.marginTop = (modal_screenheight - modal_loadingheight) / 2;
            workingObject.style.marginLeft = (modal_screenwidth - modal_loadingwidth) / 2;
        }
    }
    catch (Error) {}
}

// public functions

function modal_ShowOverlay()
{
    modal_boolOverlayVisible = true;
    var overlay = modal_getOverlay();
    modal_setWindowSizes();
    modal_overlay.style.height = modal_screenheight;
    overlay.style.display = "block";
}

function modal_HideOverlay()
{
    modal_boolOverlayVisible = false;
    var overlay = modal_getOverlay();
    overlay.style.display = "none";
}

function modal_ShowWorkingImage()
{
    modal_ShowOverlay();
    var working = modal_getWorkingObject();
    working.style.display = "block";
    modal_boolOverlayVisible = true;
}

function modal_HideWorkingImage()
{
    var working = modal_getWorkingObject();
    working.style.display = "none";
    modal_boolOverlayVisible = false;
}

function modal_Open(source, width, height )
{
    modal_ShowOverlay();
    modal_ShowWorkingImage();
    modal_boolIframeVisible = true;
    modal_popupheight = height;
    modal_popupwidth = width;
    modal_popupsource = source;
    var iframe = modal_getIframe();
    iframe = iframe;
}

function modal_RelocateAndResize(source, width, height)
{
    modal_ShowOverlay();
    modal_ShowWorkingImage();
    modal_boolIframeVisible = true;
    modal_popupsource = source;
    var iframe = modal_getIframe();
    iframe.src = source;
    modal_ResizePopup(width, height);
}

function modal_ResizePopup(width, height)
{
    try
    {    
        modal_popupheight = height;
        modal_popupwidth = width;
        modal_setWindowSizes();
        
        var modalContainer = modal_getModalContainer();
        var modalIframe = modal_getIframe();
        
        modal_overlay.style.height = modal_screenheight;
        
        modalContainer.style.width = modal_popupwidth + "px";
        modalContainer.style.height = modal_popupheight + "px";
        
        modalIframe.style.width = modal_popupwidth + "px";
        modalIframe.style.height = modal_popupheight + "px";    
    }
    catch (Error) 
    {
        alert(Error);
    }
}

function modal_ResizeAndCenterPopup(width, height)
{
    try
    {    
        modal_popupheight = height;
        modal_popupwidth = width;
        modal_setWindowSizes();
        
        var modalContainer = modal_getModalContainer();
        var modalIframe = modal_getIframe();
        
        modal_overlay.style.height = modal_screenheight;
        
        modalContainer.style.width = modal_popupwidth + "px";
        modalContainer.style.height = modal_popupheight + "px";
        
        modalIframe.style.width = modal_popupwidth + "px";
        modalIframe.style.height = modal_popupheight + "px";
        
        modalContainer.style.marginTop = (modal_screenheight - modal_popupheight) / 2 + "px";
        modalContainer.style.marginLeft = (modal_screenwidth - modal_popupwidth) / 2 + "px";
    }
    catch (Error) 
    {
        alert(Error);
    }
}

function modal_Hide()
{
    modal_ShowOverlay();
    modal_ShowWorkingImage();
    var container = modal_getModalContainer();
    container.style.marginLeft = "-100000px";
    modal_boolIframeVisible = false;
}

function modal_HideAndClose()
{
    modal_ShowOverlay();
    modal_ShowWorkingImage();
    var container = modal_getModalContainer();
    container.style.marginLeft = "-100000px";
    modal_boolIframeVisible = false;
    setTimeout("refreshParent();", 1000);
}

function refreshParent() 
{
    self.parent.location = self.parent.location;
}

function modal_Close()
{
    modal_ShowOverlay();
    modal_ShowWorkingImage();
    var container = modal_getModalContainer();
    var parent = container.parentNode;
    parent.removeChild(container);
    modal_iframe = null;
    modal_container = null;
    modal_boolIframeVisible = false;
    modal_HideWorkingImage();
    modal_HideOverlay();
}

function modal_Restore()
{
    modal_ShowOverlay();
    modal_HideWorkingImage();
    modal_boolIframeVisible = true;
    // just using the event function to restore the iframe to its position.
    modal_eventResetIFramePosition();   
    var element = modal_getModalContainer();
}
