﻿

// JScript File
function FadeUp(id, updatePanelId) {
	var opacityVal;
	var obj = document.getElementById(id);
	if (obj != null) 
	{
		opacityVal = obj.getAttribute("opacity");
		if (opacityVal == null) {
			// start to fade
			opacityVal = parseFloat("0.0");
		}
		opacityVal = parseFloat(opacityVal);
		// increment fade
		opacityVal += 0.2;
		if (opacityVal >= 0.6) {
			var parentUpdatePnl = document.getElementById(updatePanelId).parentNode;
			var intervalId = parentUpdatePnl.getAttribute("interval");
			window.clearInterval(intervalId);
			parentUpdatePnl.removeAttribute("interval");
		}

		obj.setAttribute("opacity", opacityVal);
		obj.style.opacity = opacityVal;
		obj.style.filter = "alpha(opacity=" + opacityVal * 100 + ")";
	}
}

function UpdateLoading(obj, updatePanelObj) {
	var offsetLeft = 0;
	var offsetTop = 0;
	var newwidth = 0;
	var newheight = 0;
	for (var pObj = obj.parentNode; pObj != null; ) {
		if (newwidth == 0)
			newwidth = pObj.offsetWidth;
		if (newheight == 0)
			newheight = pObj.offsetHeight;
		if (pObj.parentNode == null)
			break;
		offsetLeft += pObj.offsetLeft;
		offsetTop += pObj.offsetTop;
		if (pObj.offsetParent == null)
			pObj = pObj.parentNode;
		else
			pObj = pObj.offsetParent;
	}
	var parent = obj.parentNode;
	if (parent.clientWidth != 0)
		newwidth = parent.clientWidth;
	else
		newwidth = parent.offsetWidth;
	if (parent.clientHeight != 0)
		newheight = parent.clientHeight;
	else
		newheight = parent.offsetHeight;
	obj.style.left = offsetLeft + "px";
	obj.style.top = offsetTop + "px";
	obj.style.width = newwidth + "px";
	obj.style.height = newheight + "px";
	obj.style.display = "";

	obj.setAttribute("opacity", "0.0");
	obj.style.filter = "alpha(opacity=0)";
	obj.style.opacity = 0.0;    // firefox
	var interval = window.setInterval("FadeUp( \"" + obj.id + "\", \"" + updatePanelObj.id + "\" )", 40);
	updatePanelObj.parentNode.setAttribute("interval", interval);
}

function InitUpdateLoading() {
	Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitRequest);
}

function InitRequest(sender, args) {
	postbackElt = args.get_postBackElement();

	// search for parent update panel
	for (var curPostBackElt = postbackElt; curPostBackElt.parentNode != null; curPostBackElt = curPostBackElt.parentNode) {
		if (curPostBackElt.tagName != "DIV")
			continue;
		for (i = 0; i < sender._updatePanelClientIDs.length; i++) {
			var curUpdatePanelId = sender._updatePanelClientIDs[i];
			if (curUpdatePanelId == curPostBackElt.id) {
				var parentObj = document.getElementById(curUpdatePanelId);
				var objList = parentObj.getElementsByTagName("div");
				var hasLoading = false;
				for (j = 0; j < objList.length; j++) {
					if (objList[j].className != "loading")
						continue;
					for (var pParent = objList[j].parentNode; pParent.parentNode != null; pParent = pParent.parentNode) {
						if (pParent.id == parentObj.id) {
							hasLoading = true;
							UpdateLoading(objList[j], parentObj);
						}
					}
				}

				if (hasLoading == true) {
					return;
				}
				else {
					var div = document.createElement("DIV");
					attrClass = document.createAttribute("class");
					div.setAttribute("className", "loading");
					div.className = "loading";
					attrName = document.createAttribute("name");
					div.setAttribute("name", "loadingProgress");
					attrName = document.createAttribute("id");
					div.setAttribute("id", parentObj.id + '_Load');
					parentObj.appendChild(div);
					UpdateLoading(parentObj.lastChild, parentObj);
				}
			}
			
		}
	}
}




