//makes all hrefs that go to image script, go to lightbox instead
function LightBoxify() {
	var links = document.getElementsByTagName("a"); 
	var type;
	var id;
	for (var i = 0; i < links.length; i++) {
		if (!links[i].href) continue; //weed out any A tags without hrefs
		var urlSplit = links[i].href.split("?");
		if (/viewimage\.php/i.test(urlSplit[0])) {
			var idMatch = /id=(\d+)/.exec(urlSplit[1]);
			if (idMatch != null) {
				id = idMatch[1];
				type = 1;
			} else {
				idMatch = /image=([^&]+)/.exec(urlSplit[1]);
				if (idMatch != null) {
					id = idMatch[1];
					type = 2;
				} else continue;
			}
		} else if (/download\.php/i.test(urlSplit[0])) {
			var idMatch = /id=(\d+)/.exec(urlSplit[1]);
			if (idMatch != null) {
				id = idMatch[1];
				type = 4;
			} else continue;
		} else continue; //don't touch other links
		links[i].onclick = new Function("return OpenLightBox(" + type + ",'" + id + "')");
	}
}

var toggledfit; //used for image function
var toggledmax;
function OpenLightBox(type, id) {
	var ie=/*@cc_on!@*/false;
	toggledfit = true;
	toggledmax = false;
	var pageAttribs = getPageSize();
	//alert(type + " " + id);
	
	try {
		var overlay = document.createElement("div");
		overlay.setAttribute("id", "lightbox-overlay");
		overlay.onclick = CloseLightBox;
		var lightboxCont = document.createElement("div");
		lightboxCont.setAttribute("id", "lightbox-container");
		//lightboxCont.onclick = CloseLightBox;
		if (ie) {
			overlay.style.setAttribute("cssText", 
				"width: " + pageAttribs[0] + "px;" +
				"height: " + pageAttribs[1] + "px;" +
				"filter: alpha(opacity=0);"
			);
		} else {
			overlay.setAttribute("style", 
				"height: " + pageAttribs[1] + "px;" +
				"opacity: 0.0;"
			);
		}
		
		lightboxCont.innerHTML = '<table id="lightbox-frame" cellspacing="0" cellpadding="0">' + 
			'<tr><td class="frameLT"/><td class="frameT"/><td class="frameRT"/></tr>' + 
			'<tr><td class="frameL"/><td class="frameback"><div id="lightbox"></div></td><td class="frameR"/></tr>' + 
			'<tr><td class="frameLB"/><td class="frameB"/><td class="frameRB"/></tr>' + 
			'</table>';
		
		document.body.appendChild(overlay);
		document.body.appendChild(lightboxCont);
		
		FadeInOverlay();
		
		var lightbox = document.getElementById("lightbox");
		lightbox.innerHTML = 'Loading...';
		centerLightBox();
		
		var lightboxHttp = GetXmlHttpObject();
		if (lightboxHttp == null) {
			lightbox.innerHTML = 'Error!<br/>No AJAX Support.';
			centerLightBox();
			return true;
		}
		
		switch (type) {
			case 1:
				var url = "/lightbox_image.php?id=" + id + "&w=" + pageAttribs[2] + "&h=" + pageAttribs[3]; break;
			case 2:
				var url = "/lightbox_image.php?image=" + id + "&w=" + pageAttribs[2] + "&h=" + pageAttribs[3]; break;
			case 3:
				var url = "/lightbox_archive.php?id=" + id + "&w=" + pageAttribs[2] + "&h=" + pageAttribs[3]; break;
			case 4:
				var url = "/lightbox_download.php?id=" + id; break;
			default:
				return true;
		}
		
		
		lightboxHttp.onreadystatechange = function () { 
			if (lightboxHttp.readyState == 4) {
				lightbox.innerHTML = lightboxHttp.responseText;
				delete lightboxHttp;
				centerLightBox();
			}
		}
		
		lightboxHttp.open("GET", "http://www.snakebytestudios.com" + url, true);
		lightboxHttp.send(null);
		
		return false; 
	} catch (err) {
		return true; //return true if the lightbox fails
	}
}

function CloseLightBox() {
	FadeOutOverlay();
	document.body.removeChild(document.getElementById("lightbox-container"));
}


