﻿//==============================
//  Centerize an element to the screen
//  - Usage: $(selector).center();
//==============================
jQuery.fn.center = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

//==============================
//  Page blocking in Ajax request
//==============================
//A div with ajaxIndContainer class need to added to the page before use
//This function is called when the ajax starts
function blockPage() {
    $.blockUI({
        message: $(".ajaxIndContainer"),
        baseZ: 1008,
        fadeIn: 0,
        fadeOut: 500,
        css: {
            width: 'auto',
            top: '0',
            left: $(window).width() - $(".ajaxIndContainer").width() - 10 //scrollbar
        },
        overlayCSS: {
            opacity: 0.001
        }
    });
}

//This function is called when the request is cancelled
function cancelRequest() {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm.get_isInAsyncPostBack())
        prm.abortPostBack();
    $.unblockUI();
}

//This function is called when the ajax completed
function unblockPage() {
    $.unblockUI();
}
function openWindow(url, width, height, isModal) {
    if (isModal === true) {
        if (window.showModalDialog) {
            window.showModalDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:false","center:yes");
        } else {
            var options = "height=" + height
            options += ", width=" + width
            options += ", toolbar=no"
            options += ", directories=no"
            options += ", status=no"
            options += ", menubar=no"
            options += ", scrollbars=no"
            options += ", resizable=no"
            options += ", modal=yes"
            window.open(url, '_blank', options);
        }
    }
    else {
        if (window.showModelessDialog) {
            window.showModelessDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;status:false");
        } else {
            var options = "height=" + height
            options += ", width=" + width
            options += ", toolbar=no"
            options += ", directories=no"
            options += ", status=no"
            options += ", menubar=no"
            options += ", scrollbars=no"
            options += ", resizable=no"
            window.open(url, '_blank', options);
        }
    }
}

//Gets the parent window
function getParentWindow() {
    if (window.dialogArguments)
        return window.dialogArguments;
    else
        return opener;
}
