
var MessageBox = {

	init: function(options) {
		this.options = Object.extend({
			resizeTransition: Fx.Transitions.sineInOut,
			resizeDuration: 400,
			initialWidth: 450,
			initialHeight: 100,
			animateCaption: false
		}, options || {});

		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);

		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: 'auto', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
		this.buttons = new Element('div').setProperty('id', 'lbButtons').injectInside(this.center);

		this.overlay.onclick = this.close.bind(this);

		this.isOpen = this.isLoading = false;

		this.fx = {
			overlay: new Fx.Tween(this.overlay, {
				property: 'opacity',
				onStart: Events.prototype.clearChain,
				duration: 500,
				link: 'cancel'
			}).set(0)
		};
		$(document.body).adopt(this.overlay);


	},

	open: function() {
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15) + 100;
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.8);

		this.isOpen = true;

		return false;
	},

	position: function() {
		this.overlay.setStyles({top: window.getScrollTop()+'px', height: window.getHeight()+'px'});
	},

	setup: function(open) {
		var elements = $A(document.getElementsByTagName('object'));
		if (window.ie) elements.extend(document.getElementsByTagName('select'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		this.step = 0;
	},

    //Bestaetitung zu einer Aktion abfragen
	confirm: function(confirmText, onComplete) {

			this.buttons.set('html', '');

			this.open();

			this.confirmText = new Element('p');
			this.confirmText.set('html', confirmText);

			this.confirmButtonYes = new Element('input', {
				'id': 'confirmButtonYes',
				'type': 'submit',
				'value': lang['tooltip.yes'],
				'class': 'button',
				'styles': {
					'margin-right': '10px'
				}
			});

			this.confirmButtonNo = new Element('input', {
				'type': 'submit',
				'value': lang["tooltip.no"],
				'class': 'buttonsec'
			});

			this.confirmButtonYes.addEvent('click', function() {
				this.close();
				onComplete(true);
			}.bind(this));

			this.confirmButtonNo.addEvent('click', function() {
				this.close();
				return false;
			}.bind(this));

			this.confirmText.injectInside(this.buttons);
			this.confirmButtonYes.injectInside(this.buttons);
			this.confirmButtonNo.injectInside(this.buttons);

			$('confirmButtonYes').focus();
    },

    //Status anzeigen
	status: function(string) {

			this.buttons.set('html', '');

			this.open();

			this.confirmText = new Element('div');
			this.confirmText.set('html', string);

			this.confirmButtonOk = new Element('input', {
				'id': 'confirmButtonOk',
				'type': 'submit',
				'value': lang["tooltip.close"],
				'class': 'button',
				'styles': {
					'margin-top': '10px',
					'margin-right': '10px'
				}
			});

			this.confirmButtonOk.addEvent('click', function() {
				this.close();
                return false;
			}.bind(this));

			this.confirmText.injectInside(this.buttons);
			this.confirmButtonOk.injectInside(this.buttons);

			$('confirmButtonOk').focus();

    },

    //Eingabemaske anzeigen
	input: function(introStr, buttonStr, valueDefault, onComplete) {

			this.buttons.set('html', '');

			this.open();

			this.confirmText = new Element('p');
			this.confirmText.set('html', introStr);

			this.messageTextarea = new Element('textarea', {
				'id': 'messageTextarea',
				'class': 'text',
				'value': valueDefault,
				'styles': {
					'display': 'block',
					'height': '100px',
					'width': '100%',
					'margin-bottom': '20px'
				}
			});

			this.confirmButtonYes = new Element('input', {
				'id': 'confirmButtonYes',
				'type': 'submit',
				'value': buttonStr,
				'class': 'button',
				'styles': {
					'margin-right': '10px'
				}
			});

			this.confirmButtonNo = new Element('input', {
				'type': 'submit',
				'value': lang["tooltip.cancel"],
				'class': 'buttonsec'
			});

			this.confirmButtonYes.addEvent('click', function() {
				this.close();
                onComplete($('messageTextarea').value);
			}.bind(this));

			this.confirmButtonNo.addEvent('click', function() {
				this.close();
                return false;
			}.bind(this));

			this.confirmText.injectInside(this.buttons);
			this.messageTextarea.injectInside(this.buttons);
			this.confirmButtonYes.injectInside(this.buttons);
			this.confirmButtonNo.injectInside(this.buttons);

			$('confirmButtonYes').focus();
    },

	close: function(e) {
		var stoppable = ($type(e) == 'event');
		if (stoppable) e.stop();
		if (!this.isOpen || (stoppable && !$lambda(this.options.closable).call(this, e))) return this;
		this.fx.overlay.start(0);
		this.center.setStyle('display', 'none');
		this.isOpen = false;
		return this;
	}

};

window.addEvent('load', MessageBox.init.bind(MessageBox));
