﻿WebBase =
{
    SessionInvalid: false,
    NotifyShowPopup: function () {
        if (typeof (TriggerPortalAjaxPopup_Showing) != 'undefined' && jQuery.isFunction(TriggerPortalAjaxPopup_Showing))
            TriggerPortalAjaxPopup_Showing();
    },
    NotifyHidePopup: function () {
        if (typeof (TriggerPortalAjaxPopup_Hidden) != 'undefined' && jQuery.isFunction(TriggerPortalAjaxPopup_Hidden))
            TriggerPortalAjaxPopup_Hidden();
    },
    StartSessionKeepAliveTimer: function () {
        window.setInterval(function () {
            if (!WebBase.SessionInvalid)
                $.get(window.location.href, { SessionKeepAliveRequest: "true" }, WebBase.StartSessionKeepAliveTimer_Complete);
        }, 120000);
    },
    StartSessionKeepAliveTimer_Complete: function (data) {
        if (data.indexOf("Invalid") > -1)
            WebBase.SessionInvalid = true;
    },
    ResizeModalPopUpControl: function () {
        $.each(Sys.Application.getComponents(), function () {
            if (this.get_PopupControlID != null) {
                WebBase.ResizeModalPopUpControl_Single(this);
            }
        });

        if (typeof (Sys) != 'undefined' && Sys != null && Sys.WebForms != null && Sys.WebForms.PageRequestManager != null) {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            if (prm != null) {
                prm.remove_endRequest(WebBase.ResizeModalPopUpControl);
                prm.add_endRequest(WebBase.ResizeModalPopUpControl);
            }
        }
    },
    CallResizeWithDelay: function () {
        window.setTimeout(WebBase.ResizeModalPopUpControl, 1);
    },
    ResizeModalPopUpControl_Single: function (ModalPopUp) {
        var minHeight = document.documentElement.clientHeight - 50;
        var minWidth = document.documentElement.clientWidth - 50;
        var ModalAjaxPopUpControl = null;

        // This is a little hack, because the JQuery-Method "height()" does not always return the right dimension of an element if its Display-Attribute has been set to "none".
        // We do exactly the same the method "height()" of the JQuery-Library does in case of needed dimension of an element which was set to display:none. Store the current attributes into
        // temporary variables and change than in order to get the right dimensions. After reading the width and height we restore the previous values of the attributes and
        // go ahead.
        var curPosition = $('#' + ModalPopUp.get_PopupControlID()).css("position");
        var curVisibility = $('#' + ModalPopUp.get_PopupControlID()).css("visibility");
        var curDisplay = $('#' + ModalPopUp.get_PopupControlID()).css("display");

        $('#' + ModalPopUp.get_PopupControlID()).css({ position: "absolute", visibility: "hidden", display: "block" });
        //        var divWidth = $('#' + ModalPopUp.get_PopupControlID()).css("width");
        //        var divHeight = $('#' + ModalPopUp.get_PopupControlID()).css("height");
        var myElement = document.getElementById(ModalPopUp.get_PopupControlID());
        var divWidth = myElement.offsetWidth;
        var divHeight = myElement.offsetHeight;
        $('#' + ModalPopUp.get_PopupControlID()).css({ position: curPosition, visibility: curVisibility, display: curDisplay });

        // we store the original size as attribute to the controls, so we can resize the element back
        // to the normal size, if we get more space.
        $('#' + ModalPopUp.get_PopupControlID()).each(function () {
            if (($(this).attr("OrigHeight") == undefined || parseInt($(this).attr("OrigHeight")) == 0) &&
             ($(this).attr("OrigWidth") == undefined || parseInt($(this).attr("OrigWidth")) == 0)) {
                $(this).attr({ OrigHeight: divHeight, OrigWidth: divWidth });
            }
        });

        ModalAjaxPopUpControl = $('#' + ModalPopUp.get_PopupControlID()).get(0);

        if (ModalAjaxPopUpControl != null) {
            var PopupHeight = parseInt($(ModalAjaxPopUpControl).attr("OrigHeight"));
            var PopupWidth = parseInt($(ModalAjaxPopUpControl).attr("OrigWidth"));

            var PopupHeight = parseInt($(ModalAjaxPopUpControl).height());
            var PopupWidth = parseInt(divWidth);

            if (minHeight < PopupHeight) {
                ModalAjaxPopUpControl.style.height = minHeight + "px";
            }
            else if (PopupHeight > 0) {
                ModalAjaxPopUpControl.style.height = PopupHeight + "px";
            }

            if (minWidth < PopupWidth) {
                ModalAjaxPopUpControl.style.width = minWidth + "px";
            }
            else if (PopupWidth > 0) {
                ModalAjaxPopUpControl.style.width = PopupWidth + "px";
            }

            if (minWidth < PopupWidth || minHeight < PopupHeight) {
                ModalAjaxPopUpControl.style.overflow = 'auto';
            }

            if (PopupHeight == 0 || PopupWidth == 0) {
                ModalPopUp.remove_shown(WebBase.ResizeModalPopUpControl_Single);
                ModalPopUp.add_shown(WebBase.ResizeModalPopUpControl_Single);
            }
        }

        ModalPopUp._layout();
    },
    CheckForAjaxPopupNotification: function () {
        if (typeof (TriggerPortalAjaxPopup_HideAll) != 'undefined' && jQuery.isFunction(TriggerPortalAjaxPopup_HideAll))
            TriggerPortalAjaxPopup_HideAll();

        $.each(Sys.Application.getComponents(), function () {
            if (this.get_PopupControlID != null) {
                var ModalAjaxPopUpControl = null;
                ModalAjaxPopUpControl = $('#' + this.get_PopupControlID()).get(0);

                if (ModalAjaxPopUpControl != null) {
                    if ($(ModalAjaxPopUpControl).is(":visible")) {
                        WebBase.NotifyShowPopup();
                    }
                }

                this.remove_shown(WebBase.CheckForAjaxPopupNotification);
                this.add_shown(WebBase.CheckForAjaxPopupNotification);

                this.remove_hidden(WebBase.NotifyHidePopup);
                this.add_hidden(WebBase.NotifyHidePopup);
            }
        });

        if (typeof (Sys) != 'undefined' && Sys != null && Sys.WebForms != null && Sys.WebForms.PageRequestManager != null) {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            if (prm != null) {
                prm.remove_endRequest(WebBase.CheckForAjaxPopupNotification);
                prm.add_endRequest(WebBase.CheckForAjaxPopupNotification);
            }
        }
    }
}

$(document).ready(WebBase.StartSessionKeepAliveTimer);
$(window).load(WebBase.CallResizeWithDelay);
$(window).load(WebBase.CheckForAjaxPopupNotification);




