var PopUp = new Class({
	Implements : [Options, Events],
	options : {
		'maskopacity' : .6,
		'bodymaskopacity' : .6,
		'initheight' : 250,
		'animate' : true, 
		'hidefloatingelements' : true
	},
	itsMask : null,
	itsPopUpContainer : null,
	itsPopUp : null,
	itsPopUpHeader : null,
	itsPopUpBody : null,
	itsPopUpFooter : null,
	itsPopUpContent : null,
	itsKeyboard : null,
	initialize : function(options) {
		if (PopUp.popUp) return PopUp.popUp.setOptions(options);
		this.setOptions(options);
		this.itsKeyBoard = new Keyboard({
			defaultEventType : 'keyup', 
			events : {'esc' : (function() {this.close();}).bind(this)}
		});
		PopUp.popUp = this;
	},
	show : function(anchor, blockID) {
		if (this.options.hidefloatingelements) $try(function() {$$('select', 'object', 'embed').setStyle('visibility', 'hidden');}); // Doesn't work on < IE 9
		if (!this.itsMask) this.createMask();
		this.itsMask.element.fade(this.options.maskopacity);
		if (!this.itsPopUpContainer) this.createPopUp();
		var uri = (new URI(anchor.href)).setData({
			'pid' : null,
			'bid' : blockID
		}, true);
		this.load(uri.toString());
		return true;
	},
	close : function() {
		this.itsMask.element.set('tween', {
			'onComplete' : (function() {
				this.itsMask.destroy();
				this.itsMask = null;
			}).bind(this)
		});
		this.itsMask.element.fade(0);
		this.itsPopUpContainer.destroy();
		this.itsPopUpContainer = null;
		if (this.options.hidefloatingelements) $try(function() {$$('select', 'object', 'embed').setStyle('visibility', 'visible');});
		return true;
	},
	createMask : function() {
		this.itsMask = new Mask(null, {id : 'mask'});
		this.itsMask.element.setStyles({
			'display' : 'block',
			'position' : 'absolute',
			'top' : '0px',
			'left' : '0px',
			'opacity' : '0',
			'width' : '100%',
			'height' : $(document.body).getScrollSize().y
		}).set('tween', {
			'opacity' : 0
		}).set('events', {
			'click' : (function() {this.close();}).bind(this)
		});
		return true;
	},
	createPopUp : function() {
		this.itsPopUpContainer = new Element('div', {'id' : 'popup-container', 'class' : 'popup-container'}).adopt([
			(this.itsPopUp = new Element('div', {'id' : 'popup', 'class' : 'popup'})).adopt([
				(this.itsPopUpHeader = new Element('div', {'class' : 'popup-header'})).adopt([
					new Element('div', {'id' : 'popup-close', 'class' : 'popup-close', 'events': {'click' : (function() {this.close();}).bind(this)}})
				]),
				(this.itsPopUpBody = new Element('div', {'class' : 'popup-body'})).adopt([
					(this.itsPopUpContent = new Element('div', {'id' : 'popup-content', 'class' : 'popup-content'}))
				]),
				(this.itsPopUpFooter = new Element('div', {'class' : 'popup-footer'}))
			])
		]);
		var top = $(document.body).getScroll().y + ($(document).getSize().y / 10);
		this.itsPopUpContainer.setStyles({
			'position' : 'absolute', 
			'left' : '0px',
			'top' : top+'px'
		});
		this.itsPopUpContent.setStyle('height', this.options.initheight);
		this.itsPopUpContent.set('spinner', {
			'id' : 'popup-content-mask', 
			'class' : 'popup-content-mask',
			'style' : {
				'opacity' : this.options.bodymaskopacity
			}, 
			'fxOptions' : {
				'duration' : 0
			}
		});
		this.itsPopUpContainer.inject($(document.body));
		return true;
	},
	load : function(uri) {
		new Request.HTML({
			'url' : uri,
			'method' : 'get',
			'evalScripts' : false,
			'onRequest' : (function() {
				this.itsPopUpContent.spin();
			}).bind(this),
			'onSuccess' : (function(responseTree, responseElements, responseHTML, responseJavaScript) {
				this.itsPopUpContent.empty();
				this.itsPopUpContent.unspin();
				this.itsPopUpContent.innerHTML = responseHTML;
				this.itsPopUpContent.setStyle('height', 'auto');
				if (this.options.animate) {
					this.itsPopUpBody.set('tween', {
						'link' : 'cancel',
						'complete' : (function() {this.itsMask.element.setStyle('height', $(document.body).getScrollSize().y);}).bind(this),
						'unit' : 'px'
					});
					this.itsPopUpBody.tween('height', this.itsPopUpContent.getScrollSize().y);
				}
			}).bind(this)
		}).send();
		return true;
	}
});
PopUp.popUp = null;
