var MessageWindow = new Class({            

	initialize: function(options) {
		this.setOptions({
			"useViewPort": true,
			"travelDistance": 100,
			"containerWidth": 340,
			"containerHeight": "auto",
			"containerTop": 300,
			"containerLeft": "auto",
			"containerClass": "messageWindow",
			"timeout": null,
			"onShow": Class.empty,
			"onHide": Class.empty
		}, options);
	
		this.timer = 0;
		
		if(this.options.useViewPort){
			this.options.containerTop = (window.getHeight() / 2) + window.getScrollTop();
		}
		
		this.container = new Element('div', { 
			"opacity": 0,
			"styles": {
				"display": "none",
				"position": "absolute",
				"z-index": 999,
				"width": this.options.containerWidth,
				"height": this.options.containerHeight,
				"top": this.options.containerTop,
				"left": this.options.containerLeft
			},
			"events": {
				"click": this.hide.bind(this)
			},
			"class": this.options.containerClass
		}),
		
		this.message = new Element('div', {
			"opacity": 0 
		}).injectInside(this.container),
		
		this.fx = new Fx.Styles(this.container, {
			"wait": false,
			"duration": 400
		});

		this.fx.set({
			"opacity": 0,
			"top": this.options.containerTop
		});
		
		this.container.injectInside($(document.body));
	},
	
	setup: function(bShow){
		var elements = $A(document.getElementsByTagName('object'));
		if (window.ie6) elements.extend(document.getElementsByTagName('select'));
		elements.each(function(el){ el.style.visibility = bShow ? 'hidden' : ''; });
	},

	show: function(text) {
		if(this.options.onShow) this.options.onShow();
		this.setup(true);
		
		if(this.options.useViewPort){
			this.options.containerTop = (window.getHeight() / 2) + window.getScrollTop();
		}
	
		$clear(this.timer);
		this.container.setStyles({
			"top": this.options.containerTop,
			"display": "block",
			"left": this.options.containerLeft == "auto" ? (window.getWidth() / 2) - (this.options.containerWidth / 2) : this.options.containerLeft
		});
		this.message.setHTML(text);
		if(this.container.getStyle("opacity") != 1){
			this.fx.start({ 
				"opacity": [0, 1] 
			});
		}
		if(this.options.timeout){
			this.timer = this.hide.delay(this.options.timeout, this);
		}
	},

	hide: function() {
		if(this.options.onHide) this.options.onHide();
		this.setup(false);
		
		$clear(this.timer);
		this.fx.start({
			"opacity": 0,
			"top": this.options.containerTop - this.options.travelDistance
		});
	}

});

var Modalizer = new Class({
	defaultModalStyle: {
		display:'block',
		position:'fixed',
		top:'0px',
		left:'0px',	
		'z-index':500,
		'background-color':'#e8e8e8',
		opacity:0.8
	},

	setModalOptions: function(options){
		this.modalOptions = $merge({
			width:(window.getScrollWidth()+300)+'px',
			height:(window.getScrollHeight()+300)+'px',
			elementsToHide: 'select',
			onModalHide: Class.empty,
			onModalShow: Class.empty,
			hideOnClick: false,
			modalStyle: {},
			updateOnResize: true
		}, this.modalOptions, options || {});
	},
	resize: function(){
		if($('modalOverlay')) {
			$('modalOverlay').setStyles({
				width:(window.getScrollWidth()+300)+'px',
				height:(window.getScrollHeight()+300)+'px'
			});
		}
	},

	setModalStyle: function (styleObject){
		this.modalOptions.modalStyle = styleObject;
		this.modalStyle = $merge(this.defaultModalStyle, {
			width:this.modalOptions.width,
			height:this.modalOptions.height
		}, styleObject);
		if($('modalOverlay'))$('modalOverlay').setStyles(this.modalStyle);
		return(this.modalStyle);
	},

	modalShow: function(options){
		this.setModalOptions(options||{});
		var overlay = null;
		if($('modalOverlay')) overlay = $('modalOverlay');
		if(!overlay) overlay = new Element('div').setProperty('id','modalOverlay').injectInside(document.body);
		overlay.setStyles(this.setModalStyle(this.modalOptions.modalStyle));
		if(window.ie6) overlay.setStyle('position','absolute');
		$('modalOverlay').removeEvents('click').addEvent('click', function(){
			this.modalHide(this.modalOptions.hideOnClick);
		}.bind(this));
		this.bound = this.bound||{};
		if(!this.bound.resize && this.modalOptions.updateOnResize) {
			this.bound.resize = this.resize.bind(this);
			window.addEvent('resize', this.bound.resize);
		}
		this.modalOptions.onModalShow();
		this.togglePopThroughElements(0);
		overlay.setStyle('display','block');
		return this;
	},

	modalHide: function(override){
		if(override === false) return false; //this is internal, you don't need to pass in an argument
		this.togglePopThroughElements(1);
		this.modalOptions.onModalHide();
		if($('modalOverlay'))$('modalOverlay').setStyle('display','none');
		if(this.modalOptions.updateOnResize) {
			this.bound = this.bound||{};
			if(!this.bound.resize) this.bound.resize = this.resize.bind(this);
			window.removeEvent('resize', this.bound.resize);
		}

		return this;
	},
	togglePopThroughElements: function(opacity){
		if((window.ie6 || (window.gecko && navigator.userAgent.test('mac', 'i')))) {
			$$(this.modalOptions.elementsToHide).each(function(sel){
				sel.setStyle('opacity', opacity);
			});
		}
	}
});

MessageWindow.implement(new Options);
MessageWindow.implement(new Modalizer);

