﻿
// Was built off the BAIweb design and this one is on ALL pages, so be carefull what you put in here
// Like NO Sys.<somefunction> will not work, is scriptmanager is not on page.

// create the BAI namespace if it does not exist yet
if (!BAIweb) var BAIweb = {};
if (!BAIweb.TimeOutModal) BAIweb.TimeOutModal = {};
if (!BAIweb.Helper) BAIweb.Helper = {};

// wwill detect a window resize and recalc the size and position of modal.
jQuery(window).bind('resize', function() { BAIweb.TimeOutModal.ReSizeOverlay(); });

//anonymous namespace/function
(function() {
    var overlaywhite;
    var edit_modUploadWrap; //modUploadWrap
    var edit_modUploadCont; //modUploadCont

    var redirectURL = null;
    var returnURL = null;
    var currURL = null;
    var appType = null;

    var objInterval = null;
    var objTOInterval = null;
    var objTOCount = null;

    var xx = 0;

    var IsStopped = false;

    //was intialized at top of page.
    BAIweb.TimeOutModal = {
        Message: function(id) { jQuery("[id$='" + id + "']").hide(); },
        HideMessage: function(id) { jQuery("[id$='" + id + "']").hide(); },
        Run: function(predirectURL, pCurrURL, pAppType, pretURL) {
            clearInterval(objInterval);
            clearInterval(objTOCount);
            clearInterval(objTOInterval);
            redirectURL = predirectURL;
            currURL = pCurrURL;
            appType = pAppType;
            returnURL = pretURL;
            //set the time interval 'BAIweb.TimeOutModal.CheckIdleTime()'
            objInterval = setInterval("BAIweb.TimeOutModal.CheckIdleTime()", 60000);
        },
        OpenProcessLoaderModal: function(contID) {

            // Will OPEN the modal loader popup. 
            // contID       - The container ID. ID can be part of an ASP.Net ID, will be handled.

            jQuery("[id$='" + contID + "']").show();

            this.AdjustLoaderModal(contID);
        },
        PostBackProcessLoaderModal: function(contID) {
            // Will Force the modal to stay oepn after END requests events have fired
            // contID       - The container ID. ID can be part of an ASP.Net ID, will be handled.            
            jQuery("[id$='" + contID + "']").show();
            this.AdjustLoaderModal(contID);
            BAIweb.TimeOutModal.RemainOpen = true;
        },
        CloseModal: function(contID) {
            // Will CLOSE the modal popup. 
            // contID       - The container ID. ID can be part of an ASP.Net ID, will be handled.
            jQuery("[id$='" + contID + "']").hide();
            BAIweb.TimeOutModal.IsOpen = false;
            BAIweb.TimeOutModal.RemainOpen = false;
        },
        AdjustLoaderModal: function(contID) {

            // Use JQuery to Adjust the modal loader postion and placements.
            // The overlay is inside the container div, so we can use a ong image or BG tran% color for the overlay
            // contID       - The container ID. ID can be part of an ASP.Net ID, will be handled.
            var divBox = jQuery("[id$='" + contID + "']");
            overlaywhite = divBox.find("overlaywhite");
            edit_modUploadWrap = divBox.find(".modUploadWrap");
            edit_modUploadCont = divBox.find(".modUploadCont");

            if (edit_modUploadWrap) {
                edit_modUploadWrap.css('top', 250);
                edit_modUploadWrap.css('left', jQuery(window).width() / 2 - edit_modUploadCont.width() / 2);
                edit_modUploadWrap.css('z-index', '999998');
                edit_modUploadCont.css('z-index', '999999');
            }

            if (overlaywhite) {
                divBox.css("height", jQuery(document).height() + 'px');
                divBox.css("width", jQuery(window).width() + 'px');
                divBox.css('z-index', '999996');
                divBox.css("position", "absolute");
                divBox.css("top", "0");
                divBox.css("left", "0");
                //overlayclear.css("height", jQuery(document).height() + 'px');
                //overlayclear.css("width", jQuery(window).width() + 'px');
                overlaywhite.css('z-index', '999995');
                //reset the scroll to the top
                jQuery(window).scrollTop(0);
            }
        },
        ReSizeOverlay: function() {
            // Resize the page for currently open modal.
            // Fires on a window resize event.
            if (overlaywhite) {
                if (overlaywhite.css("display") == "block") {
                    //wrap_mod.css("top",jQuery(window).height() - wrap_mod.height());
                    overlaywhite.css("height", jQuery(document).height() + 'px');
                    overlaywhite.css("width", jQuery(window).width() + 'px');
                }
            }
        },
        CheckIdleTime: function() {

            if (IsStopped == false) {
                jQuery.ajax({
                    type: "POST",
                    url: currURL + "/webservices/CheckTimeOut.asmx/IsExpired",
                    data: "{'pintApplicationType':'" + appType + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        var o = eval("(" + msg.d + ")");
                        if (o != true) {
                            if (objTOInterval)
                            { clearInterval(objTOInterval); }
                            clearInterval(objInterval);
                            xx = 0;
                            BAIweb.TimeOutModal.StartTimeOut();
                        }
                    }
                });
            }
        },
        ResetLogin: function() {
            clearInterval(objInterval);
            clearInterval(objTOCount);
            clearInterval(objTOInterval);

            jQuery.ajax({
                type: "POST",
                url: currURL + "/webservices/CheckTimeOut.asmx/ResetLogin",
                data: "{'pintApplicationType':'" + appType + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    var o = eval("(" + msg.d + ")");

                    if (o != true) {
                        BAIweb.TimeOutModal.StopCheck();
                        BAIweb.TimeOutModal.Redirect();
                    }
                    else {
                        clearInterval(objTOCount);
                        clearInterval(objTOInterval);
                        BAIweb.TimeOutModal.HideMessage('divTimeOutModal');
                        objInterval = setInterval(function() { BAIweb.TimeOutModal.CheckIdleTime(); }, 60000);
                        IsStopped = false;
                    }

                }
            });
        },
        StartTimeOut: function() {
            this.PostBackProcessLoaderModal('divTimeOutModal');
            objTOInterval = setInterval("BAIweb.TimeOutModal.Redirect()", 60000);
            objTOCount = setInterval("BAIweb.TimeOutModal.IncrementCounter()", 1000);
        },
        IncrementCounter: function() {
            xx += 1;
            var num = 61 - xx;
            jQuery("#logtime").html(num);
        },
        Redirect: function() {
            if (IsStopped != true) {
                jQuery.ajax({
                    type: "POST",
                    url: currURL + "/webservices/CheckTimeOut.asmx/LogOut",
                    data: "{'pintApplicationType':'" + appType + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function() { }
                });

                if (returnURL != "") {
                    window.location = redirectURL + "/default.aspx?q=" + returnURL;
                }
                else {
                    window.location = redirectURL + "/default.aspx";
                }
            }
        },
        StopCheck: function() {
            // IsStopped = true;
            if (objTOCount)
            { clearInterval(objTOCount); }
            if (objInterval)
            { clearInterval(objInterval); }
            if (objInterval)
            { clearInterval(objTOInterval); }
            objTOCount = null;
            objInterval = null;
        }
    };
} ());

//BAIweb.Helper.GoToTop
//anonymous namespace/function
(function() {
    //was intialized at top of page.
    BAIweb.Helper = {
        GoToTop: function() {
            scroll(0, 0);
        }
    };
} ());





