var Textbox = {

	init: function(options){
		this.options = $extend({
			resizeDuration: 400,
			resizeTransition: false,
			initialWidth: 400,
			initialHeight: 300,
			endWidth: 1100,
			endHeight: 600,
			animateCaption: true,
			showCounter: true
		}, options || {});

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^textbox/i)){
				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', {'id': 'lbOverlay'}).injectInside(document.body);
		this.center = new Element('div', {'id': 'lbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
		
		this.textbox = new Element('div', {'id': 'lbTextBox'}).injectInside(this.center);

		this.bottomContainer = new Element('div', {'id': 'lbBottomContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
		this.bottom = new Element('div', {'id': 'lbBottom'}).injectInside(this.bottomContainer);
		new Element('a', {'id': 'lbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div', {'id': 'lbCaption'}).injectInside(this.bottom);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			textbox: this.textbox.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};
	},

	click: function(link){
		return this.open(link.rev, link.title);
	},

	open: function(rev,title){
		this.text = document.getElementById(rev).innerHTML;
		this.title = title;
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + 120;
		this.center.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.5);
		return this.changeText();
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
		}
	},
	
	changeText: function(){
		this.textbox.scrollTop = 0;
		this.textbox.scrollLeft = 0;
		this.step = 1;
		this.fx.textbox.hide();
		this.center.className = 'lbLoading';
		this.nextEffect();
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';
			this.textbox.innerHTML = this.text;
			if(textboxBackgroundColor != null || textboxBackgroundColor != undefined){
				this.textbox.style.backgroundColor = textboxBackgroundColor;
			}
			if(textboxColor != null || textboxColor != undefined){
				this.textbox.style.color = textboxColor;
			}
			this.textbox.style.width = this.bottom.style.width = this.options.endWidth+'px';
			this.textbox.style.height = this.options.endHeight+'px';
			//var caption = new captionObj(); 
			this.caption.setHTML(this.title);
			
			if (this.center.clientHeight != this.textbox.offsetHeight){
				this.fx.resize.start({height: this.textbox.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.textbox.offsetWidth){
				this.fx.resize.start({width: this.textbox.offsetWidth, marginLeft: -this.textbox.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.textbox.start(1);
			break;
		case 4:
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};
var textboxBackgroundColor = null;
var textboxColor = null;
window.addEvent('domready', Textbox.init.bind(Textbox));

