/*
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){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.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 ($('overlay')){
		$('overlay').style.display = display;
		}
		if(this.content){
		$(this.content).style.display = display;
		}
		else{
		dsElem.style.display = display;	
		}
		/* TUI Dark Site addition */			
		if (display == 'none' && lightbox.prototype.dsAlertIdElem){
			dsElem.removeClassName('leightbox');
			dsElem.style.display = 'block';
			$$('.lbAction').each(function(lba){	
				if (lba.descendantOf($(lightbox.prototype.dsAlertIdElem)));
				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");
	},
	
	addLightboxMarkup: function(){

	bod 				= document.getElementsByTagName('body')[0];

	overlay 			= document.createElement('div');
	overlay.id			= 'overlay';

	bod.appendChild(overlay);
}
}

/*-----------------------------------------------------------------------------------------------*/

// 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" />');

/* Extras used only for Darksite implementation of Lightbox - if we start using Lightbox elsewhere it might be adventagous to move it out of here to seperate data from functionality */
Extras = {

/* Configure a lightbox Dark Site warning to activate on onload */
darksite : function(activeClass, contentToDispId, alertId, closeLink, insertLinkElem){
	if(typeof(lightbox) === "function"){
		for(j=0,jlen=Extras.darksiteURLArray.length; j<jlen; j++){
			if (document.location.href === Extras.darksiteURLArray[j]){	
				if ($$(activeClass)[0]){
					var count = 0;
					for(i=0,ilen=Extras.darksiteURLArray.length; i<ilen; i++){
						if(!document.referrer.match(Extras.darksiteURLArray[i])){
						count++;					
						}
	    			}
					if (count === ilen){						
		    			if ($$(insertLinkElem)[0]){
		    			new Insertion.Top($$(insertLinkElem)[0], closeLink);	
		    			}
	    			lightbox.prototype.addLightboxMarkup();
					lightbox.prototype.dsAlertIdElem = $(alertId);
	    			lightbox.prototype.onLoadPageContent = $(contentToDispId); // id of the content you want to load
	    			lightbox.prototype.activate();
					}
					else{
						if($$(activeClass)[0]){
						$$(activeClass)[0].removeClassName('leightbox');
						}
					}
				}
			}
		}
	}	
	else{
		return false;
	}
}

}

var initExtras = function(){
/* Extras.darksiteURLArray[0] - [3] used to define only page that allows Darksite Lightbox overlay. Also the arrays URLs define the document referrers that will NOT trigger the overlay */
Extras.darksiteURLArray = new Array('http://10.33.118.24:90/', 'http://www.falconholidays.co.uk/', 'http://www.falconholidays.ie/', 'http://www.tuifalconpat.co.uk/', 'http://www.tuifalconpat.ie/', 'http://10.40.16.64:8090/', 'http://bookings.falconholidays.ie/');
/* Configure darksite (class required to activate it, id of content to put in modal overlay, url to match so that returning to home page will not repeatedly trigger darksite)*/
Extras.darksite('.darksite', 'lightbox1', 'SevereAlertHome', '<a rel="deactivate" class="lbAction insertedLbA" href="#"><strong>Close</strong> <img src="/images/buttons/close.gif" alt=""/></a>', '.newWarningBlockContent');
}

Event.observe(window, 'load', initExtras, false);