/*
 * dieses Script basiert auf:
 * 
 * Slimbox v1.22 - The ultimate lightweight Lightbox clone
 * by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
 * Inspired by the original Lightbox v2 by Lokesh Dhakar.
 * 
 * und wurde im Dezember 2006 erweitert und angepasst (fuer Textdarstellung) durch
 * Torsten Berger, WahnBerlin
 * www.wahnberlin.de
 * Berlin, Germany
 * 
 */

var Wahntextbox = {

	init: function(options) {
		this.options = Object.extend({
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 250,		// Initial width of the box (px)
			initialHeight: 250,		// Initial height of the box (px)
			animateCaption: true	// Enable/Disable caption animation
		}, options || {});
		
		this.anchors = [];
		$A(document.getElementsByTagName('a')).each(function(el){
			if(el.rel && el.href && el.rel.test('^wahntextbox', 'i')) {
				$textheight = this.initialHeight;
				$textwidth = this.initialWidth;

				zahl_h = el.rel.test('[h]\[[0-9]+\]');
				if (zahl_h) { 
					$textheight = zahl_h[0].test('[0-9]+');
				}
				zahl_h = '';

				zahl_w = el.rel.test('[w]\[[0-9]+\]');
				if (zahl_w) { 
					$textwidth = zahl_w[0].test('[0-9]+');
				}
				zahl_w = '';
				el.textheight = $textheight;
				el.textwidth = $textwidth;
				
				el.onclick = this.click.pass([el], this);
				
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div').setProperty('id', 'wtbOverlay').injectInside(document.body);

		this.center = new Element('div').setProperty('id', 'wtbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
		this.text = new Element('div').setProperty('id', 'wtbText').injectInside(this.center);
		this.overlay.onclick = this.close.bind(this);

		this.bottom = new Element('div').setProperty('id', 'wtbBottom').setStyle('display', 'none').injectInside(document.body);
		new Element('a').setProperties({id: 'wtbCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);


		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', { duration: 500 }).hide(),
			resize: this.center.effects({ duration: this.options.resizeDuration, onComplete: nextEffect }),
			bottom: this.bottom.effects({ duration: 400, onComplete: nextEffect })
		};
	},

	click: function(link) {
		this.textheight = link.textheight;
		this.textwidth = link.textwidth;
		this.poplink = link.href;

		return this.open();
	},

	open: function() {
		this.position();
		this.setup(true);
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.goTo(0.8);
		return this.showText();
	},

	position: function() {
		this.overlay.setStyles({top: '0px', height: Window.getScrollHeight()+'px'});
	},

	setup: function(open) {
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ActiveXObject ? 'select' : 'embed'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},
	
	keyboardListener: function(event) {
		switch(event.keyCode) { 
			case 27: case 81: case 88: case 67: this.close(); break; // esc, q, x, c
		}
	},

	showText: function() {	
		if(this.step) return false;
		this.step = 1;
		this.text.setHTML('');
		this.center.className = 'wtbLoading';
		this.nextEffect();
		return false;
	},

	nextEffect: function() {

		switch(this.step++) {
		
		case 1:
			if(this.textheight && (this.center.clientHeight != this.textheight)) {
				this.fx.resize.custom({height: [this.center.clientHeight, this.textheight]});
				break;
			}
			this.step++;	
		
		case 2:
			if(this.textwidth && (this.center.clientWidth != this.textwidth)) {
				this.fx.resize.custom({width: [this.center.clientWidth, this.textwidth], marginLeft: [-this.center.clientWidth/2, -this.textwidth/2]});
				break;
			}
			this.step++;
		
		case 3:
			var myAjax = new Ajax(this.poplink, {update: $(this.text), evalScripts: true});
			myAjax.request();
			this.center.className = '';

		case 4:
			this.bottom.setStyles({top: (this.top + this.center.clientHeight + 10)+'px', width: (this.textwidth-20)+'px', marginLeft: this.center.style.marginLeft, display: ''});
			break;

		case 5:
			if(this.options.animateCaption) {
				this.fx.bottom.custom({opacity: [0, 1], height: [0, this.bottom.scrollHeight]});
				break;
			}
			this.bottom.setStyles({opacity: '1', height: this.bottom.scrollHeight+'px'});

		case 6:
			this.step = 0;
		}
	},

	close: function() {
		if(this.step < 0) return;
		this.step = -1;
		for(var f in this.fx) this.fx[f].clearTimer();
		this.center.style.display = this.bottom.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).goTo(0);
		this.overlay.setStyles({top: '0px', height: '0px'});
		return false;
	}
};

Window.onDomReady(Wahntextbox.init.bind(Wahntextbox));
