
var lightBox = Class.create({
	
	_overlay: null,
	_lb: null,
	
	initialize: function(className){
		
		this._overlay = $('lightBoxOverlay');
				
		if (!this._overlay) {
			this._overlay = new Element('div',{
				id: 'lightBoxOverlay'
			}).hide().setStyle({
				height: this._getPageSize()[1] + "px"
			});
									
			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);
		
			this._center();
		}
								
	},
		
	open: function(content){
		this._overlay.show();
		this._lb.show().update(content);
		this._initCloseLinks();
		this._center();
	},
	
	close: function(){
		this._overlay.hide();
		this._lb.hide();
	},
	
	_center: function(){
		this._lb.setStyle({
			left: ((document.viewport.getWidth() - this._lb.getWidth()) / 2) + "px"
		});
	},
	
	_initCloseLinks: function(){
		this._lb.select(['a.close','img.lightBoxClose']).invoke('observe','click',function(e){
			this.close();
			e.stop();
		}.bind(this));
	},
	
    _getPageSize: function(){
	        
	    var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			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
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				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 = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}		
	
});

var errorLightBox = Class.create(lightBox, {
	
	open: function($super, content){
		this._lb.addClassName('errorLightBox')
		$super('<img class="lightBoxClose" src="gfx/lightbox.close.gif" alt="Schließen" title="Schließen" /><div class="lightBoxContent"><h1>Es ist ein Fehler aufgetreten.</h1><br /><p>' + content + '</p><p>Bitte versuchen Sie es in ein paar Minuten erneut.</p><p>Sollte das Problem auch nach längerer Zeit bestehen, können Sie sich an <a href="mailto:hilfe@altmuehlnet.de">hilfe@altmuehlnet.de</a> wenden.</p><p style="text-align: right;"><a class="close" href="javascript:;">Meldung schließen</a></p></div>');
	},
	
	close: function($super){
		this._lb.removeClassName('errorLightBox');
		$super();
	}
	
});

