Bootstrap();
var DOMIsLoaded
function Bootstrap()
{
	DOMIsLoaded = false;
	$(document).ready(function(){DOMIsLoaded = true;});
}
function PopupWindow(CreateInWindow, TempUrl)
{
    this.Mask = CreateInWindow.document.createElement("div");
    this.Container = CreateInWindow.document.createElement("div");
    this.Inner = CreateInWindow.document.createElement("div");
    this.TitleBar = CreateInWindow.document.createElement("div");
    this.Title = CreateInWindow.document.createElement("div");
    this.Frame = CreateInWindow.document.createElement("iframe");
    this.CloseButton = CreateInWindow.document.createElement("div");
    this.Visible = false;
    this.Window = CreateInWindow;
    this.TempUrl = TempUrl

    this.Show = PopupWindowShow;
    this.Hide = PopupWindowHide;
    this.Center = PopupWindowCenter;
    this.ViewPortHeight = PopupWindowViewPortHeight;
    this.ViewPortWidth = PopupWindowViewPortWidth;

    this.Frame.style.width = "100%";
    this.Frame.style.height = "100%";
    this.Frame.style.backgroundColor = "transparent";
    this.Frame.scrolling = "auto";
    this.Frame.frameBorder = 0;
    this.Frame.allowTransparency = true;
    this.Frame.src = this.TempUrl;
    PopupWindowAddEvent(this.CloseButton, "click", function(){PopupWindowHide(false);});

    this.Mask.className = "popupMask";
    this.Container.className = "popupContainer";
    this.Inner.className = "popupInner";
    this.TitleBar.className = "popupTitleBar";
    this.Title.className = "popupTitle";
    this.Frame.className = "popupFrame";
    this.CloseButton.className = "popupCloseButton";

	AddToDOM(CreateInWindow);
}
function AddToDOM(CreateInWindow)
{   	
	if (DOMIsLoaded)
	{	   
//    this.Container.insertAdjacentElement("afterBegin", this.Inner);
//    this.Inner.insertAdjacentElement("afterBegin", this.TitleBar);
//    this.Inner.insertAdjacentElement("beforeEnd", this.Frame);
//    this.TitleBar.insertAdjacentElement("afterBegin", this.Title);

        this.Container.insertBefore(this.Inner, this.Container.firstChild);
        this.Inner.insertBefore(this.TitleBar, this.Inner.firstChild);
        this.Inner.appendChild(this.Frame);
        this.TitleBar.insertBefore(this.Title, this.TitleBar.firstChild);
        this.TitleBar.insertBefore(this.CloseButton, this.TitleBar.firstChild);
    
        CreateInWindow.document.body.appendChild(this.Mask);
        CreateInWindow.document.body.appendChild(this.Container);

//    CreateInWindow.document.body.insertAdjacentElement("beforeEnd", this.Mask);
//    CreateInWindow.document.body.insertAdjacentElement("beforeEnd", this.Container);

        PopupWindows[PopupWindows.length] = this;
    }
    else
		setTimeout(function(){AddToDOM(CreateInWindow);}, 300);
}
function PopupWindowShow(Url, Width, Height, ReturnFunction)
{
    this.Visible = true;
    this.Mask.style.display = "block";
    this.Container.style.display = "block";

    this.Width = Width;
    this.Height = Height;

    this.Center(Width, Height);

    this.Frame.src = Url;
    this.ReturnFunction = ReturnFunction;

    this.Window.setTimeout("PopupWindowRefreshTitles();", 200);
}
function PopupWindowRefreshTitles()
{
    for (var i = 0; i < PopupWindows.length; i++)
    {
        var pu = PopupWindows[i];

        if (pu.Visible)
        {
            if (pu.Frame.contentWindow.document.title == null || pu.Frame.contentWindow.document.title == "Loading...")
            {
                pu.Window.setTimeout("PopupWindowRefreshTitles();", 200);
            }
            else
            {
                pu.Title.innerHTML = pu.Frame.contentWindow.document.title;
            }
        }
    }
}
function PopupWindowHide(CallReturnFunction)
{
	this.Visible = false;

	if (this.Mask == null)
	{
		return;
	}
	else
	{
        this.Mask.style.display = "none";
        this.Container.style.display = "none";

        if (CallReturnFunction && this.ReturnFunction != null)
        {
            if (this.ReturnValue != null)
            {
                this.ReturnFunction(this.ReturnValue);
            }
            else
            {
                this.ReturnFunction();
            }
        }

	    this.Title.innerHTML = "";
	    this.Frame.src = this.TempUrl;
	}
}
function PopupWindowCenter(Width, Height)
{
    if (this.Visible)
    {
        if (Width == null || isNaN(Width))
        {
            if (this.Width != null)
            {
                Width = this.Width;
            }
            else
            {
                Width = this.Container.offsetWidth;
            }
        }

        if (Height == null || isNaN(Height))
        {
            if (this.Height != null)
            {
                Height = this.Height;
            }
            else
            {
                Height = this.Container.offsetHeight;
            }
        }

        var FullHeight = this.ViewPortHeight();
        var FullWidth = this.ViewPortWidth();

        var Left = 0;
        var Top = 0;

        if (this.Window.pageYOffset)
        {
	        Left = this.Window.pageXOffset;
	        Top = this.Window.pageYOffset;
        }
        else if (this.Window.document.documentElement && this.Window.document.documentElement.scrollTop)
        {
	        Left = this.Window.document.documentElement.scrollLeft;
	        Top = this.Window.document.documentElement.scrollTop;
        }
        else if (this.Window.document.body)
        {
	        Left = this.Window.document.body.scrollLeft;
	        Top = this.Window.document.body.scrollTop;
        }

        this.Mask.style.height = FullHeight + "px";
        this.Mask.style.width = FullWidth + "px";
        this.Mask.style.top = Top + "px";
        this.Mask.style.left = Left + "px";

        var TopMargin = Top + ((FullHeight - (Height + parseInt(this.TitleBar.offsetHeight, 10))) / 2);

        if (TopMargin < 0)
        {
            TopMargin = 0;
        }

        this.Container.style.top = TopMargin + "px";
        this.Container.style.left = (Left + ((FullWidth - Width) / 2)) + "px";
	    this.Container.style.width = Width + "px";
	    this.Container.style.height = (Height + parseInt(this.TitleBar.offsetHeight, 10)) + "px";

        this.Frame.style.width = parseInt(this.TitleBar.offsetWidth, 10) + "px";
        this.Frame.style.height = Height + "px";
    }
}
function PopupWindowViewPortHeight()
{
	if (this.Window.innerHeight != window.undefined)
	{
	    return this.Window.innerHeight;
	}

	if (this.Window.document.compatMode == 'CSS1Compat')
	{
	    return this.Window.document.documentElement.clientHeight;
	}

	if (this.Window.document.body)
	{
	    return this.Window.document.body.clientHeight;
	}

	return window.undefined; 
}
function PopupWindowViewPortWidth()
{
	if (this.Window.innerWidth != window.undefined)
	{
	    return this.Window.innerWidth;
	}

	if (this.Window.document.compatMode == 'CSS1Compat')
	{
	    return this.Window.document.documentElement.clientWidth;
	}

	if (this.Window.document.body)
	{
	    return this.Window.document.body.clientWidth;
	}

	return window.undefined;
}

var PopupWindows = new Array();

PopupWindowAddEvent(window, "resize", PopupWindowsCenter);
PopupWindowAddEvent(window, "scroll", PopupWindowsCenter);

function PopupWindowsCenter()
{
    for (var i = 0; i < PopupWindows.length; i++)
    {
        PopupWindows[i].Center();
    }
}
function PopupWindowAddEvent(obj, evType, fn)
{
    if (obj.addEventListener)
    {
        obj.addEventListener(evType, fn, false);
        return true;
    }
    else if (obj.attachEvent)
    {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    }
    else
    {
        return false;
    }
}
