Logi.UI.Popup = new Class ({

	__construct : function (content, options) {
		options = options || {};
        this.cssClass = options.cssClass || 'logi_popup';
        this.container = Logi.DOM.Element.get (options.container) || document.body;

		var popup = document.createElement ('DIV');
		//popup.id = popupId;
		popup.className = this.cssClass;
		popup.style.display = 'none';
		if (options.layout == 'fixed' || options.layout == 'absolute') {
			popup.style.position = options.layout;
		}
		
		if (options.position == 'center' || options.position == 'horizontal') {
			/*popup.style.left = '50%';
			popup.style.marginLeft = (-(options.width / 2 || 0)) + 'px';*/			
			
			popup.style.left = Math.max ((this.container.offsetWidth - (options.width || 0)) / 2, 0) + 'px';
			
		} else {
			popup.style.left = (options.left || 0) + 'px';
		}
		if (options.position == 'center' || options.position == 'vertical') {
			if (popup.style.position == 'fixed') {
				popup.style.top = '50%';
				popup.style.marginTop = (-(options.height / 2 || 0)) + 'px';
			} else {			
				popup.style.top = Math.max ((this.container.offsetHeight - (options.height || 0)) / 2, 0) + 'px';
			}
		} else {
			popup.style.top = (options.top || 0) + 'px';
		}		
		
		if (options.width) popup.style.width = options.width + 'px';
		if (options.height) popup.style.height = options.height + 'px';
		
        //this.container.insertBefore (popup, this.container.firstChild);
        this.container.appendChild (popup);
        this.dom = popup;
        this.isShown = false;
        this.setContent (content);
        
		if (options.resizable) {
			this.resizer = new Logi.UI.DragNDrop.ResizerFactory (popup, this.cssClass, options.resizable);
		}
		        
        this.fadeEffect = options.fade ? parseFloat (options.fade) : false;
        if (isNaN (this.fadeEffect)) this.fadeEffect = 1;
	},
	
	setContent : function (content) {
		Logi.DOM.Element.setContent (this.dom, content);
	},

	show : function (callback) {
		callback = typeof callback == 'function' ? callback : Logi.emptyFunction;
		
		if (this.isShown) return;
		this.dom.style.zIndex = Logi.UI.LayerManager.requestLayer();
		this.dom.style.display = 'block';
		this.isShown = true;
		if (this.fadeEffect) {
			var f = new Logi.UI.Effects.Iterator (new Logi.UI.Effects.Fade (this.dom, {startOpacity:0,endOpacity:1}));
			f.run (callback);
		} else {
			callback (this);
		}
	},
	
	hide : function (callback) {
		callback = typeof callback == 'function' ? callback : Logi.emptyFunction;

		if (!this.isShown) return;
		
		var whenDone = Delegate (this, function() {
			this.isShown = false;
			this.dom.style.display = 'none';
			Logi.UI.LayerManager.disposeLayer();
			callback (this);
		});
		
		if (this.fadeEffect) {
			var f = new Logi.UI.Effects.Iterator (new Logi.UI.Effects.Fade (this.dom, {startOpacity:1,endOpacity:0}));
			f.run (whenDone);
		} else {
			whenDone();
		}
	},
	destroy : function() {
		this.dom.parentNode.removeChild (this.dom);
	}
});

