var showPageTimeout = null;
var veilLoadingImage = new Image();
var bumpUp = true;
var formerPosX = 0;
var formerPosY = 0;
var floater = null;
var veil = null;
var newTargetName = null;

veilLoadingImage.src = "/client_include/images/indolents-t.gif";


addWindowOnload(function() {
	floater = document.getElementById("floater");
	veil = document.getElementById("veil");

	clearVeil();
});

function openVeil(customCloseHandler, bump) {
	
	if (customCloseHandler == null || customCloseHandler == undefined) {
		customCloseHandler = "closeVeil()";
	}
	if (bump != null && bump != undefined) {
		bumpUp = bump;
		formerPosY = getScrollY();
		formerPosX = getScrollX();
	} else {
		bumpUp = true;
		formerPosY = getScrollY();
		formerPosX = getScrollX();
	}
	floater.style.visibility = "hidden";
	floater.innerHTML = "<div class=\"center\"><img id=\"loading_image\" alt=\"Loading ...\" title=\"Loading ...\" /><br /><br /><img class=\"clickable\" src=\"/client_include/images/x.gif\" alt=\"Close\" title=\"Close\" onclick=\""+customCloseHandler+";\" /></div>";
	document.getElementById("loading_image").src = veilLoadingImage.src;
	
	veil.style.position = "fixed";
	veil.style.width = "20%";
	veil.style.height = "100%";
	veil.style.left = "0px";
	veil.style.top = "0px";
	veil.style.backgroundColor = "#000000";
	veil.style.visibility = "visible";
	veil.style.display = "block";
    floater.style.borderColor = "transparent";
    floater.style.borderWidth = "0px";
    floater.style.borderStyle = "none";
	setOpacity(veil, 90);
	var timeInterval = 20;
	var amount = 10; 
	if (!window.opera) {
		if (!window.ActiveXObject) {
			showPageTimeout = window.setInterval(extendVeil, timeInterval, amount);
		} else {
			showPageTimeout = window.setInterval("extendVeil("+amount+")", timeInterval);
		}
	} else {
		setToDisplay();
	}
}

function setToDisplay() {
	floater.style.visibility = "visible";
	floater.style.display = "block";
	floater.style.position = "absolute";
	floater.style.backgroundColor = "transparent";
	floater.style.textAlign = "center";
	floater.style.top = "0px";
	floater.style.left = "0px";
	floater.style.width = "100%";
	floater.style.color = "#FFFFFF";
	floater.style.padding= "0px";
	veil.style.width = "100%";
	veil.textAlign = "center";
	document.onkeyup = clearVeil;
	if (bumpUp) {
		window.scroll(0,0);
	}
}

function extendVeil(amount) {
	var floater = document.getElementById("floater");
	if (typeof extendVeil.width == 'undefined') {
		extendVeil.width = 20;
		extendVeil.bodyWidth = document.documentElement.clientWidth;
	}
	if (extendVeil.width >= 100) {
		window.clearInterval(showPageTimeout);
		extendVeil.width = 30;
		if (!window.opera) {
			fadeIn(floater.id, 70, null, 5);
		}
		if (newTargetName != null) {
			replaceTarget(newTargetName);
		}

		setToDisplay();
		
		return true;
	}
	var newWidth = extendVeil.width*1+amount*1;
	veil.style.width = newWidth+"%";
	extendVeil.width += amount*1;
}

function closeVeil() {
	if (!window.opera) {
		var timeInterval = 20;
		var amount = 10; 
		fadeOut(floater.id, 70, null, 5);
		window.scroll(formerPosX, formerPosY);
		if (!window.ActiveXObject) {
			showPageTimeout = window.setInterval(retractVeil, timeInterval, amount);
		} else {
			showPageTimeout = window.setInterval("retractVeil("+amount+")", timeInterval);
		}
	} else {
		window.scroll(formerPosX, formerPosY);
		clearVeil();
	}
}

function retractVeil(amount) {
	floater.style.display = "none";
	if (typeof retractVeil.width == 'undefined') {
		retractVeil.width = 100;
		retractVeil.bodyWidth = 100;
	}
	if (retractVeil.width <= 0) {
		window.clearInterval(showPageTimeout);
		retractVeil.width = 100;
		clearVeil();
		
		return true;
	}
	var newWidth = retractVeil.width*1-amount*1;
	veil.style.width = newWidth+"%";
	retractVeil.width -= amount*1;
}

function clearVeil(e) {
	if (e && e.keyCode != 27) {
		return false;	
	}
	
	floater.style.visibility = "hidden";
	floater.style.display = "none";
	veil.style.visibility = "hidden";
	veil.style.display = "none";
	floater.innerHTML = "";
	veil.innerHTML = "";
	floater.style.width = "auto";
	floater.style.height = "auto";
	floater.style.textAlign = "left";
	veil.style.width = "auto";
	veil.style.height = "auto";
	veil.style.textAlign = "left";
	veil.style.backgroundColor = "";
	floater.style.paddingRight = "";
	setOpacity(floater, 100);
	setOpacity(veil, 100);
	return true;
}

function displayAjaxPage(page, method, string, bump) {
	var userAgent = navigator.userAgent;
	if (userAgent.match("MSIE")) {
		var matches = userAgent.match(/MSIE ((?:\d|\.)+)/);
		if ((matches[1]*1) < 7) {
			return false;
		}
	}
	if (window.opera) {
		return false;
	}
	
	openVeil();
	if (bump != null && bump != undefined) {
		bumpUp = bump;
		formerPosY = getScrollY();
		formerPosX = getScrollX();
	} else {
		bumpUp = true;
		formerPosY = getScrollY();
		formerPosX = getScrollX();
	}
	
	if (method == null || method == undefined) {
		method = "GET";
	}
	if (string == null || string == undefined) {
		string = null;
	}
	
	var xmlhttp = getXmlHttp();
	xmlhttp.open(method, page);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			floater.innerHTML = "<div class=\"center\">"+xmlhttp.responseText+"<br /><img class=\"clickable\" src=\"/client_include/images/x.gif\" alt=\"Close\" title=\"Close\" onclick=\"closeVeil();\" /></div>";
			fadeIn(floater.id, 70, null, 5);
		}
	}
	xmlhttp.send(string);
	return true;
}

function displayImage(page, string, bump) {
	var userAgent = navigator.userAgent;
	if (userAgent.match("MSIE")) {
		var matches = userAgent.match(/MSIE ((?:\d|\.)+)/);
		if ((matches[1]*1) < 7) {
			return false;
		}
	}
	if (window.opera) {
		return false;
	}
	
	openVeil();
	if (bump != null && bump != undefined) {
		bumpUp = bump;
		formerPosY = getScrollY();
		formerPosX = getScrollX();
	} else {
		bumpUp = true;
		formerPosY = getScrollY();
		formerPosX = getScrollX();
	}
	
	
	floater.innerHTML = "<div class=\"center\"><img src=\""+page+"\" alt=\""+page+"\" /><br /><img class=\"clickable\" src=\"/client_include/images/x.gif\" alt=\"Close\" title=\"Close\" onclick=\"closeVeil();\" /></div>";
	fadeIn(floater.id, 70, null, 5);

	return true;
}
