// http://snippets.dzone.com/posts/show/3411
// Modified by igorbt and jetzt.de

var DialogManager = Class.create({

	initialize: function(overlayId, dialogClassName, overlayZIndex) {
		this.overlayZIndex = overlayZIndex;
		this.overlayId = overlayId;
		this.dialogClassName = dialogClassName;
		
		// Cache positionBox function to allow for disabling of 
		// event handling
		// (see http://www.prototypejs.org/api/event/stopObserving
		// -> "Why won't it stop observing!?")
		this.positionBoxCached = this.positionBox.bindAsEventListener(this);
	},
	
	overlay : function() {
		if ($(this.overlayId)) {
			this.dialog_overlay = $(this.overlayId);
		} else {
			this.dialog_overlay = this.generateOverlay(this.overlayId, this.overlayZIndex),
			$$('body').first().insertBefore(this.dialog_overlay, $$('body').first().childNodes[0]);
		}
		var bodyDimensions = $$('body').first().getDimensions();
		this.dialog_overlay.style.height = bodyDimensions.height + 'px';
		this.dialog_overlay.onclick = this.off.bind(this);
		if (this.dialog_overlay.style.display == 'none') {
			new Effect.Appear(this.dialog_overlay, {
				duration :0.1,
				from :0.0,
				to :0.7
			});
		}
	},
	
	generateOverlay: function(idval, zindexval) {
		var overlay = document.createElement('div');
		overlay.id = idval;
		Object.extend(overlay.style, {
			position :'absolute',
			top :0,
			left :0,
			zIndex : zindexval,
			width :'100%',
			backgroundColor :'#000',
			display :'none'
		});
		return overlay;
	},
	
	off: function() {
		this.clear();
		new Effect.Fade(this.dialog_overlay, {
			duration :0.1
		});
	},
	
	display: function(box) {
		this.clear();
		this.overlay();
		this.dialog_box = $(box);
		this.dialog_box.hide();
		this.dialog_box.addClassName(this.dialogClassName);
		this.dialog_box.style.position = 'absolute';
		this.dialog_box.style.zIndex = Math.floor(this.dialog_overlay.style.zIndex) + 1;
		this.dialog_box = this.dialog_overlay.parentNode.insertBefore(this.dialog_box, this.dialog_overlay);
		this.positionBox();
		this.dialog_box.show();
		// Reposition box upon browser window resize
		Event.observe(document.onresize ? document : window, "resize", this.positionBoxCached);  
	},
	
	positionBox: function() {
		var elemdims = Element.getDimensions(this.dialog_box);
		var viewdims = document.viewport.getDimensions();
		var offsets = document.viewport.getScrollOffsets();

		this.dialog_box.style.left = offsets.left
									 + Math.max(0, viewdims.width / 2
									 - elemdims.width /2) + 'px';
		
		this.dialog_box.style.top = offsets.top 
									+ Math.max(0, viewdims.height/2
									-elemdims.height/2)+ 'px';
	},
	
	
	clear: function() {
		// Stop observing the resize event
		Event.stopObserving(document.onresize ? document : window, "resize", this.positionBoxCached);  
		// Remove dialog box
		$$('.'+this.dialogClassName).each(function(elem) {elem.remove();});		
	},
	
	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)
			})
	},
	
	
	
	displayMessage: function(message) {
		var strVar="";
		strVar += "<div class=\"dialogBox\" style=\"width:300px\">";
		strVar += "	<div class=\"dialogBoxTitlebar dialogBoxClearfix\" unselectable=\"on\">";
		strVar += "		<a href=\"javascript:void(0)\" class=\"dialogBoxTitlebarClose dialogBoxClose\" unselectable=\"on\">";
		strVar += "			<span unselectable=\"on\">close<\/span>";
		strVar += "		<\/a>";
		strVar += "     Meldung";
		strVar += "	<\/div>";
		strVar += "	<div class=\"dialogBoxContent\">";
		strVar += message;
		
		strVar += "  <div class=\"clearfix\">";
		strVar += "    <p>";
		strVar += "      <input class=\"submitbutton dialogBoxClose\" type=\"button\" value=\"OK\" \/>";
		strVar += "    <\/p>";
		strVar += "  <\/div>";

		
		strVar += "	<\/div>";
		strVar += "<\/div>";
		var box = document.createElement('div');
		box = $(box);
		box.update(strVar);
		box.select('.dialogBoxClose').each(
				function(elem){
					elem.onclick=this.off.bind(this);
				}.bind(this));
		this.display(box);
	}
});

var ErrorDialogManager = Class.create(DialogManager, {
	displayMessage: function(message) {
	var strVar="";
	strVar += "<div class=\"dialogBox error\" style=\"width:300px\">";
	strVar += "	<div class=\"dialogBoxTitlebar dialogBoxClearfix\" unselectable=\"on\">";
	strVar += "		<a href=\"javascript:void(0)\" class=\"dialogBoxClose dialogBoxTitlebarClose\" unselectable=\"on\">";
	strVar += "			<span unselectable=\"on\">close<\/span>";
	strVar += "		<\/a>";
	strVar += "     Fehler";
	strVar += "	<\/div>";
	strVar += "	<div class=\"dialogBoxContent\">";
	strVar += message;
	strVar += "  <div class=\"clearfix\">";
	strVar += "    <p>";
	strVar += "      <input class=\"submitbutton dialogBoxClose\" type=\"button\" value=\"OK\" \/>";
	strVar += "    <\/p>";
	strVar += "  <\/div>";
	strVar += "	<\/div>";
	strVar += "<\/div>";
	var box = document.createElement('div');
	box = $(box);
	box.update(strVar);
	box.select('.dialogBoxClose').each(
			function(elem){
				elem.onclick=this.off.bind(this);
			}.bind(this));
	this.display(box);
	},
	displayError: function(error) {
		return this.displayMessage(error);
	}
});