/*
lyteBox.js
For creating an animated DOM lightbox using javascript.
©2008 Dan Schellhas
*/

function dePX (value) {
	var val = parseInt(value.substr(0, value.length - 2));
	return val;
}

function lyteBox( picture, width, height , picture2 ) {	
	
	var ext1, ext2;
	var winWidth;
	var winHeight;
	var winXOff;
	var winYOff;
	var isFirefox;
	var isIE;
	
	var innerDiv = document.createElement("div");
	var xDiv = document.createElement("img");
	var otherDiv = document.createElement("div");
	var innerImg = document.createElement("img");
	var dropShadow = document.createElement("div");
	var whiteDiv = document.createElement("div");
	var loadingDiv = document.createElement("div");
	var shadowDiv = document.createElement("div");

	if (navigator.userAgent) {
		isFirefox = navigator.userAgent.indexOf('Firefox') + 1;
		isIE = navigator.userAgent.indexOf('IE') + 1;
	}
	else {
		isFirefox = false;
		isIE = false;
	}
	ext1 = picture.substr(picture.length - 3);
	if (picture2 != undefined) {
		ext2 = picture2.substr(picture2.length - 3);
	}
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
		winXOff = window.pageXOffset;
		winYOff = window.pageYOffset;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
		winXOff = document.documentElement.scrollLeft;
		winYOff = document.documentElement.scrollTop;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winWidth = document.body.clientWidth;
		winHeight = document.body.clientHeight;
		winXOff = document.body.scrollLeft;
		winYOff = document.body.scrollTop;
	  }

	document.body.style.overflow = 'hidden';

	window.onresize = function () {
		var shadowDiv = document.getElementById("shadowDiv");
		var whiteDiv  = document.getElementById("whiteDiv");
		var winWidth;
		var winHeight;
		var winXOff;
		var winYOff;
		
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			winWidth = window.innerWidth;
			winHeight = window.innerHeight;
			winXOff = window.pageXOffset;
			winYOff = window.pageYOffset;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			winWidth = document.documentElement.clientWidth;
			winHeight = document.documentElement.clientHeight;
			winXOff = document.documentElement.scrollLeft;
			winYOff = document.documentElement.scrollTop;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			winWidth = document.body.clientWidth;
			winHeight = document.body.clientHeight;
			winXOff = document.body.scrollLeft;
			winYOff = document.body.scrollTop;
		}
		shadowDiv.style.left = winXOff + 'px';
		shadowDiv.style.top = winYOff + 'px';
		shadowDiv.style.width = winWidth + "px";
		shadowDiv.style.height = winHeight + "px";
		whiteDiv.style.left = (( winWidth / 2 ) - (( width + 20 ) / 2 ) + winXOff) + "px";
		whiteDiv.style.top = (( (winHeight - 25) / 2 ) - (( height + 20 ) / 2 ) + winYOff) + "px";
		dropShadow.style.left = (( winWidth / 2 ) - (( width + 20 ) / 2 ) + 7 + winXOff) + "px";
		dropShadow.style.top = (((winHeight - 25) / 2 ) - (( height + 20 ) / 2 ) + 7 + winYOff) + "px";
		loadingDiv.style.left = (( winWidth / 2 ) - (( width + 20 ) / 2 )) + "px";
		loadingDiv.style.top = (( (winHeight - 25) / 2 ) - (dePX(loadingDiv.style.fontSize) / 2 )) + "px";
		
	}
	
	var scrollPos;
	if (isFirefox) { scrollPos = winYOff; winXOff = 0; winYOff = 0;}
	
	shadowDiv.id = "shadowDiv";
	shadowDiv.style.backgroundColor = "#000";
	shadowDiv.style.position = "absolute";
	shadowDiv.style.left = winXOff + "px";
	shadowDiv.style.top = winYOff + "px";
	shadowDiv.style.width = winWidth + "px";
	shadowDiv.style.height = winHeight + "px";
	shadowDiv.style.opacity = 0;
	shadowDiv.style.filter = 'alpha(opacity=0)';
	
	loadingDiv.id = "loadingDiv";
	loadingDiv.style.position = "absolute";
	loadingDiv.style.color = "#ffffff";
	loadingDiv.style.fontSize = "30px";
	loadingDiv.style.fontFamily = "Arial";
	loadingDiv.style.textAlign = "center";
	loadingDiv.innerHTML = "Loading...";
	loadingDiv.style.left = (( winWidth / 2 ) - (( width + 20 ) / 2 )) + "px";
	loadingDiv.style.top = (( (winHeight - 25) / 2 ) - (dePX(loadingDiv.style.fontSize) / 2 )) + "px";
	loadingDiv.style.width = (width + 40) + "px";
	loadingDiv.style.height = "35px";
	
	
	whiteDiv.id = "whiteDiv";
	whiteDiv.style.visibility = 'hidden';
	whiteDiv.style.border = '1px solid #000';
	whiteDiv.style.backgroundColor = "#ffffff";
	whiteDiv.style.position = "absolute";
	whiteDiv.style.left = (( winWidth / 2 ) - (( width + 20 ) / 2 ) + winXOff) + "px";
	whiteDiv.style.top = (( (winHeight - 25) / 2 ) - (( height + 20 ) / 2 ) + winYOff) + "px";
	whiteDiv.style.width = (width + 40) + "px";
	whiteDiv.style.height = (height + 40) + "px";
	whiteDiv.realWidth = width + 40 + 2;
	whiteDiv.realHeight = height + 40 + 2; 
	whiteDiv.style.opacity = "0";
	whiteDiv.style.filter = "alpha(opacity=0)";
	whiteDiv.style.zOrder = "1";
	if (picture2 != undefined) {
		whiteDiv.innerHTML = "<div style='float:right;clear:left;padding:3px;'><a style='font-family:Verdana;font-size:11px;' href='javascript:var doNothing;' onclick='this.parentNode.parentNode.childNodes[1].swap();'>Over &gt;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";
	}

	
	dropShadow.style.opacity = '0';
	dropShadow.style.filter = 'alpha(opacity=0)';
	dropShadow.style.position = "absolute";
	dropShadow.style.left = (( winWidth / 2 ) - (( width + 20 ) / 2 ) + 7 + winXOff) + "px";
	dropShadow.style.top = (((winHeight - 25) / 2 ) - (( height + 20 ) / 2 ) + 7 + winYOff) + "px";
	dropShadow.style.width = (width + 40) + "px";
	dropShadow.style.height = (height + 40) + "px";
	dropShadow.style.backgroundColor = "#000000";
	dropShadow.style.zOrder = "0";
	dropShadow.style.visibility = 'hidden';

	innerImg.shadow = dropShadow;
	innerImg.style.visibility = 'hidden';
	innerImg.style.width = width + 'px';
	innerImg.style.height = height + 'px';
	if (isIE) {
		whiteDiv.style.visibility = 'visible';
		innerImg.shadow.style.visibility = 'visible';
		xDiv.style.visibility = 'visible';
	}
	else {
		innerImg.onload = function () { this.parentNode.parentNode.style.visibility = 'visible'; this.shadow.style.visibility = 'visible'; }
	}
	
	otherDiv.style.width = (width ) + 'px';
	otherDiv.style.height = (height) + 'px';
	otherDiv.style.position = 'absolute';
	otherDiv.style.visibility = 'hidden';
	otherDiv.style.backgroundColor = '#ccc';
	otherDiv.style.overflow = 'hidden';
	otherDiv.id = "otherDiv";
	otherDiv.innerHTML = "<center><h1>Loading...</h1></center>";
	
	var ext = picture.substr(picture.indexOf('.') + 1, 3);
	if ( ext == "gif" || ext == "jpg" ) {
		innerImg.src = picture;
		innerImg.style.visibility = 'visible';
	}
	else if ( ext == "htm" || ext == "tml" || ext == "php") {
		var httpRequest;
		// AJAX Initialization
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType("text/xml");
				// See note below about this line
			}
		} 
		else if (window.ActiveXObject) { // IE
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} 
				catch (e) {
						   try {
								httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
							   } 
							 catch (e) {}
				}
		}
		httpRequest.onreadystatechange = function reFiller() { 			
			if (httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {
					otherDiv.innerHTML = httpRequest.responseText;
				} else {
					alert("There was a problem with the fill request.\nStatus: " + httpRequest.status + "\n" + picture);
				}
			}
		};
		httpRequest.open("GET", picture, true);
		httpRequest.send(null);

		whiteDiv.style.visibility = 'visible';
		innerImg.shadow.style.visibility = 'visible';
		otherDiv.style.visibility = 'visible';
	}
	else if ( ext == "ahp" ) {
		var httpRequest;
		// AJAX Initialization
		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType("text/xml");
				// See note below about this line
			}
		} 
		else if (window.ActiveXObject) { // IE
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} 
				catch (e) {
						   try {
								httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
							   } 
							 catch (e) {}
				}
		}
		httpRequest.onreadystatechange = function reFiller() { 			
			if (httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {
					//alert(httpRequest.responseText);
				} else {
					alert("There was a problem with the fill request.\nStatus: " + httpRequest.status);
				}
			}
		};
		httpRequest.open("GET", "setModule.php?mod=" + picture.substr(0, picture.indexOf(".")), true);
		httpRequest.send(null);
		AC_FL_RunContent('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0', 'width', '800', 'height', '600', 'src', 'allHazardPlayer', 'quality', 'high', 'pluginspage', 'http://www.macromedia.com/go/getflashplayer', 'align', 'middle','play', 'true','loop', 'true','scale', 'showall','wmode', 'window','devicefont', 'false','id', 'allHazardPlayer','bgcolor', '#ffffff','name', 'allHazardPlayer','menu', 'true','allowFullScreen', 'false','allowScriptAccess','sameDomain','movie', 'allHazardPlayer','salign', ''); //end AC code
		whiteDiv.style.visibility = 'visible';
		innerImg.shadow.style.visibility = 'visible';
		otherDiv.style.visibility = 'visible';
	}
	

	innerDiv.src = picture;
	innerDiv.src2 = picture2;
	innerDiv.img = innerImg;
	innerDiv.div = otherDiv;
	innerDiv.style.border = '1px solid #000';
	innerDiv.style.position = 'absolute';
	innerDiv.style.left = '19px';
	innerDiv.style.top = '19px';
	innerDiv.style.width = width + 'px';
	innerDiv.style.height = height + 'px';
	innerDiv.state = 0;
	if (picture2 != undefined) {
		innerDiv.swap = function () {
			if (this.state) {
				var ext = this.src.substr(this.src.indexOf('.') + 1, 3);
				if ( ext == "gif" || ext == "jpg") {
					this.img.src = this.src;
					this.img.style.visibility = 'visible';
					this.div.style.visibility = 'hidden';
					this.state = 0;
				}
				else if ( ext == "php" || ext == "htm" || ext == "tml" ) {
					var httpRequest;
					// AJAX Initialization
					if (window.XMLHttpRequest) { // Mozilla, Safari, ...
						httpRequest = new XMLHttpRequest();
						if (httpRequest.overrideMimeType) {
							httpRequest.overrideMimeType("text/xml");
							// See note below about this line
						}
					} 
					else if (window.ActiveXObject) { // IE
						try {
							httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
							} 
							catch (e) {
									   try {
											httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
										   } 
										 catch (e) {}
							}
					}
					var temp = this;
					httpRequest.onreadystatechange = function reFiller() { 			
						if (httpRequest.readyState == 4) {
							if (httpRequest.status == 200) {
								temp.div.innerHTML = httpRequest.responseText;
							} else {
								alert("There was a problem with the fill request.\nStatus: " + httpRequest.status);
							}
						}
					};
					httpRequest.open("GET", this.src, true);
					httpRequest.send(null);
							
					this.div.style.visibility = 'visible';
					this.img.style.visibility = 'hidden';
					this.state = 0;
				}
				
			}
			else {
				var ext = this.src2.substr(this.src2.indexOf('.') + 1, 3);
				if ( ext == "gif" || ext == "jpg") {
					this.img.src = this.src2;
					this.img.style.visibility = 'visible';
					this.div.style.visibility = 'hidden';
					this.state = 1;
				}
				else if ( ext == "php" || ext == "htm" || ext == "tml" ) {
					
					var httpRequest;
					// AJAX Initialization
					if (window.XMLHttpRequest) { // Mozilla, Safari, ...
						httpRequest = new XMLHttpRequest();
						if (httpRequest.overrideMimeType) {
							httpRequest.overrideMimeType("text/xml");
							// See note below about this line
						}
					} 
					else if (window.ActiveXObject) { // IE
						try {
							httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
							} 
							catch (e) {
									   try {
											httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
										   } 
										 catch (e) {}
							}
					}
					var temp = this;
					httpRequest.onreadystatechange = function reFiller() { 			
						if (httpRequest.readyState == 4) {
							if (httpRequest.status == 200) {
								temp.div.innerHTML = httpRequest.responseText;
							} else {
								alert("There was a problem with the fill request.\nStatus: " + httpRequest.status);
							}
						}
					};
					httpRequest.open("GET", this.src2, true);
					httpRequest.send(null);
					
					this.div.style.visibility = 'visible';
					this.img.style.visibility = 'hidden';
					this.state = 1;
				}
			}
		}
	}
	
	xDiv.src = 'xBox.gif';
	xDiv.style.position = 'absolute';
	xDiv.style.borderStyle = 'solid';
	xDiv.style.borderColor = '#000';
	xDiv.style.borderWidth = '2px';
	xDiv.style.width = '17px';
	xDiv.style.height = '17px';
	if (!isNaN(dePX(xDiv.style.borderWidth))) {
		 var bordW = dePX(xDiv.style.borderWidth);
	}
	else {
		var bordW = 2;
	}
	xDiv.style.left = (whiteDiv.realWidth - dePX(xDiv.style.width) - 1 - (bordW * 2)) +'px';
	xDiv.style.top = '-1px';
	xDiv.style.backgroundColor = "#ffffff";
	xDiv.style.cursor = 'pointer';
	xDiv.scrollPos = scrollPos;
	xDiv.onclick = function () { 
		var q = 0;
		
		loadingDiv.style.visibility = "hidden";
		for (i = 0; i < 10; i++) {
			q++;
			pushAnim(function (t, p1) { t.style.opacity = p1 / 100; t.style.filter = 'alpha(opacity=' + p1 +')';}, q, whiteDiv, (9 - i) * 10);
			pushAnim(function (t, p1) { t.style.opacity = p1 / 100; t.style.filter = 'alpha(opacity=' + p1 +')';}, q, dropShadow, (9 - i) * 5);
		}
		
		for (i = 0; i < 10; i++) {
			q++;
			pushAnim(function (t, p1) { t.style.opacity = p1 / 100; t.style.filter = 'alpha(opacity=' + p1 +')';}, q, shadowDiv, (9 - i) * 8);
		}
		
		q++;
		pushAnim(function (t, p1) { document.body.removeChild(document.body.lastChild); window.onresize = null; document.body.style.overflow = 'auto'; document.body.scrollTop = this.scrollPos; }, q, document, 0);
	
	}
	
	var lightBox = document.createElement("div");
	lightBox.style.position = 'absolute';
	lightBox.style.left = '0px';
	lightBox.style.top = '0px';
	lightBox.style.width = '0px';
	lightBox.style.height = '0px';
	
	
	var q = 0;
	
	for (i = 0; i < 10; i++) {
		q++;
		pushAnim(function (t, p1) { t.style.opacity = p1 / 100; t.style.filter = 'alpha(opacity=' + p1 +')';}, q, shadowDiv, i * 8);
	}
	
	for (i = 0; i < 11; i++) {
		q++;
		pushAnim(function (t, p1) { t.style.opacity = p1 / 100; t.style.filter = 'alpha(opacity=' + p1 +')';}, q, whiteDiv, i * 10);
		pushAnim(function (t, p1) { t.style.opacity = p1 / 100; t.style.filter = 'alpha(opacity=' + p1 +')';}, q, dropShadow, i * 5);
	}


	
	shadowDiv.appendChild(loadingDiv);
	lightBox.appendChild(shadowDiv);
	lightBox.appendChild(dropShadow);
	innerDiv.appendChild(otherDiv);
  	innerDiv.appendChild(innerImg);
	whiteDiv.appendChild(innerDiv);
	whiteDiv.appendChild(xDiv);
	lightBox.appendChild(whiteDiv);
	document.body.appendChild(lightBox);
	window.onresize();
}
