/*
	Lightbox : A lightweight lightbox utility
	
	Basic to use.  Simply include this file in your header, then after the page
	loads call the Lightbox.show( obj ) function.  Where obj is the DOM object
	you want displayed in the lightbox container.
	 
	
	Functions
	 - getPageScroll
	 - getPageScroll
	 - getPageScroll
	 - getPageScroll
	 - getPageScroll

*/

var Lightbox = new Object();

Lightbox.maxOpacity = (navigator.appVersion.indexOf("MSIE")!=-1) ? 70 : 100;

Lightbox.init = function( obj ) {
	if (obj == undefined) obj = {};
	
	Lightbox.loadingImage    = obj.loadingImage    ? obj.loadingImage    : "/images/loading.gif";
	Lightbox.closeButton     = obj.closeButon      ? obj.closeButton     : "/images/close.gif";
	Lightbox.backgroundImage = obj.backgroundImage ? obj.backgroundImage : "/images/lightbox-overlay.png";
	
	Lightbox.id                 = obj.id                 ? obj.id                 : "lightbox";
	Lightbox.containerId        = obj.containerId        ? obj.containerId        : "lightbox_container";
	Lightbox.loadingImageId     = obj.loadingImageId     ? obj.loadingImageId     : "lightbox_loading";
	Lightbox.containerContentId = obj.containerContentId ? obj.containerContentId : "lightbox_container_content";
	Lightbox.contentId          = obj.contentId          ? obj.contentId          : "lightbox_content";
	
	Lightbox.fps = 5;
	
	Lightbox.hidden = true;
	
	Lightbox.createLightbox();
}

Lightbox.createLightbox = function() {
	if (document.getElementById(Lightbox.id)) return;
	
	// load / create all necessary objects
	var objLightbox  = document.createElement("div");
	var objContainer = document.createElement("div");
	var objContent   = document.createElement("div");
	var objCloseLink = document.createElement("a");
	var objClose     = document.createElement("img");
	var objLoading   = document.createElement("img");
	var objBody      = document.getElementsByTagName("body").item(0);
	
	// create the lightbox background / overlay
	objLightbox.setAttribute("id", Lightbox.id);
	objLightbox.style.backgroundImage = "url("+Lightbox.backgroundImage+")";
	objLightbox.style.position = "absolute";
	objLightbox.style.top = 0;
	objLightbox.style.left = 0;
	objLightbox.style.width = "100%";
	objLightbox.style.height = "100%";
	objLightbox.style.zIndex = "900000000";
	objLightbox.style.display = "none";
	objLightbox.style.opacity = "0";
	objLightbox.style.mozOpacity = "0";
	objLightbox.style.filter = "alpha(opacity=0)";
	
	objBody.appendChild(objLightbox);
	
	
	// create lightbox container
	objContainer.setAttribute("id", Lightbox.containerId);
	objContainer.style.background = "#EEE";
	objContainer.style.padding = "10px";
	objContainer.style.borderBottom = "1px solid #373737";
	objContainer.style.borderRight = "1px solid #373737";
	objContainer.style.position = "absolute";
	objContainer.style.top = "20%";
	objContainer.style.left = "20%";
	objContainer.style.width = "100px";
	objContainer.style.zIndex = "950000000";
	objContainer.style.display = "none";
	objContainer.style.opacity = "0";
	objContainer.style.MozOpacity = "0";
	objContainer.style.filter = "alpha(opacity=0)";
	objContainer.style.font = "11px/13px 'Tahoma', Arials, Sans-serif";
	objContainer.style.color = "#EEEEEE";
	
	objContent.setAttribute("id", Lightbox.contentId);
	objContent.style.background = "#373737";
	objContent.style.padding = "10px";
	objContent.style.textAlign = "left";
	
	objClose.setAttribute("src", Lightbox.closeButton);
	objClose.onclick = function() { Lightbox.hide(); };
	objClose.border = 0;
	objClose.style.position = "absolute";
	objClose.style.top = "5px";
	objClose.style.right = "5px";
	
	//objCloseLink.setAttribute("onclick", "Lightbox.hide()");
	objCloseLink.setAttribute("href", "javascript:Lightbox.hide();");
	objCloseLink.appendChild(objClose);
	
	objContainer.appendChild(objCloseLink);
	
	objContainer.appendChild(objContent);
	objBody.appendChild(objContainer);
}

Lightbox.setContainerContent = function( obj ) {
	var objContent   = document.getElementById(Lightbox.contentId);
	var objContainer = document.getElementById(Lightbox.containerId);
	
	while(objContent.firstChild) { objContent.removeChild(objContent.firstChild); }
	
	objContent.appendChild(obj);
	
	var pagesize   = Lightbox.getPageSize();
	
	var locTop = (pagesize[3] - objContainer.offsetHeight) / 2;
	var locLeft = (pagesize[2] - objContainer.offsetWidth) / 2;
	
	objContainer.style.top  = locTop + "px";
	objContainer.style.left = locLeft + "px";
}

