var globalObjects = { "globalObjects" : true, "isAtlasLoading" : false };

function initCustomUpdateProgress() {
    var beginRequestHandlerDelegate = Function.createDelegate(null, customUpdateProgress_handleBeginRequest);
    var endRequestHandlerDelegate = Function.createDelegate(null, customUpdateProgress_handleEndRequest );
	var pageRequestManager = null;

	if (Sys.WebForms && Sys.WebForms.PageRequestManager) {
		pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
    }

	if (pageRequestManager !== null ) {
		pageRequestManager.add_beginRequest(beginRequestHandlerDelegate);
		pageRequestManager.add_endRequest(endRequestHandlerDelegate);
    }
}

function customUpdateProgress_handleBeginRequest(sender, arg) {
	changeUpdateProgress(true);
}

function customUpdateProgress_handleEndRequest(sender, arg) {
	changeUpdateProgress(false);
}

function changeUpdateProgress(isLoading) {
	globalObjects["isAtlasLoading"] = isLoading;

	var atlasStateContainer = document.getElementById("atlasStateContainer");
	if (atlasStateContainer) {
		atlasStateContainer.value = globalObjects["isAtlasLoading"] ? "loaded" : "not loaded";
		var spinner = document.getElementById("atlasUpdateProgress");
		if (spinner) {
			var position = GetSpinnerPosition();
			spinner.style.top = position.top + "px";
			spinner.style.left = position.left + "px";
			spinner.style.display = globalObjects["isAtlasLoading"] ? "block" : "none";
			
			// showing/hiding the busy wall
			if (globalObjects["isAtlasLoading"]) {
				showBusyWall();
			} else {
				hideBusyWall();
			}
		}
	}
}

function GetSpinnerPosition() {	
	var width=176, height=24;
	var body = document.getElementsByTagName("body")[0];	
	var scrollTop = body.scrollTop;
	var windowHeight = body.clientHeight;
	var windowWidth = body.clientWidth;

	var position = new Object();
	position.top = scrollTop;
	position.left = 0;	
	
	return position;
}

function OnLoad() {
	var spinner = document.getElementById("atlasUpdateProgress");
	if (spinner) {		
		spinner.style.display = "none";
		spinner.style.position = "absolute";
		
		var position = GetSpinnerPosition();
		spinner.style.top = position.top + "px";
		spinner.style.left = position.left + "px";
		
		window.onscroll = function() {
			var atlasStateContainer = document.getElementById("atlasStateContainer");
			if (atlasStateContainer && atlasStateContainer.value == "loaded") {
				var spinner = document.getElementById("atlasUpdateProgress");
				if (spinner) {			
					var position = GetSpinnerPosition();
					spinner.style.top = position.top + "px";
					spinner.style.left = position.left + "px";
				}
			}
		}

		window.onresize = function() {
			var spinner = document.getElementById("atlasUpdateProgress");
			if (spinner) {			
				var position = GetSpinnerPosition();
				spinner.style.top = position.top + "px";
				spinner.style.left = position.left + "px";
			}
		}
	}
}

function showBusyWall()
{
	var busyWall = document.getElementById("busyWall");
	if (busyWall)
	{
		busyWall.height = document.body.scrollHeight;
		busyWall.width = document.body.scrollWidth;
		busyWall.style.display = "block";
		document.body.style.cursor = "wait";
	}
}

function hideBusyWall()
{
	var busyWall = document.getElementById("busyWall");
	if (busyWall)
	{
		busyWall.style.display = "none";
		document.body.style.cursor = "";
	}
}

OnLoad();
initCustomUpdateProgress();