(function() {
	
	Logi.UI.PopupWindow = new Class ({
		
	//	__construct : function (windowClassName, titleClassName, windowContainer) {
	
		__construct : function (title, content, options) {		
			this.options = options = options || {};
			titleCssClass = options.titleBarCssClass || Logi.UI.PopupWindow.SETUP.titleBarCssClass;
			options.escTextCssClass = options.escTextCssClass || Logi.UI.PopupWindow.SETUP.escTextCssClass;

			if (options.fogClose !== false && options.fogClose !== true) {
				options.fogClose = Logi.UI.PopupWindow.SETUP.fogClose;
			}

			var dopt = Logi.UI.PopupWindow.SETUP;
			for (var opt in dopt) {
				options[opt] = options[opt] || dopt[opt]; 
			}  
			
			this.titleBar = Logi.DOM.Element.create ('div', {
				className:titleCssClass
			});
			this.setTitle (title);
			
			if (options.onClose) {
				var closeButton = Logi.DOM.Element.create ('div', {
					className:options.closeButtonCssClass || 'logi_popupwindow_closebutton'
				}, 'x');
				
				var handler = options.onClose === true ? Logi.UI.PopupWindowManager.close : options.onClose;
				this.handler = handler;
				var win = this;
				
				Logi.DOM.Element.compatNode (closeButton).addEventListener ('click', handler, true);
				this.keyPressFunc = function (event) {
					if (popups[popups.length - 1] == win && event.keyCode == 27) {
						handler(); 
					}
				};
				Logi.DOM.Element.compatNode (document).addEventListener ('keydown', this.keyPressFunc, true);
				this.titleBar.insertBefore (closeButton, this.titleBar.firstChild);
			}
			
			this.contentBar = Logi.DOM.Element.create ('div', {
				className:options.contentBarCssClass || 'logi_popupwindow_contentbar'
			});
			if (options.contentHeight) {
				this.contentBar.style.height = options.contentHeight + 'px';
			}
			
			this.setContent (content);
			
			this.popup = new Logi.UI.Popup ([this.titleBar, this.contentBar], options);
			this.windowCssClass = options.windowCssClass || 'logi_popupwindow';		
			Logi.DOM.Element.addClass (this.popup.dom, this.windowCssClass);
	        this.dragger = new Logi.UI.DragNDrop.Listener (this.windowCssClass, titleCssClass);		
	        if (options.roundedBoxManager) {
	        	var mgr = options.roundedBoxManager === true ?
	        		new Logi.UI.PopupWindow.RoundedBox() : options.roundedBoxManager;
	        	mgr.applyTo (this.popup.dom);
	        }
	    },    
	    show : function (callback) {
	    	document.documentElement.scrollTop = 0;
	    	this.popup.show (Delegate (this, function() {
				if (this.options.onClose && this.options.fogClose) {
					Logi.UI.LayerManager.handleLayerClick (this.popup.dom.style.zIndex, this.handler);
				}
	    		if (typeof callback == 'function') callback();
	    	}));
	    },    
	    hide : function (callback) {
	    	this.popup.hide (callback);
	    },    
	    setContent : function (content) {
	    	Logi.DOM.Element.setContent (this.contentBar, content);
	    },    
	    setTitle : function (content) {
	    	Logi.DOM.Element.setContent (this.titleBar, content);
	    },
	    destroy : function() {
	    	this.popup.destroy();
	    }
	
	});

	Logi.UI.PopupWindow.SETUP = {
		fogClose : false,
		roundedBoxManager : null,
		titleBarCssClass : 'logi_popupwindow_titlebar',
		closeButtonCssClass : 'logi_popupwindow_closebutton',
		contentBarCssClass : 'logi_popupwindow_contentbar',
		escTextCssClass : 'logi_popupwindow_esctext'
	};

	Logi.UI.PopupWindow.RoundedBox = new Class ({
		__construct : function (baseCssClass) {
			this.baseCssClass = baseCssClass || 'rounded_box';
		},
		applyTo : function (container) {
			var bcss = this.baseCssClass;
			//Logi.DOM.Element.addClass (container, bcss);
			var directions = ['bottom', 'top', 'right', 'left', 
				'bottom_right', 'bottom_left', 'top_right', 'top_left'];
			for (var i = 0; i < directions.length; i++) {
				Logi.DOM.Element.create ('div', {
					className : bcss + ' ' + bcss + '_' + directions[i]
				}, ' ', container);
			}
		}
		
	});
	
	var popups = [];
	Logi.UI.PopupWindowManager = {		
		hasAny : function() {
			return popups.length > 0;
		},
		open : function (title, content, options, onOpen) {
			var popup = new Logi.UI.PopupWindow (title, content, options);			
			popups.push (popup);
			popup.show (onOpen);
			return popup;
		},
		close : function (onClose) {
			var popup = popups.pop();
			if (!popup) {
				throw "There is no popup in the stack";
			}
			popup.hide (function() {
				popup.destroy(); popup = null;
				if (typeof onClose == 'function') {
					onClose();
				}
			});
		},
		getTopPopup : function() {
			return popups[popups.length - 1];
		},
		setTopTitle : function (content) {
			popups[popups.length - 1].setTitle (content);
		}
	};
	
})();

