﻿var isShowing = false;
var dotCount = 5;
$(document).ready(function() {
    if (typeof Sys !== "undefined") {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(LoadingMsg_BeginRequestHandler);
        prm.add_endRequest(LoadingMsg_EndRequestHandler);
        $(window).scroll(PositionLoadingMsg);
    }
});

function LoadingMsg_BeginRequestHandler() {
    ShowLoadingMsg();
}

function LoadingMsg_EndRequestHandler() {
    HideLoadingMsg();
}

function ShowLoadingMsg() {
    //set the flag(instead of running a jQuery select on every scroll)
    isShowing = true;
    //reposition loading message to top of browser
    PositionLoadingMsg();
    //start the dots
    showDots();
    //show the loading message
    $('#loading_msg').show();
}

function HideLoadingMsg() {
    //hide the loading message
    $('#loading_msg').hide();
    //unset the flag
    isShowing = false;
}

function PositionLoadingMsg() {
    if (isShowing) {
        $('#loading_msg').css("top", $(window).scrollTop() + "px");
    }
}

function showDots() {
    if (isShowing) {
        if (dotCount < 5) {
            dotCount++;
            $("#loading_msg").html($("#loading_msg").html() + ".&nbsp;").animate({ opacity: 1.0 }, 500, "", showDots);
        }
        else {
            dotCount = 0;
            $("#loading_msg").html("Loading&nbsp;").animate({ opacity: 1.0 }, 500, "", showDots);
        }
    }
}