var Dialog = {};
Dialog.Box = Class.create();
Object.extend(Dialog.Box.prototype, {
	initialize: function(id) {
		this.createOverlay();
		this.dialog_box = $(id);
		this.dialog_box.show = this.show.bind(this);
		this.dialog_box.hide = this.hide.bind(this);
		
		this.parent_element = this.dialog_box.parentNode;
		
		this.dialog_box.style.position = "absolute";
		var e_dims = Element.getDimensions(this.dialog_box);
		var b_dims = Element.getDimensions(this.overlay);
		this.dialog_box.style.left = ((b_dims.width/2) - (e_dims.width/2)) + 'px';
		this.dialog_box.style.top = '10px';
		this.dialog_box.style.zIndex = this.overlay.style.zIndex + 1;
	},

	createOverlay: function() {
		if($('dialog_overlay')) {
			this.overlay = $('dialog_overlay');
		} else {
			this.overlay = document.createElement('div');
			this.overlay.id = 'dialog_overlay';
			Object.extend(this.overlay.style, {
				zIndex: 99998,
				width: '100%'
			});
			// this should be styled in the stylesheets... id = dialog_overlay
			/*
			Object.extend(this.overlay.style, {
				position: 'absolute',
				top: 0,
				left: 0,
				zIndex: 90,
				width: '100%',
				backgroundColor: '#000',
				display: 'none'
			});
			*/
			document.body.insertBefore(this.overlay, document.body.childNodes[0]);
		}
	},

	moveDialogBox: function(where) {
		Element.remove(this.dialog_box);
		if(where == 'back')
			this.dialog_box = this.parent_element.appendChild(this.dialog_box);
		else
			this.dialog_box = this.overlay.parentNode.insertBefore(this.dialog_box, this.overlay);
	},

	show: function(forceAction) {
		this.overlay.style.height = Element.getDimensions(document.body).height+'px';
		this.moveDialogBox('out');
		// Comment this line out if you don't want the dialog to disappear when clicking on the background
		if (forceAction == 1) { } else {
			this.overlay.onclick = this.hide.bind(this);
		}
		this.selectBoxes('hide');
		this.flashContent('hide');
		this.iframeContent('hide');
		new Effect.Appear(this.overlay, {duration: 0.1, from: 0.0, to: 0.8});
		this.dialog_box.style.display = '';
		// Go to top of page
		window.scrollTo(0,0);
	},

	hide: function() {
		this.selectBoxes('show');
		this.flashContent('show');
		this.iframeContent('show');
		new Effect.Fade(this.overlay, {duration: 0.5});
		this.dialog_box.style.display = 'none';
		this.moveDialogBox('back');
		$A(this.dialog_box.getElementsByTagName('input')).each(function(e){if(e.type!='submit')e.value=''});
	},

	selectBoxes: function(what) {
		$A(document.getElementsByTagName('select')).each(function(select) {
			Element[what](select);
		});
		
		if(what == 'hide')
			$A(this.dialog_box.getElementsByTagName('select')).each(function(select){Element.show(select)})
	},
	
	flashContent: function(what) {
		$A(document.getElementsByTagName('object')).each(function(select) {
			Element[what](select);
		});
		
		if(what == 'hide')
			$A(this.dialog_box.getElementsByTagName('object')).each(function(select){Element.show(select)})
	},
	
	iframeContent: function(what) {
		$A(document.getElementsByTagName('iframe')).each(function(select) {
			Element[what](select);
		});

		if(what == 'hide')
			$A(this.dialog_box.getElementsByTagName('iframe')).each(function(select){Element.show(select)})
	}
});

