/*
 * =================================================================
 * LightBox
 * =================================================================
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)
	
	Adapted by IgoUgo, 2006.
 */

LightBox = {
   HideCallback: null
};

LightBox.init = function() {
   var objBody = document.getElementsByTagName("body").item(0);
   // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	// without the following line, it crashes IE with a "Runtime Error"
	objOverlay.innerHTML = '';
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () { LightBox.hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.width = '100%';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '100000';
 	objBody.insertBefore(objOverlay, objBody.firstChild);
	
   
	// create lightbox div, same note about styles as above
	var objLightbox = document.createElement("div");
	// without the following line, it crashes IE with a "Runtime Error"
	objLightbox.innerHTML = '';
	objLightbox.onclick = function () { LightBox.hideLightbox(); return false;}
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100001';	
	objLightbox.style.left = '100px';	
	objLightbox.style.top = '100px';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
}

LightBox.appendChildToLight = function(pChild) {
	var objLightbox = $('lightbox');
	if (pChild && objLightbox) {
		objLightbox.appendChild(pChild);
	}
}

// disableOverlayClosing()
// Removes the onclick function that closes
// the lightbox when a click occured on the 
// overlay
LightBox.disableOverlayClosing = function() {
	var objOverlay = $('overlay');
	if (objOverlay) {
		objOverlay.onclick = function() { return false };
	}
	var objLightbox = $('lightbox');
	if (objLightbox) {
		objLightbox.onclick = function() { return false };
	}
}

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
LightBox.getPageScroll = function() {
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
LightBox.getPageSize = function() {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
LightBox.showLightbox = function(objDisplay, pOnLoad, pOnCloseCallback) {
   LightBox.HideCallback = pOnCloseCallback;
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	
	var arrayPageSize = LightBox.getPageSize();
	var arrayPageScroll = LightBox.getPageScroll();
	
	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	if (objDisplay) {
		if (pOnLoad) {
			objDisplay.onload = function() {
				objDisplay.style.display='block';
			}
		} else {
			objDisplay.style.display='block';
		}
		var oDisplayWidth = (objDisplay.width?objDisplay.width:objDisplay.style.width);
		oDisplayWidth = oDisplayWidth.replace('px', '');
		var oDisplayHeight = (objDisplay.height?objDisplay.height:objDisplay.style.height);
		oDisplayHeight = oDisplayHeight.replace('px', '');
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - oDisplayHeight) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - oDisplayWidth) / 2);
	
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	}
	
	objLightbox.style.display = 'block';
	// make select boxes not visible
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
	// make elements named ad not visible
	ads = document.getElementsByName("ad");
	for (i = 0; i != ads.length; i++) {
		ads[i].style.visibility = "hidden";
	}

}

//
// hideLightbox()
//
LightBox.hideLightbox = function() {
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	
	// hide child node of Lightbox
	for (var i = 0; i < objLightbox.childNodes.length; i++) {
		objLightbox.childNodes[i].style.display='none';
	}
	// make select boxes visible
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
	// make elements named ad visible
	ads = document.getElementsByName("ad");
	for (i = 0; i != ads.length; i++) {
		ads[i].style.visibility = "visible";
	}
	// disable keypress listener
	document.onkeypress = '';
	
	if (LightBox.HideCallback && typeof(LightBox.HideCallback) == 'function') {
	   LightBox.HideCallback();
	   LightBox.HideCallback = null;
	}
	
}

LightBox.switchOverlay = function() {
	if (this.mIsOverlayed) {
		LightBox.hideLightbox();
		this.mIsOverlayed = false;
	} else {
		this.mIsOverlayed = true;
		LightBox.showLightbox(null);
	}
}

