var Ajaxbox = {

	init: function(options){
		this.options = $extend({
			resizeSpeed: 400,
			resizeTransition: false,	// default transition
			initialWidth: 500,
			initialHeight: 500,
			animate: true,
			overlayOpacity: 0.8
		}, options || {});
		
		this.resizeDuration = this.options.animate ? this.options.resizeSpeed : 0;
		
		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^ajaxbox/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': 'overlay'}).injectInside(document.body);

		this.center = new Element('div', {'id': 'tx_whidamgallery_abCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
		this.content = new Element('div', {'id': 'tx_whidamgallery_abContent', 'styles': {'height': this.options.initialHeight}}).injectInside(this.center);

		this.bottomContainer = new Element('div', {'id': 'tx_whidamgallery_abBottomContainer', 'styles': {'display': 'none', 'width': this.options.initialWidth}}).injectInside(document.body);
		this.bottom = new Element('div', {'id': 'tx_whidamgallery_abBottom'}).injectInside(this.bottomContainer);
		new Element('a', {'id': 'tx_whidamgallery_abCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: this.resizeDuration}).hide(),
			content: this.content.effect('opacity', {duration: this.resizeDuration, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: this.resizeDuration, onComplete: nextEffect})
		};
	},

	click: function(link){
		return this.open(link.rel);
	},

	open: function(params){
		// Split function name and parameters
		var tmp = params.split('||')[1].split('|');
		this.afunc = this.escape(tmp.shift());
		this.aparams = this.escape(tmp);

		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 30);
		this.center.setStyles({top: this.top, display: '', height: window.getHeight() * 0.8});
		//this.content.setStyles({height: (this.center.clientHeight - this.center.offsetTop)});
		this.content.setStyles({height: (window.getHeight() * 0.8 - 40)});
		// Added var "overlayOpacity" to be used here
		this.fx.overlay.start(this.options.overlayOpacity);
		return this.changeContent();

	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.abBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.abBackupStyle;
		});
		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: this.close(); break;
		}
	},

	changeContent: function(){
		if (this.step) return false;
		this.step = 1;

		this.bottomContainer.style.display = 'none';
		this.content.className = 'tx_whidamgallery_abLoading';

		this.nextEffect();
		
		return false;
	},
	
	nextEffect: function(){
		switch (this.step++){
		case 1:
			eval('var content = ' + this.afunc + '(this.aparams);');
			//this.content.className = '';
		//alert(content);
			if (content !== true) {
				this.content.setHTML('Sorry, error while fetching content...');
			}
			
			this.step++;
		case 2:
			this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.content.start(1);
			break;
		case 3:
			if (this.options.animate){
				this.fx.bottom.set(-this.bottom.offsetHeight - 20);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 4:
			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.content.chain(this.setup.pass(false, this)).start(0);
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		this.content.setHTML('');
		return false;
	},
	
	
	escape: function(params) {
		if (params.constructor.toString().indexOf("Array") == -1) {
			return escape(params);
		} else {
			var escapedParams = new Array();
			$each(params, function(param){
				escapedParams.push(escape(param));
			});
			return escapedParams;
		}
	}
};
