var Lightbox = {
	lightboxType : null,
	lightboxCurrentContentID : null,
	
	showBoxString : function(content, boxWidth, boxHeight){
		this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		var contents = $('boxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},

	showBoxImage : function(href,caption) {
		this.lightboxType = 'image';
		
		var contents = $('boxContents');
		var caption = caption || '';
		$('box').className = '';
		var objImage = document.createElement("img");
		objImage.setAttribute('id','lightboxImage');
		contents.appendChild(objImage);
		imgPreload = new Image();
		imgPreload.onload=function(){
			objImage.src = href;
			Lightbox.showBox();
		};
		imgPreload.src = href;
		$('caption').innerHTML= caption;
		return false;
	},

	showBoxByID : function(id, boxWidth, boxHeight) {
		this.lightboxType = 'id';
		this.lightboxCurrentContentID = id;
		this.setLightboxDimensions(boxWidth, boxHeight);
		var element = $(id);
		var contents = $('boxContents');
		contents.appendChild(element);
		Element.show(id);
		this.showBox();
		return false;
	},

	showBoxByAJAX : function(href, boxWidth, boxHeight) {
		this.lightboxType = 'ajax';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('boxContents');
		$('box').className = 'ajax';
		$('caption').innerHTML= '';
		var myAjax = new Ajax.Updater(contents, href, {method: 'get', evalScripts: true});
		
		this.showBox();
		return false;
	},
	
	setLightboxDimensions : function(width, height) {
		var windowSize = this.getPageDimensions();
		if(width) {
			if(width < windowSize[0]) {
				$('box').style.width = width + 'px';
			} else {
				$('box').style.width = (windowSize[0] - 50) + 'px';
			}
		}
		if(height) {
			if(height < windowSize[1]) {
				$('box').style.height = height + 'px';
			} else {
				$('box').style.height = (windowSize[1] - 50) + 'px';
			}
		}
	},


	showBox : function() {
		hideSelectBoxes();
		hideFlashMovies();
		Element.show('overlay');
		Event.observe('overlay', 'click', this.hideBox, false);
		this.center('box');
		// document.getElementsByTagName('body')[0].style.overflow='hidden';
		return false;
	},
	
	
	hideBox : function(){
		Element.hide('box');
		var contents = $('boxContents');
		if(this.lightboxType == 'id') {
			var body = document.getElementsByTagName("body").item(0);
			Element.hide(this.lightboxCurrentContentID);
			body.appendChild($(this.lightboxCurrentContentID));
		}
		contents.innerHTML = '';
		$('box').style.width = null;
		$('box').style.height = null;
		Element.hide('overlay');
		//show select boxes
		showSelectBoxes();
		showFlashMovies();
		// document.getElementsByTagName('body')[0].style.overflow='auto';		
		return false;
	},
	
	// taken from lightbox js, modified argument return order
	getPageDimensions : 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(windowWidth,windowHeight,pageWidth,pageHeight);
		return arrayPageSize;
	},
	
	center : function(element,status){
		try{
			element = document.getElementById(element);
		}catch(e){
			return;
		}
		
		var windowSize = this.getPageDimensions();
		var window_width  = windowSize[0];
		var window_height = windowSize[1];

		//so you don't get white BG when the window is small and you scroll - hacky fix
		var adjustOverlay = (windowSize[1]<=598 ? 200 : 0);
		$('overlay').style.height = windowSize[3] + adjustOverlay +'px';
		
		element.style.position = 'absolute';
		element.style.zIndex   = 99;
	
		var scrollY = 0;
	
		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}
	
		var elementDimensions = Element.getDimensions(element);
		var setX = ( window_width  - elementDimensions.width  ) / 2;
		var setY = ( window_height - elementDimensions.height ) / 2 + scrollY;
	
		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;
	
		//takes scroll bar into consideration
		element.style.left = setX-16 + "px";
		element.style.top  = setY + "px";

		Element.show(element);
	},
	
	init : function() {				  
		var lightboxtext = '<div id="overlay" title="Close popup" style="display:none"></div>';
		lightboxtext += '<div id="box" style="display:none">';
		lightboxtext += '<div id="top" class="close"><span id="caption"></span><a href="#" onclick="Lightbox.hideBox(); return false;">Close [x]</a></div>';
		lightboxtext += '<div id="boxContents"></div>';
		lightboxtext += '</div>';
		var body = document.getElementsByTagName("body").item(0);
		new Insertion.Bottom(body, lightboxtext);
	}
};

// important because in IE selectboxes appear above the lightbox
function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	};
};

function showFlashMovies(){
	if( $('interactive_map') )
		$('interactive_map').show();
};

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	};
};

function hideFlashMovies(){
	if( $('interactive_map') )
		$('interactive_map').hide();
}

function init(){
	Lightbox.init();
};

function center(){
	($('box').style.display!="none"? Lightbox.center('box') : '');
};

// execute the script when the page has loaded
//Lightbox addition
// Event.onDOMReady(function(){init();});
Event.observe(window,"load",init, false);
Event.observe(window, 'resize', center, false);