var shadowinit=true;
var fadeID, fA;
var scrW, scrH, sbDiv;

function initSB() {
	scrW = screen.width;
	scrH = screen.height;
	sbDiv = document.getElementById("shadowbox");
	if (!sbDiv) {
		sbDiv = document.createElement("div");
		sbDiv.id = "shadowbox";
		sbDiv.style.backgroundColor = "#000000";
		sbDiv.style.position = 'absolute'
		sbDiv.style.top = "0px";
		sbDiv.style.left = "0px";
		sbDiv.style.width = "10px";
		sbDiv.style.height = "10px";
		sbDiv.style.display = "none";
		sbDiv.style.zIndex = '900';
		if (typeof(sbDiv.style.MozOpacity) != "undefined") { sbDiv.style.MozOpacity = "0.2"; }
		else if (typeof(sbDiv.style.opacity) != "undefined") { sbDiv.style.opacity = "0.2"; }
		else if (typeof(sbDiv.style.filter) != "undefined") { sbDiv.style.filter = "alpha(opacity=20)"; }
		var aDiv = document.getElementById('page');
		aDiv.appendChild(sbDiv);
	} else {
		if (typeof(sbDiv.style.MozOpacity) != "undefined") { sbDiv.style.MozOpacity = "0.2"; }
		else if (typeof(sbDiv.style.opacity) != "undefined") { sbDiv.style.opacity = "0.2"; }
		else if (typeof(sbDiv.style.filter) != "undefined") { sbDiv.style.filter = "alpha(opacity=20)"; }
	}
}

function openSBox(aDiv) {
	if (shadowinit) { shadowinit=false; initSB(); }
	var cDiv = document.getElementById(aDiv);
	var bW = document.documentElement.clientWidth;
	var bH = document.documentElement.clientHeight;
	var scrL = document.documentElement.scrollLeft;
	var scrT = document.documentElement.scrollTop;

	sbDiv.style.width = scrL + bW + 'px';
	sbDiv.style.height = scrT + bH + 'px';
	
	cDiv.style.position = 'absolute'
	cDiv.style.zIndex = '920';	
	cDiv.style.visibility = 'hidden';
	cDiv.style.display = 'block';
	var cW = cDiv.offsetWidth;
	var cH = cDiv.offsetHeight;
	cDiv.style.display = 'none';
	cDiv.style.visibility = 'visible';
	cDiv.style.left = Math.round(scrL + ((bW-cW)/2)) + 'px';
	cDiv.style.top = Math.round(scrT + ((bH-cH)/2)) + 'px';
	
	// add 30 if hiding overflow
	document.documentElement.style.overflow = 'hidden';
	sbDiv.style.width = parseInt(sbDiv.style.width) + 130 + 'px';
	sbDiv.style.height = parseInt(sbDiv.style.height) + 130 + 'px';

	divFadeIn(cDiv.id,80,10);
}

function closeSBox(aDiv) {
	divFadeOut(aDiv,0,10);
}
function endSBox(aDiv) {
	document.documentElement.style.overflow = 'auto';
	cDiv =  document.getElementById(aDiv);
	sbDiv.style.display = 'none';
	cDiv.style.display = 'none';
}

function divFadeIn(aDiv,aAmt,aSpd) {
	fA = 0;
	cDiv = document.getElementById(aDiv);
	
	if (typeof(sbDiv.style.MozOpacity)!="undefined") { sbDiv.style.opacity = fA/100; cDiv.style.opacity = fA/100; }
	else if (typeof(sbDiv.style.opacity)!="undefined") { sbDiv.style.opacity = fA/100; cDiv.style.opacity = fA/100; }
	else if (typeof(sbDiv.style.filter)!="undefined") { sbDiv.style.filter="alpha(opacity="+fA+")"; cDiv.style.filter="alpha(opacity="+fA+")"; }
	fadeID = setInterval("divFader('"+aDiv+"','"+aAmt+"','"+aSpd+"','In');", 1);
	
	sbDiv.style.display = 'block';
	cDiv.style.display = 'block';
}

function divFadeOut(aDiv,aAmt,aSpd) {
	cDiv =  document.getElementById(aDiv);
	if (typeof(cDiv.style.MozOpacity)!="undefined") { fA = sbDiv.style.MozOpacity * 100; }
	else if (typeof(cDiv.style.opacity)!="undefined") { fA = sbDiv.style.opacity * 100; }
	else if (typeof(cDiv.style.filter)!="undefined") { fA = sbDiv.filters.alpha.opacity; }
	clearInterval(fadeID);
	fadeID = setInterval("divFader('"+aDiv+"','"+aAmt+"','"+aSpd+"','Out');", 1);
}

function divFader(aDiv,aAmt,aSpd,aAct) {
	cDiv = document.getElementById(aDiv);
	if (aAct=='Out') { aSpd = (aSpd * -1); }
	aAmt = parseInt(aAmt);
	fA=parseInt(fA)+(parseInt(aSpd));
	if (cDiv.style.MozOpacity!=null) { cDiv.style.MozOpacity=fA/100; sbDiv.style.MozOpacity=fA/100; }
	else if (cDiv.style.opacity!=null) { cDiv.style.opacity=fA/100; sbDiv.style.opacity=fA/100; }
	else if (cDiv.filters.alpha.opacity!=null) { cDiv.filters.alpha.opacity=fA; sbDiv.filters.alpha.opacity=fA; }
	if (aAct=='Out') {
		if (fA<=aAmt) { 
			clearInterval(fadeID);
			sbDiv.style.display = 'none';
			cDiv.style.display = 'none';
			document.documentElement.style.overflow = 'auto';
		}
	} else {
		if (fA>=aAmt) {
			if (cDiv.style.MozOpacity!=null) { cDiv.style.MozOpacity=1; sbDiv.style.MozOpacity=aAmt/100; }
			else if (cDiv.style.opacity!=null) { cDiv.style.opacity=1; sbDiv.style.opacity=aAmt/100; }
			else if (cDiv.filters.alpha.opacity!=null) { cDiv.filters.alpha.opacity=100; sbDiv.filters.alpha.opacity=aAmt; }
			clearInterval(fadeID);
		}
	}
}
