
/**
 * LightBox
 * 
 */
var lightBox = Class.create({

	/**
	 * Overlay
	 */
	_overlay: null,

	/**
	 * Lightbox-Container
	 */
	_lb: null,

	/**
	 * Konstruktor
	 *
	 * @param {String} className optionale CSS-Klasse, die dem Lightbox-Container gegeben wird
	 */
	initialize: function(className){
		
		this._overlay = $('lightBoxOverlay');
				
		if (!this._overlay) {
			this._overlay = new Element('div',{
				id: 'lightBoxOverlay'
			}).setOpacity(0.5).hide();
			
			$(document.body).insert(this._overlay);
		}
		
		this._lb = $('lightBox');
		
		if (!this._lb) {
			this._lb = new Element('div', {
				id: 'lightBox'
			}).addClassName('lightBox').hide();
			
			if(className){
				this._lb.addClassName(className);
			}
			
			$(document.body).insert(this._lb);
		}
								
	},

	/**
	 * Öffnet die Lightbox
	 *
	 * @param {String} content optionaler Inhalt
	 */
	open: function(content){
		if(!this._overlay.visible()){
			
			if(bluegfx.IE6){
				var pageSize = this._getPageSize();

				this._overlay.setStyle({
					height: pageSize.height + "px",
					width: pageSize.width + "px"
				});
			}
			
			this._overlay.show();
		}

		if(this._lb.visible()){
			this._lb.setStyle({
				height: this._lb.getHeight() + "px"
			});
		}

		this._lb.update(content || '<img class="lightBoxLoad" src="gfx/lightbox.load.gif" alt="" title="" />');

		if(!this._lb.visible()){
			this._lb.setStyle({
				width: "112px"
			}).show();
		}

		this._initCloseLinks();
		this._center();
	},

	/**
	 * Schließt die Lightbox
	 * 
	 */
	close: function(e){
		this._overlay.hide();
		this._lb.hide();
		e.stop();
	},

	/**
	 * Zentriert die Lightbox
	 *
	 */
	_center: function(){
		this._lb.setStyle({
			left: ((document.viewport.getWidth() - this._lb.getWidth()) / 2) + "px",
			top: document.viewport.getScrollOffsets().top + Math.max(0, (document.viewport.getHeight() - this._lb.getHeight()) / 2) + "px"
		});
	},

	/**
	 * Initialisiert die Schließen-Links und Buttons
	 *
	 */
	_initCloseLinks: function(){
		this._lb.select(['a.close','img.lightBoxClose']).invoke('observe','click',function(e){
			this.close();
			e.stop();
		}.bind(this));
	},

	/**
	 * Ermittelt die Ausmaße der ganzen Seite
	 *
	 * @return {Array}
	 */
    _getPageSize: function(){

		var width = window.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
		var height = window.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);

		var scroll_x = window.scrollMaxX ? window.scrollMaxX + width : Math.max(Math.max(document.body.scrollWidth, document.getElementsByTagName('html')[0].scrollWidth), document.body.offsetWidth);
		var scroll_y = window.scrollMaxY ? window.scrollMaxY + height : Math.max(Math.max(document.body.scrollHeight, document.getElementsByTagName('html')[0].scrollHeight), document.body.offsetHeight);

		width = Math.max(width, scroll_x);
		height = Math.max(height, scroll_y);
		
		return {width: width, height: height};

	}		
	
});

