/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Adapted By: Simon de Haan
Website: http://blog.eight.nl
Date: 21/2/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
And the lightbox gone wild by ParticleTree at http://particletree.com/features/lightbox-gone-wild/

*/

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,
	
	width : '100%',
	height : '100%',

	initialize: function(ctrl) {
		this.content = ctrl.rel;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser.isIE){
			this.getScroll();
			this.prepareIE(this.height, 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		lbBody = document.getElementsByTagName('body')[0];
		lbBody.style.height = height;
		lbBody.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayLightbox: function(display){
		var dsElem = $(lightbox.prototype.onLoadPageContent);
		if ($('lbOverlay')){
		$('lbOverlay').style.display = display;
		}
		if(this.content){
		$(this.content).style.display = display;
		}
		else{
		dsElem.style.display = display;	
		}
		/* TUI Dark Site addition will remove close link and Icon*/			
		if (display == 'none' && lightbox.prototype.dsAlertIdElem){	
			dsElem.removeClassName('leightbox');
			dsElem.style.display = 'block';
			$$('.lbAction').each(function(lba){	
				if (lba.descendantOf(lightbox.prototype.dsAlertIdElem) === true){
				lba.remove();
				}
			});
		}
		/* TUI Dark Site addition */
		if(display != 'none'){
			this.actions();	
		}
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');
		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		if (browser.isIE){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto","");
			this.hideSelects("visible");
		}		
		this.displayLightbox("none");
	},
	
	// Add in markup necessary to make this work. Basically two divs:
	// lbOverlay holds the shadow
	// Lightbox is the centered square that the content is put into.
	addLightboxMarkup: function(){

	lbBody 				= document.getElementsByTagName('body')[0];

	lbOverlay 			= document.createElement('div');
	lbOverlay.id			= 'lbOverlay';

	lbBody.appendChild(lbOverlay);
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	lightbox.prototype.addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}
}

document.write('<link href="/css/screen.css" rel="stylesheet" media="screen, print" />');