DarkenBackground = Class.create();
DarkenBackground.prototype = {
	initialize: function(id, aobj) {
		var self = this;

		var id = id;
		var style = {
			'background':	'black',
			'height':   	'101%',
			//'height':   	'240%',
			'width':    	'100%',
			'left':		 	0,
			'top': 			0,
			'opacity': 		0.45,
			'position': 	'absolute',
			'zIndex':		'40'
		};

		this.element = new Element('div', {'id': id});
		this.element.setStyle(style);

		if (aobj)
		{
			this.obj = aobj;
		}

		this.element.observe('click', function() {
				self.element.remove();

				if (self.obj)
				{
					self.obj.hide();
				}
			}
		);

		this.show();
	},

	show: function() {
		var body = $(body);
		if (! body)
			body = $('body').up();
		if (! body)
			body = $('#body').up();
		if (body)
			body.insert(this.element);
	}
};

ShowModal = Class.create();
ShowModal.prototype = {
	initialize: function (id) {

		var obj = $(id);
		obj.addClassName('modal-dialog');

		var odims = obj.getDimensions();
		var dims = document.viewport.getDimensions();
		var offset = document.viewport.getScrollOffsets();

		new DarkenBackground('modal_bg' + id, obj);

		if (odims.height < dims.height)
		{
			var val = (dims.height - odims.height) / 2;
			obj.setStyle({'top': '', 'bottom': val + 'px', 'position': 'fixed'});
		}
		else
		{
			if (set_modal_on_top == 1)
			{
				var val = dims.bottom;
				if (!val)
					val = 10;
				obj.setStyle({'top': '', 'bottom': val + 'px', 'position': 'absolute'});
			}
			else
			{
				var val = dims.bottom;
				if (!val)
					val = 10;
				obj.setStyle({'top': val + 'px', 'bottom': '', 'position': 'absolute'});
			}
		}
		
		if (odims.width < dims.width)
		{
			var val = (dims.width - odims.width + offset.left) / 2;
			obj.setStyle({'left': val + 'px'});
		}
		else
		{
			var val = offset.left;
			obj.setStyle({'left': offset.left + 'px'});
		}


		var close_id = id + '_close';
		var close = $(close_id);

		if (close)
		{
			close.observe('click', function () {
				var back = $('modal_bg' + id);
				if (back)
					back.remove();
				$(id).hide();
			});
		}

		var body = $(body);
		if (! body)
			body = $('body').up();

		var el = obj.remove();

		body.setStyle({'position': 'relative'});
		el.setStyle({'zIndex': 50});
		
		body.insert(el);
		el.show();
	}
};
