// use css class: dialogBig, dialogMed, dialogSmall, dialogWarn for different formats
function RenderDialog(result, css) {
    CreateOverlayDialog(result, false, css);
}

// Function to create overlay dialogs with fixed sizes
function CreateOverlayDialog(id, holder, className) {

    window.onresize = ResizeOverlayDialog;

    var mainDialog = $get("maindialog");

    if (mainDialog && mainDialog.innerHTML) {
        // remove objects from dom
        for (var i = 0; i < mainDialog.childNodes.length; i++) {
            var item = mainDialog.childNodes[i];
            mainDialog.removeChild(item);
        }
    }

    var overlaydialogclass = function() {
        this.dialogdiv = null;
        this.dialoglayer = null;
    }

    overlaydialogclass.prototype = {

        init: function() {

            if (!document.getElementsByTagName) {
                return;
            }
            captureMousePosition;

            // For stacked windows, do not stack the covers
            var testcover = $get("maindialogcover");

            if (testcover == null) {
                this.dialoglayer = document.createElement("div");
                this.dialoglayer.id = "maindialogcover";
                this.dialoglayer.className = "maindialogcover";
                this.dialoglayer.style.width = windowsize.width + "px";
                this.dialoglayer.style.height = windowsize.height + "px";
                this.dialoglayer.style.top = '0px';

                //Enable Iframe
                if (isIE) {
                    var iframedialog = $get("iframedialog");

                    if (iframedialog == null) {
                        iframedialog = document.createElement('iframe');
                        iframedialog.setAttribute('id', 'iframedialog');
                        iframedialog.style.display = 'none';
                        iframedialog.style.top = '0px';
                        iframedialog.style.left = '0px';
                        iframedialog.style.position = 'absolute';
                        iframedialog.src = iframesrc;
                        iframedialog.frameBorder = '0';
                        iframedialog.scrolling = 'no';

                        iframedialog.style.width = windowsize.width + "px";
                        iframedialog.style.height = windowsize.height + "px";
                        iframedialog.style.zIndex = this.dialoglayer.style.zIndex - 1;
                        iframedialog.style.display = this.dialoglayer.style.display;
                    }

                    this.dialoglayer.appendChild(iframedialog);
                }
                document.body.appendChild(this.dialoglayer);
            }

            if (id) {
                this.dialogdiv = $get("maindialog");

                if (this.dialogdiv == null) {
                    this.dialogdiv = document.createElement("div");
                    this.dialogdiv.id = "maindialog";

                    //<div class="dialogTop">
                    this.dialogTop = document.createElement("div");
                    this.dialogTop.className = "dialogTop";
                    this.dialogdiv.appendChild(this.dialogTop);

                    //<a href="javascript:CloseDialog();" class="btnClose" title="Close">
                    this.closebutton = document.createElement("a");
                    this.closebutton.href = "javascript:CloseDialog('" + id + "','" + holder + "');";
                    this.closebutton.className = "btnClose";
                    this.dialogTop.appendChild(this.closebutton);

                    //<img src="/files/22222222-2222-2222-2222-222222222222/neutral/base/img/dialogs/structure/btn-close.png" alt="Close" width="20" height="20" />
                    this.closebuttonimg = document.createElement("img");
                    this.closebuttonimg.src = src = "/files/22222222-2222-2222-2222-222222222222/neutral/base/img/dialogs/structure/btn-close.png";
                    this.closebuttonimg.width = "20";
                    this.closebuttonimg.height = "20";
                    this.closebutton.appendChild(this.closebuttonimg);

                    //  <div id="dialogTop" class="dialogMove"></div>
                    // skipped

                    //  <div class="dialogLogo"></div>
                    this.dialogLogo = document.createElement("div");
                    this.dialogLogo.className = "dialogLogo";
                    this.dialogTop.appendChild(this.dialogLogo);

                    //</div>
                    //<div class="dialogMiddle contentBox">
                    this.dialogMiddle = document.createElement("div");
                    this.dialogMiddle.className = "dialogMiddle contentBox";
                    this.dialogdiv.appendChild(this.dialogMiddle);

                    //  <div class="reposition"></div>
                    this.reposition = document.createElement("div");
                    this.reposition.id = "reposition";
                    this.reposition.className = "reposition";
                    this.dialogMiddle.appendChild(this.reposition);

                    //<div class="dialogBottom"></div>
                    this.dialogBottom = document.createElement("div");
                    this.dialogBottom.className = "dialogBottom";
                    this.dialogdiv.appendChild(this.dialogBottom);
                }
                document.body.appendChild(this.dialogdiv);
            }
        }
    };

    var overlaydialog = new overlaydialogclass();
    overlaydialog.init();

    mainDialog = $get("maindialog");

    var minXOffset = 200, minYOffset = 200;

    // if id is null, mainDialog will be null
    if (mainDialog) {
        if (className) {
            mainDialog.className = "maindialog " + className;
        }
        else {
            mainDialog.className = "maindialog";
        }
    }

    if (id) {
        $get("reposition").appendChild($get(id));
    }

    $get('maindialogcover').style.display = 'block';

    if (id) {
        $get("maindialog").style.display = 'block';
        $get("maindialog").style.visibility = 'hidden';
    }

    dialogfadein($get("maindialog"));

    ResizeOverlayDialog();
}


