/*
---
description: Simple modal class (with overlay) for MooTools.
license: MIT-style
authors: [Christopher Pitt]
provides: [Modal]
requires: 
  Overlay (sixtyseconds)/0.1: [Overlay]
...
*/

(function(context) {

	var z, Modal = new Class({
		'Implements': [Options, Events],

		'options': {    
			/*
			'onLoad': $empty,
			'onPosition': $empty,
			'onShow': $empty,
			'onHide': $empty,
			'onButton': $empty,
			*/
			'zIndex': 1,
			'buttons': 'ok|cancel',
			'duration': 100,
			'color': '#000',
			'opacity': 0.7,
			'prefix': 'modal-'
		},

		'initialize': function(options)
		{
			this.setOptions(options);
			z = this.options.zIndex;
			var self = this,
				buttons = self.options.buttons.split('|'),

				overlay = new Overlay(self.options),

				border = new Element('div', {
					'class': self.options.prefix + 'border',
					'styles': {
						'z-index': z++,
						'position': 'absolute',
						'display': 'none'
					}
				}).inject(document.id(self.options.container || document.body)),
				win = new Element('div', {
					'class': self.options.prefix + 'window',
					'styles': {
						'z-index': z++
						//'position': 'absolute'
					}
				}).inject(border),
				content = document.id(self.options.content) || new Element('div', {'class': self.options.prefix + 'content'});
				content.setStyles({
					'z-index': z++
				}).inject(win);            
				Array.each(buttons, function(button) {
					new Element('a', {
						'class': 'button ' + button,
						'text': button,
						'href': '#',
						'events': {
							'click': function(e)
							{
								self.fireEvent('button', [e, button]);
							}
						}
					}).inject(win);
				});

			self.overlay = overlay;
			self.border = border;
			self.win = win;
			self.content = content;

			self.fireEvent('load');
		},

		'show': function()
		{
			this.overlay.show();
			this.border.setStyles({
				'display': 'block',
				'top': window.getScrollTop() + 100
			});
			this.fireEvent('show');
		},

		'hide': function()
		{
			this.overlay.hide();
			this.border.setStyle('display', 'none');
			this.fireEvent('hide');
		}
	});

	context.Modal = Modal;

})(window ? window : this);
