/*--------------------------------------------------------------------------*/
/*	Lightbox
*	This is a script for creating modal dialog windows (like the ones your operating
*	system uses)
*	
*/

var LightboxPopup = {
	/* hideAll - closes all open lightbox windows */
	hideAll: function(){
		lboxes = document.getElementsByClassName('lightbox')
		lboxes.each(function(box){
				Element.hide(box)
			}
		)
		if ($('overlay')){
			$('overlay').remove();
		}
	}
}
LightboxPopup.base = Class.create();
LightboxPopup.base.prototype = {

	initialize: function(element, options){
		//start by hiding all lightboxes
		LightboxPopup.hideAll();

		this.options = Object.extend({
			lightboxClassName : 'lightbox',
			closeOnOverlayClick : false,
			externalControl : false,
			url: false,
			width: 500, // default width
			height: 500 // default height
		}, options || {} )

		if (this.options.url) {
			//create the iframe
			var iframe = document.createElement("iframe");
			iframe.style.width = this.options.width + 'px';
			iframe.style.height = this.options.height + 'px';
			iframe.id = element + '_iframe';
			iframe.name = element + '_iframe';
			iframe.src = this.options.url;
            iframe.scrolling = 'no';

			//create a container for the iframe
			var container = document.createElement("div");
			container.style.width = this.options.width + 'px';
			container.style.height = this.options.height + 'px';
			container.id = element;

			//add iframe container
			container.appendChild(iframe);
			bod = document.getElementsByTagName('body')[0];
			bod.appendChild(container);
		}

		this.element = $(element);

		//create the overlay
		new Insertion.Before(this.element, "<div id='overlay' style='display:none;'></div>");
		
Element.addClassName(this.element, this.options.lightboxClassName)
	
		//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')
		
		//Tip: make sure the path to the close.gif image below is correct for your setup
		closer = '<img id="close" src="http://blog.feedmarker.com/wp-content/themes/feedmarker/lightbox/images/close.gif" alt="Close" title="Close this window" />'

		//insert the closer image into the div
		new Insertion.Top(this.element, closer);
		
		Event.observe($('close'), 'click', this.hideBox.bindAsEventListener(this) );
		
		if (this.options.closeOnOverlayClick){
			Event.observe($('overlay'), 'click', this.hideBox.bindAsEventListener(this) );
		}
		if (this.options.externalControl){
			Event.observe($(this.options.externalControl), 'click', this.hideBox.bindAsEventListener(this) );
		}
				
		this.showBox();	
	},
	
	showBox : function(){
		//show the overlay
	   Element.show('overlay');

		//center the lightbox
	   this.center();
	   
	   	//show the lightbox
	   Element.show(this.element);
	   return false;
	},
	
	hideBox : function(evt){	
		Element.removeClassName(this.element, this.options.lightboxClassName)
		Element.hide(this.element);
		//remove the overlay element from the DOM completely
        Element.remove('overlay');
        for (var i=0;i<4;i++) {
            if ($('overlay')) {
                $('overlay').remove();
            }
        }
        Element.remove(this.element);
		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   = 600;
		
		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";
        //this.element.scrolling = 'no';
		
	}

	
}