function ResizeOverlayDialog() {
    capturePageDimensions();

    mainDialog = $get("maindialog");

    if (mainDialog) {
        var minXOffset = 200;
        var minYOffset = parseInt(getStyle(mainDialog, 'height').replace('px', '')) / 2;
        if (isNaN(minYOffset)) {
            
            if (mainDialog.className.indexOf("dialogGiant") > 0)
                minYOffset = 350;
            else if (mainDialog.className.indexOf("dialogExtraLarge") > 0)
                minYOffset = 300;
            else if (mainDialog.className.indexOf("dialogLarge") > 0)
                minYOffset = 200;
            else if (mainDialog.className.indexOf("dialogMedium") > 0)
                minYOffset = 200;
            else if (mainDialog.className.indexOf("dialogSmall") > 0)
                minYOffset = 200;
            else if (mainDialog.className.indexOf("dialogSlideshow") > 0)
                minYOffset = 200;
            else if (mainDialog.className.indexOf("dialogWarning") > 0)
                minYOffset = 200;
            else
                minYOffset = 200;
        }

        var divWidth = getStyle(mainDialog, 'width');
        if (divWidth) {
            divWidth = parseInt(divWidth.replace('px', ''))
            if (divWidth > 10) {
                minXOffset = divWidth / 2;
            }
        }

        var divHeight = getStyle(mainDialog, 'height');
        if (divHeight) {
            divHeight = parseInt(divHeight.replace('px', ''))
            if (divHeight > 10) {
                minYOffset = divHeight / 2;
            }
        }

        if (divWidth >= windowsize.width) {
            mainDialog.style.left = "0px";
        }
        else {
            mainDialog.style.left = (Math.floor(windowsize.windowWidth / 2 - minXOffset)) + "px";
        }
        if (divHeight >= windowsize.height) {
            mainDialog.style.top = "0px";
        }
        else {
        	if ((Math.floor(windowsize.windowHeight / 2 - minYOffset)) + windowsize.scrollTop > 0)
        		mainDialog.style.top = (Math.floor(windowsize.windowHeight / 2 - minYOffset)) + windowsize.scrollTop + "px";
			else
				mainDialog.style.top = "0px";
        }

        mainDialogCover = $get('maindialogcover')
        if (mainDialogCover) {
            if (windowsize.windowWidth < 640) {
                mainDialogCover.style.width = "640px";
            }
            else {
                mainDialogCover.style.width = (isIE ? windowsize.windowWidth : windowsize.windowWidth - 18) + "px";
            }
            if (windowsize.windowHeight < 480) {
                mainDialogCover.style.height = "480px";
            }
            else {
                mainDialogCover.style.height = windowsize.height + "px";
            }

            var iframedialog = $get('iframedialog')
            if (iframedialog) {
                if (iframedialog.style) {
                    if (windowsize.width < 640) {
                        iframedialog.style.width = "640px";
                    }
                    else {
                        iframedialog.style.width = windowsize.width + "px";
                    }
                    if (windowsize.height < 480) {
                        iframedialog.style.height = "480px";
                    }
                    else {
                        if (window.document.body.offsetHeight > windowsize.height)
                            iframedialog.style.height = window.document.body.offsetHeight + "px";
                        else
                            iframedialog.style.height = windowsize.height + "px";
                    }
                }
            }

            $get("maindialog").style.visibility = 'visible';
        }
    }
}

function CloseDialog(id, holder) {
    $get(holder).appendChild($get(id));
    var maindlg = $get("maindialog");
    var cover = $get("maindialogcover");
    var parent = null;
    if (cover) {
        parent = cover.parentNode;
        cover.style.display = 'none';
        if (parent) {
            parent.removeChild(cover);
        }
    }
    if (maindlg) {
        parent = maindlg.parentNode;
        if (maindlg.innerHTML) {
            maindlg.innerHTML = null;
        }
        maindlg.style.display = 'none';
        if (parent) {
            parent.removeChild(maindlg);
        }
    }
}

//-------------------------------------------
var dialogobj;
function dialogfadein(item) {
	dialogobj = item;
	jQuery(item).fadeIn("slow");
}
function dialogfadeout() {
	jQuery(dialogobj).fadeOut(500, CloseDialog);
	return false;
}