var Toplayer = new Class({
	
	// public:
	
	
	initialize: function() {
		var self = this;
		this._view = new Toplayer.View();
		this._view.addEvent('close', function() {
			self.hide();
		});
		this._loader = new Toplayer.Loader();
		this._loader.addEvent('load', function(response) {
			self.setContent(response);
		});
	},
	
	
	openUrl: function(url) {
		this._loader.load(url);
	},
	
	
	setContent: function(content) {
		var self = this;
		$each(content, function(element) {
			element.getElement('.toplayer-close').addEvent('click', function() {
				self.hide();
			});
		});
		this._view.getContentElement().adopt(content);
		this._view.show();
	},
	
	
	hide: function() {
		this._view.hide();
	},
	
	
	// private:
	
	_view: null,
	_loader: null
});


Toplayer.Loader = new Class({
	Implements: Events,
	
	// public:
	
	
	initialize: function() {
		var self = this;
		this._request = new Request.HTML({
			method: 'get',
			onSuccess: function(response) {
				self.fireEvent('load', [response]);
			}
		});
	},
	
	
	load: function(url) {
		this._request.cancel();
		this._request.send({url: url});
	},
	
	
	// private:
	
	_request: null
});


Toplayer.View = new Class({
	Implements: Events,
	
	// public:
	
	
	getContentElement: function() {
		if (this._content === null) {
			this._content = new Element(
				'div',
				{
					'class': 'js-popup-content'
				}
			);
			this._content.inject(this._getFrame());
		}
		return this._content;
	},
	
	
	show: function() {
		this._getOverlay().open();
		var frame = this._getFrame();
		
		function getTargetFramePosition() {
			return frame.position({
				relativeTo:	document.body,
				position:	'centerTop',
				edge:		'centerTop',
				returnPos:	true
			});
		}
		
		function setFramePositionStyles(pos) {
			frame.setStyles({
				position: 'absolute',
				top: pos.top,
				left: pos.left
			});
		};
		
		var firstPos = getTargetFramePosition();
		setFramePositionStyles(firstPos);
		
		this._positionSetTimer = (function() {
			var pos = getTargetFramePosition();
			pos.top = firstPos.top;
			setFramePositionStyles(pos);
		}).periodical(500);
		
		frame.fade('in');
	},
	
	
	hide: function() {
		clearInterval(this._posTimer);
		this._getOverlay().close();
		this._getFrame().fade('out');
	},
	
	
	// private:
	
	_overlay: null,
	_frame: null,
	_content: null,
	_positionSetTimer: null,
	
	
	_getOverlay: function() {
		var self = this;
		if (this._overlay === null) {
			this._overlay = new Overlay(
				document.body,
				{
					onClick: function() {
						self.fireEvent('close');
					}
				}
			);
		}
		return this._overlay;
	},
	
	
	_getFrame: function() {
		if (this._frame === null) {
			var self = this;
			
			this._frame = new Element(
				'div',
				{
					'class': 'js-popup-frame',
					'styles': {
						'z-index': 5001
					}
				}
			);
			this._frame.fade('hide');
			this._frame.inject(document.body);
			
			var closeButton = new Element('div', {
				'class': 'js-close-button'
			});
			closeButton
				.addEvent('click', function() {
					self.fireEvent('close');
				})
				.inject(this._frame);
		}
		return this._frame;
	}
});