Lightbox.show = function( obj ) {
	if (obj == undefined) obj = {};
	
	Lightbox.hidden = false;
	
	if (typeof obj == "string") {
		var o = document.createElement("div");
		o.innerHTML = obj;
		
		obj = {
			content: o
		}
	}
	
	var pagesize   = Lightbox.getPageSize();
	var pagescroll = Lightbox.getPageScroll();

	// hide all embed and select objects
	Lightbox.hideAll("embed");
	Lightbox.hideAll("select");
	Lightbox.hideAll("iframe");
	
	var objLightbox  = document.getElementById(Lightbox.id);
	var objContainer = document.getElementById(Lightbox.containerId);
	var objContent   = document.getElementById(Lightbox.contentId);
	
	objLightbox.style.display = "block";
	objContainer.style.display = "block";
	
	objLightbox.style.height = pagesize[1] + "px";
	
	if ( !document.all )
	{
		new Effect.Appear( $( Lightbox.id ) );
		new Effect.Appear( $( Lightbox.containerId ) );
	}
	else
	{
		new Effect.Appear( $( Lightbox.id ), { to: .7 } );
		new Effect.Appear( $( Lightbox.containerId ) );
	}
	
	if (!obj.content) {
		obj.content = document.createElement("div");
		obj.content.setAttribute("style", "text-align: left");
		obj.content.innerHTML = "<h1>Not Found</h1><p>Sorry for the inconvenience, but I don't know what to show you...</p>";
	}
	
	objContent.appendChild(obj.content);
	objContent.style.width = "410px";
	objContainer.style.width = "430px";
	
	if (obj.onClose) {
		Lightbox.onClose = obj.onClose;
	}
	
	var locTop = (pagesize[3] - objContainer.offsetHeight) / 2;
	var locLeft = (pagesize[2] - objContainer.offsetWidth) / 2;
	
	objContainer.style.top  = locTop + "px";
	objContainer.style.left = locLeft + "px";
	
	Lightbox.oldScroll = pagescroll;
	
	scroll(0,0);
	
	document.getElementsByTagName("body").item(0).style.overflow = "hidden";
}

Lightbox.hide = function( obj ) {
	if (Lightbox.hidden) return;
	
	Lightbox.hidden = true;
	
	if (obj == undefined) obj = {};
	
	var objLightbox  = document.getElementById(Lightbox.id);
	var objContainer = document.getElementById(Lightbox.containerId);
	var objContent   = document.getElementById(Lightbox.contentId);
	
	new Effect.Fade( $( Lightbox.id ) );
	new Effect.Fade( $( Lightbox.containerId ) );
	
	while(objContent.firstChild) {
		objContent.removeChild(objContent.firstChild);
	}
	
	setTimeout( function() {
					try { Lightbox.onClose(); } catch(e) { };
					Lightbox.onClose = new Function();
					
					Lightbox.showAll("embed");
					Lightbox.showAll("select");
					Lightbox.showAll("iframe");
					
					objLightbox.style.display = "none";
					objContainer.style.display = "none";
				}, 1000);
				
	try {
		scroll(0,Lightbox.oldScroll[1]);
	} catch(e) {}
	
	document.getElementsByTagName("body").item(0).style.overflow = "";
}

Lightbox.hideAll = function( objName ) {
	try {
		var objects = document.getElementsByTagName(objName);
		
		for(i = 0; i < objects.length; i++) {
			
			if (objName == "embed")
			{
				objects[i].oldHeight = objects[i].style.height;
				objects[i].style.height = "0px";
			}
			else
			{
				objects[i].oldDisplay = objects[i].style.display;
				objects[i].style.display = "none";
			}
		}
		
		var frames = document.frames;
		
		for( i = 0; i < frames.length; i++ )
		{
			var objects = document.frames[i].getElementsByTagName(objName);
			
			for(i = 0; i < objects.length; i++) {
				
				if (objName == "embed")
				{
					objects[i].oldHeight = objects[i].style.height;
					objects[i].style.height = "0px";
				}
				else
				{
					objects[i].oldDisplay = objects[i].style.display;
					objects[i].style.display = "none";
				}
			}
		}
	}
	catch(e) {}
}

Lightbox.showAll = function( objName ) {
	try {
		var objects = document.getElementsByTagName(objName);
		
		for(i = 0; i < objects.length; i++) {
			if (objName == "embed")
			{
				objects[i].style.height = objects[i].oldHeight;
			}
			else
			{
				objects[i].style.display = objects[i].oldDisplay;
			}
		}
		
		var frames = document.frames;
		
		for( i = 0; i < frames.length; i++ )
		{
			var objects = document.frames[i].getElementsByTagName(objName);
			
			for(i = 0; i < objects.length; i++) {
				if (objName == "embed")
				{
					objects[i].style.height = objects[i].oldHeight;
				}
				else
				{
					objects[i].style.display = objects[i].oldDisplay;
				}
			}
		}
	}
	catch(e) {}
}

Lightbox.getSpeed = function( from, to, steps ) {
	return (to - from) / steps;
}

//
// 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;
}

Lightbox.hideListener = function() {
	if (Lightbox.hiding) {
		Lightbox.overlay.steps = Lightbox.overlay.steps - 1;
		
		Lightbox.opacity           = Lightbox.opacity           + Lightbox.overlay.speed;
		Lightbox.container.opacity = Lightbox.container.opacity + Lightbox.container.speed;
		
		Lightbox.setOpacity(Lightbox.id, Lightbox.opacity);
		Lightbox.setOpacity(Lightbox.containerId, Lightbox.container.opacity);
		
		if (Lightbox.overlay.steps < 1) {
			Lightbox.hiding = false;
		}
	}
}

Lightbox.setOpacity = function(id, opacity) {
	document.getElementById(id).style.opacity = opacity / 100;
	document.getElementById(id).style.MozOpacity = opacity / 100;
	document.getElementById(id).style.filter = "alpha(opacity=" + opacity + ")";
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

Lightbox.getKey = function(keyStroke) {
	if (Lightbox.hidden) return;
	
   var keyCode = (document.layers) ? keyStroke.which : keyStroke.keyCode;
   var keyString = String.fromCharCode(keyCode).toLowerCase();
   
   if (keyCode == 27) Lightbox.hide();
}

addEvent(window, "keypress", Lightbox.getKey);

Lightbox.init();	// run initLightbox onLoad