/*--------------------------------------------------------------------------*/
/*	Lightbox	
*	This is a script for creating modal dialog windows (like the ones your operating
*	system uses)
*	
*/

var Lightbox = {
	/* hideAll - closes all open lightbox windows */
	hideAll: function(){
		lboxes = document.getElementsByClassName('lbox')
		lboxes.each(function(box){
				Element.hide(box)
			}
		)
		if ($('overlay')){
			//Element.remove('overlay');
			Element.hide('overlay')
			}
	},
	
    showOverlay : function(element){
	    if ($('overlay')) {
	        Element.show('overlay');
	    }
	    else {
	        new Insertion.Before(element, "<div id='overlay' style='display:none;'></div>");
	    }
	}
}
Lightbox.base = Class.create();
Lightbox.base.prototype = {

	initialize: function(element, options){
	    var closer = ''
	    
		//start by hiding all lightboxes
		Lightbox.hideAll();
	
		this.element = $(element);
		this.options = Object.extend({
			//cssClassName : 'lightbox',
			closeImageHREF : 'images/close.gif',
			//closeOnOverlayClick : false, // feature broken
			closeLink : false
		}, options || {} )

		//create the overlay
		Lightbox.showOverlay(this.element);
		
//Element.addClassName(this.element, this.options.cssClassName)
	
		//also add a default lbox class to the lightbox div so we can find and close all lightboxes if we need to
		Element.addClassName(this.element, 'lbox')
		if (this.options.closeImageHREF != ''){
			closer = '<img id="close" src="' + this.options.closeImageHREF +'" alt="Close" title="Close this window" class="close"/>'
		    //insert the closer image into the div
		    new Insertion.Top(this.element, closer);
		}
		//if the closer image is shown then attach event for it
		if (closer !='') {
		    Event.observe($('close'), 'click', this.hideBox.bindAsEventListener(this) );
		    }
		if (this.options.closeOnOverlayClick){
			Event.observe($('overlay'), 'click', this.hideBox);
		   //($('overlay')).onclick = this.hideBox;
		   }
		if (this.options.closeLink){
			Event.observe($(this.options.closeLink), 'click', this.hideBox.bindAsEventListener(this) );
			//($(this.options.closeLink)).onclick = this.hideBox;
		}
		this.showBox();	
	},
	
	showBox : function(){
		//show the overlay
	        Lightbox.showOverlay(this.element);
		if (this.options.closeOnOverlayClick){
			Event.observe($('overlay'), 'click', this.hideBox);
		   }
		//center the lightbox
	   this.center();
	   	//show the lightbox
	   Element.show(this.element);
	   return false;
	},
	
	hideBox : function(evt){	
		Element.hide(this.element);
		//Element.removeClassName(this.element, this.options.cssClassName)
		Event.stopObserving($('overlay'), 'click', this.hideBox);
		//hide the overlay
		Element.hide('overlay');
		return false;
	},
		
	center : function(){
		var my_width  = 0;
		var my_height = 0;
		
		if ( typeof( window.innerWidth ) == 'number' ){
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
		}else if ( document.documentElement && 
				 ( document.documentElement.clientWidth ||
				   document.documentElement.clientHeight ) ){
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		}
		else if ( document.body && 
				( document.body.clientWidth || document.body.clientHeight ) ){
			my_width  = document.body.clientWidth;
			my_height = document.body.clientHeight;
		}
		
		this.element.style.position = 'absolute';
		this.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(this.element);
		
		var setX = ( my_width  - elementDimensions.width  ) / 2;
		var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;
		
		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;
		
		this.element.style.left = setX + "px";
		this.element.style.top  = setY + "px";
		
	}

	
}

