﻿var ModalBackgroundIsAdded = false;

function ShowDialog(DialogId) {
    if (ModalBackgroundIsAdded == false) {
        AddDialogBackground();
    }
    
    $('#' + DialogId).css("display","block");
    $('#' + DialogId).css("z-index","999");
    ResizeDialog();
    $('.ModalBackground').css("display","block");
    $('.ModalBackground').css("z-index","998");

}

function ShowDialogIframe(Url, DialogId) {
    // random querystring is to prevent iframe caching!!
    if (Url.indexOf('?') > 0) {
        Url += '&';
    } else {
        Url += '?';
    }
    Url += 'random=' + (new Date()).valueOf();
    
    if (document.getElementById(DialogId) == null) {
        var HTML = '<div id="' + DialogId + '" class="ModalDialog">'
        HTML += '    <iframe frameborder="0" src="' + Url + '"></iframe>'
        HTML += '</div>'
        $("body").prepend(HTML);
    } else {
        $("#" + DialogId + " > iframe" ).attr("src",Url);
    }
    
    ShowDialog(DialogId);
}

/*
Hide All div's with class = ModalDialog
*/

function HideDialog() {
    $('.ModalDialog').css("display","none");
    $('.ModalBackground').css("display","none");
    
    //clear all iframes
    $(".ModalDialog > iframe" ).attr("src",'');
}


/*
Resize All div's with class = ModalDialog
*/
function ResizeDialog() {
    
    var top = ((($(window).height()) -  ($('.ModalDialog').height()))/2) + document.body.scrollTop;
    var left = ((($(window).width()) -  ($('.ModalDialog').width()))/2) + document.body.scrollLeft;
    
    
    $('.ModalDialog').css("top",top);
    $('.ModalDialog').css("left",left);
    $('.ModalBackground').css("left",document.body.scrollLeft);
    $('.ModalBackground').css("top",document.body.scrollTop);
    $('.ModalBackground').css("width",$(window).width());
    $('.ModalBackground').css("height",$(window).height());
}

$(window).resize(function(){
    ResizeDialog();
});

$(window).scroll(function(){
    ResizeDialog();
});

function AddDialogBackground() {
    $("body").prepend('<div id="ModalBackground" class="ModalBackground" style="display: none;"></div>');
    ModalBackgroundIsAdded = true;
}