var overlayOpacityEndTime = -1;
var overlayOpacity = 0;
var FadeInTimer;
var OpacityMax = 65;
function FadeInOverlay() {
	var FadeTime = 750;
	var FadeStep = 20;
	
	now = new Date()
	if (overlayOpacityEndTime < 0) overlayOpacityEndTime = now.getTime() + FadeTime;
	var timeElapsed = FadeTime - (overlayOpacityEndTime - now.getTime());
	
	if ((timeElapsed <= FadeTime) || (overlayOpacity < OpacityMax)) { //testing both to make sure the opacity gets to the right value
		overlayOpacity = (timeElapsed / FadeTime) * OpacityMax;
		if (overlayOpacity > OpacityMax) overlayOpacity = OpacityMax;
		
		if (/*@cc_on!@*/false) {
			document.getElementById("lightbox-overlay").style.filter = "alpha(opacity=" + overlayOpacity + ")";
		} else {
			document.getElementById("lightbox-overlay").style.opacity = (overlayOpacity/100);
		}
		FadeInTimer = setTimeout(arguments.callee, FadeStep);
	} else {
		overlayOpacityEndTime = -1;
		overlayOpacity = 0;
	}
}

function FadeOutOverlay() {
	var FadeTime = 300;
	var FadeStep = 20;
	
	clearTimeout(FadeInTimer);
	
	now = new Date()
	if (overlayOpacityEndTime < 0) overlayOpacityEndTime = now.getTime() + FadeTime;
	var timeElapsed = FadeTime - (overlayOpacityEndTime - now.getTime());
	
	if (timeElapsed <= FadeTime) {
		var opacity = OpacityMax - ((timeElapsed / FadeTime) * OpacityMax);
		if (opacity < 0) opacity = 0;
		
		if (/*@cc_on!@*/false) {
			document.getElementById("lightbox-overlay").style.filter = "alpha(opacity=" + opacity + ")";
		} else {
			document.getElementById("lightbox-overlay").style.opacity = (opacity/100);
		}
		setTimeout(arguments.callee, FadeStep);
	} else {
		overlayOpacityEndTime = -1;
		document.body.removeChild(document.getElementById("lightbox-overlay"));
	}
}

function centerLightBox() {
	var lightbox = document.getElementById("lightbox-container");
	var pageAttribs = getPageSize();
	
	if (/*@cc_on!@*/false) {
		lightbox.style.setAttribute("cssText", 
			"top: " + (pageAttribs[5] + Math.round((pageAttribs[3] - lightbox.offsetHeight) / 2)) + "px;" + 
			"left: " + Math.round((pageAttribs[2] - lightbox.offsetWidth) / 2) + "px;"
		);
	} else {
		lightbox.setAttribute("style", 
			"top: " + (pageAttribs[5] + Math.round((pageAttribs[3] - lightbox.offsetHeight) / 2)) + "px;" + 
			"left: " + Math.round((pageAttribs[2] - lightbox.offsetWidth) / 2) + "px;"
		);
	}
}

(function(DomReady) {
var ua = navigator.userAgent;
var ie=/*@cc_on!@*/false;
if (/webkit/i.test(ua)) {
	setTimeout(function() {
		var dr=document.readyState;
		if (dr=="loaded"||dr=="complete") {
			DomReady();
		} else {
			setTimeout(arguments.callee,10);
		}
	} ,10);
} else if ((/mozilla/i.test(ua)&&!/(compati)/.test(ua)) || (/opera/i.test(ua))) {
	document.addEventListener("DOMContentLoaded", DomReady, false);
} else if (ie) {
	(function(){
		var t=document.createElement('doc:rdy');
		try {
			t.doScroll('left');
			DomReady();
			t=null;
		} catch(e){
			setTimeout(arguments.callee,10);
		}
	})();
} else {
	window.onload=DomReady;
}
})(LightBoxify);

/*
[0] -> page width
[1] -> page height
[2] -> window width
[3] -> window height
[4] -> scroll x
[5] -> scroll y
*/
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {  
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}   
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){  
		pageWidth = xScroll;        
	} else {
		pageWidth = windowWidth;
	}
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {     // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft; 
	}
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, xScroll, yScroll) 
	return arrayPageSize;
};

