var Interface = {
	dialog: function() {
		if (absent('layer_dialog')) {	
			var notifyInner = new Element('div', { id: 'layer_dialog_body' }).addClassName('dialog_body');
			var notifyOuter = new Element('div', { id: 'layer_dialog' }).setStyle({ zIndex: '6000' }).insert(notifyInner);
			$('overlay').insert({ bottom: notifyOuter }).show();
			$('layer_dialog').show();
			
			this.activateDialogCloseOnKeyBound = this.activateDialogCloseOnKey.bindAsEventListener(this);
			this.setDialogLoading();
			this.activateDialogCloseOnKeyBound();
		}

		return $('layer_dialog').centerize();
	},
	setDialogLoading: function(text) {
		$('layer_dialog_body').update("<div id='loading_message'>" + $('loadbars').contents() +"<p>Loading...</p></div>");
		$('layer_dialog').setStyle({width: '200px'}).centerize();
	},
	insertLoadingBars: function(element) {
		$$("div#baseload").detect(function(e) { e.remove(); });
		var bars = new Element('div', { id: 'baseload'}).update($('preloadimg_load_bar_orange').contents());
		$(element).insert({ bottom: bars });
	},
	destroyLoadingBars: function() {
		$('baseload').remove();
	},
	updateDialog: function(contents, importOptions) {
		$$('#close_dialog').detect(function(e) { e.remove(); });
		var closedInner = new Element('div', { id: 'close_dialog' }).update();
		$('layer_dialog').insert(closedInner);
		options = $H({
			width: "",
			height: "",
		}).update(importOptions);
		
		$('layer_dialog_body').update(contents);
		if (options) {
			if (options.get('width')) $('layer_dialog').setWidth(options.get('width')); 
			if (options.get('height')) $('layer_dialog').setHeight(options.get('height')); 
		}
		
		$('close_dialog').observe('click', Interface.destroyDialog);
		this.centerizeDialog();
		return $('layer_dialog');
	},
	insertDialog: function(html) {
		$('layer_dialog_body').insert({bottom: html});
		$('layer_dialog').centerize();
	},
	centerizeDialog: function() {
		var clientHeight = document.viewport.getDimensions().height;
		if ( $('layer_dialog').getHeight() >= clientHeight) {
			$('layer_dialog_body').setStyle({ height: clientHeight - 50 +'px'});
		}	
		$('layer_dialog').centerize();
	},
	destroyDialog: function(callback) {
		Effect.Fade('layer_dialog', {
			duration: 0.2,
			afterFinish: function() {
				$('layer_dialog').remove();
				$('overlay').hide();
				document.stopObserving('keydown', Interface.activateDialogCloseOnKeyBound(Interface));
				if (typeof callback == 'function') { callback(); }
			}
		});
				
	},
	activateDialogCloseOnKey: function() {
		document.observe('keydown', function(e) {
			if (e.keyCode == 27) {
				Interface.destroyDialog();
			}
		});
	},	
};	

/* is element absent from DOM? */
var absent = function(element) {
	if ($(element) == undefined) { return true; }
	else { return false; }
};
/* is element present in DOM? */
var present = function(element) {
	if ($(element) == undefined) { return false; }
	else { return true; }
};

