var Popup;
var AjaxPopup;

window.addEvent('domready', function() {
	
	Popup = new Class({
		
		Implements: Options,
		options: {
			'className': 'popup',
			'container': document.body
		},
		popup: null,
		content: null,
		
		// Initialisierung
		initialize: function(options){
			this.setOptions(options);
			this.popup = new Element('div', {
				'class': this.options.className
			});
			var content_div = new Element('div', {
				'class': 'popupContent'
			});
			this.content = content_div;
			content_div.inject(this.popup);
			
		},
		
		// Popup anzeigen
		display: function(){
			this.popup.inject(this.options.container);
		}
		
	});
	

	AjaxPopup = new Class({

		Extends: Popup,
		Implements: [Options, Events],
		overlay: null,
		drag: null,
		minContainer: null,
		submit: null,

		// Die Titelleiste
		addTitle: function(text){
			var titleBar = new Element('div', {
				'class': 'popupTitle',
				'text': text,
				'cursor': 'move'
			});
			//this.getMinimizeButton().inject(titleBar);
			this.getCloseButton().inject(titleBar);
			titleBar.inject(this.popup, 'top');
			this.drag = new Drag(this.popup, {
				'handle': titleBar,
				onBeforeStart: function(el){
					var pos = el.getPosition();
					el.setStyles({
						'top': pos.y,
						'left': pos.x
					});
				},
				onComplete: function(el){
			        if (el.getStyle('top').toInt() < 0) el.setStyle('top', '0');
			        if (el.getStyle('left').toInt() < 0) el.setStyle('left', '0');
			    }
			});
		},

		// Minimier-Button
		getMinimizeButton: function(){
			this.minContainer = new Element('div', {
				'html': this.popup.get('html')
			});
			this.popup.set('html', '');
			this.minContainer.inject(this.popup);
			var popupSlide = new Fx.Slide(this.minContainer, {
				onComplete: function(){
					if(this.open){
						minimizeButton.set('text', '-');
					} else {
						minimizeButton.set('text', '+');
					}
				}
			});
			var minimizeButton = new Element('span', {
				'class': 'popupMinimize',
				'text': '-',
				'events': {
					'click': function(){
						popupSlide.toggle();
					}
				}
			});
			return minimizeButton;
		},
		
		// Schliessen-Button
		getCloseButton: function() {
			var closeButton = new Element('span', {
				'class': 'popupClose',
				'text': 'x'
			});
			closeButton.addEvent('click', function(){
				this.close();
			}.bind(this));
			return closeButton;
		},

		// Der Cancel-Button
		addCancelButton: function(text){
			var button = new Element('div', {
				'class': 'popupButton',
				'text': text
			});
			var container = new Element('div', {
				'class': 'popupButtonContainer'
			});
			Popup.implement(Events);
			button.addEvent('click', function(){
				this.close();
			}.bind(this));
			button.inject(container);
			glowbuttoneffect(button);
			if(this.minContainer != null){
				container.inject(this.minContainer, 'bottom');
			} else {
				container.inject(this.popup, 'bottom');
			}
		},
		
		// Der OK-Button
		addOKButton: function(text){
			var button = new Element('div', {
				'class': 'popupButtonSingle',
				'text': text
			});
			Popup.implement(Events);
			button.addEvent('click', function(){
				this.close();
			}.bind(this));
			glowbuttoneffect(button);
			if(this.minContainer != null){
				button.inject(this.minContainer, 'bottom');
			} else {
				button.inject(this.popup, 'bottom');
			}
		},
		
		// Der Submit-Button
		addSubmitButton: function(text, submit_function){
			var button = new Element('div', {
				'class': 'popupButtonSubmit',
				'text': text
			});
			var container = new Element('div', {
				'class': 'popupButtonContainer'
			});
			Popup.implement(Events);
			button.addEvent('click', submit_function);
			button.inject(container);
			glowbuttoneffect(button, '#00d000');
			if(this.minContainer != null){
				container.inject(this.minContainer, 'bottom');
			} else {
				container.inject(this.popup, 'bottom');
			}
			this.submit = container;
		},
		
		// Submit-Button unterdruecken
		removeSubmitButton : function() {
			if (this.submit != null) {
				this.submit.fade('out');
			}
		},

		// Das Overlay
		addOverlay: function(){
			this.overlay = new Element('div', {
				'class': 'popupOverlay',
				'styles': {
					'opacity': 0,
					'height': this.options.container.getSize().y
				}
			});
			this.overlay.inject(this.options.container, 'top');
			this.overlay.tween('opacity', 0.75);
		},
		
		// Fenster Schliessen
		close: function(){
			this.popup.destroy();
			if(this.overlay != null){
				this.overlay.set('tween', {
					onComplete: function(el){
						el.destroy();
					}
				});
				this.overlay.tween('opacity', 0);
			}
		},
		
		// Popup anzeigen
		display: function(request) {
			request.addEvent('success', function(responseText, responseXML) {
				this.content.set('html', responseText);
				this.popup.inject(this.options.container);
				this.popup.position();
			}.bind(this));
			request.send();
		},
		
		displayStatic: function(content) {
			this.content.set('html', content);
			this.popup.inject(this.options.container);
			this.popup.position();
		},
		
		// Neu laden
		redisplay: function(content_) {
			this.content.set('html', content_);
			this.popup.position();
		}

	});
	
});

function displayErrorPopup(text, title) {
	title = title || 'Fehler';
	var errorPopup = new AjaxPopup();
	errorPopup.addTitle(title);
	errorPopup.addOKButton('Schließen');
	errorPopup.addOverlay();
	errorPopup.displayStatic(text);
}