function GetXmlHttpObject() {
	var XMLHttpFactories = [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];

	var xmlhttp = null;
	for (var i=0; i<XMLHttpFactories.length; i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		} catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}


//code for image-specific lightboxes
/*function lightboxZoom(fitWidth, fitHeight) {
	if (toggledmax == true) return; //current implementation of maximize zooms already
	var lightboxImage = document.getElementById("lightbox-image");
	var lightboxImageFit = document.getElementById("lightbox-imgfit");
	var lightboxImageOuter = document.getElementById("lightbox-placeholder");
	if (toggledfit == true) {	//zoom in
		lightboxImageOuter.style.overflow = "scroll";
		lightboxImage.style.width = "auto";
		lightboxImage.style.height = "auto";
		lightboxImageFit.src = "/images/img_zoom_out.gif";
		lightboxImageFit.title = "Zoom Out";
		toggledfit = false;
	} else {	//zoom out
		lightboxImage.style.width = "100%";
		lightboxImage.style.height = "100%";
		lightboxImageOuter.style.overflow = "hidden";
		lightboxImageFit.src = "/images/img_zoom_in.gif";
		lightboxImageFit.title = "Zoom In";
		toggledfit = true;
	}
}*/

function lightboxFullScreen(fitWidth, fitHeight, origWidth, origHeight) {
	var lightboxImage = document.getElementById("lightbox-image");
	var lightboxImageMax = document.getElementById("lightbox-imgmax");
	var lightboxImageOuter = document.getElementById("lightbox-placeholder");
	var lightbox = document.getElementById("lightbox-container");
	var lightboxImageCaption = document.getElementById("lightbox-image-caption");
	var pageAttribs = getPageSize();
	
	if (toggledmax == false) { //maximize
		var padding = 26; //16 - border width, 10 - lightbox margin
		var maxSizeWidth = pageAttribs[2] - padding;
		var captionHeight = 0;
		if (lightboxImageCaption != null) captionHeight = lightboxImageCaption.offsetHeight
		var maxSizeHeight = pageAttribs[3] - (padding + captionHeight);
		
		if (origWidth > maxSizeWidth) {
			lightboxImageOuter.style.overflowX = "scroll";
		} else {
			maxSizeWidth = origWidth;
			if (origHeight > maxSizeHeight) maxSizeWidth += 17; //account for scrollbar width
		}
		if (origHeight > maxSizeHeight) {
			lightboxImageOuter.style.overflowY = "scroll";
		} else {
			maxSizeHeight = origHeight;
			//if (origWidth > maxSizeWidth) maxSizeHeight += 17; //account for scrollbar height (caption height?)
		}
		
		lightbox.style.left = ((pageAttribs[2] - maxSizeWidth) / 2) + "px";
		lightbox.style.top = ((pageAttribs[3] - maxSizeHeight) / 2) + "px";
		lightboxImageOuter.style.width = maxSizeWidth + "px";
		lightboxImageOuter.style.height = maxSizeHeight + "px";
		if (lightboxImageCaption != null) lightboxImageCaption.style.width = maxSizeWidth + "px";
		lightboxImage.style.width = "auto";
		lightboxImage.style.height = "auto";
		
		lightboxImageMax.src = "/images/frames.gif";
		lightboxImageMax.title = "Restore";
		toggledmax = true;
	} else { //restore
		var padding = 16; //16 - border width
		var restoreWidth = fitWidth + padding;
		var restoreHeight = fitHeight + padding;
	
		lightbox.style.left = ((pageAttribs[2] - restoreWidth) / 2) + "px";
		lightbox.style.top = ((pageAttribs[3] - restoreHeight) / 2) + "px";
		lightboxImageOuter.style.width = restoreWidth + "px";
		lightboxImageOuter.style.height = restoreHeight + "px";
		if (lightboxImageCaption != null) lightboxImageCaption.style.width = restoreWidth + "px";
		lightboxImage.style.width = "100%";
		lightboxImage.style.height = "100%";
		lightboxImageOuter.style.overflow = "hidden";
		
		lightboxImageMax.src = "/images/noframes.gif";
		lightboxImageMax.title = "Maximize";
		toggledmax = false;
	}
	centerLightBox();
}