
//********************************************************//
//	Popup Class Ver. 0.5
//  작성자 : 정병태
//  작성일 : 2008-04-01
//	
//********************************************************//

var Popup = Class.create({
 
	// 클래스 생성자 함수
	initialize: function(element, url, options) {
		// 비어있는 option 객체를 생성한다.
		this.options = {
			top: 0, left: 0, width: 0, height: 0, 
			overlay : false,
			parameters : "",
			onLoad: Prototype.emptyFunction, 
			onCloseBefore: Prototype.emptyFunction,
			onClose: Prototype.emptyFunction
		};
		Object.extend(this.options, options || {});
		if($(element)) this.element = $(element);
		else this.element = new Element("div",{id:element});
		$$("body")[0].insert(this.element);
		this.closeHandler = this.close.bind(this);
		this.url = url;
		this.open();
	},

	// 팝업을 생성한다.
	open : function() {

		new Ajax.Request(this.url,{
			parameters : this.options.parameters,
			onSuccess : function(request) {
				if(this.options.overlay) {
					this.overlay = new Effect.Overlay();
					this.overlay.show();
				}
				this.element.update(request.responseText);
				var arrayPageSize = getPageSize();
				var arrayPageScroll = getPageScroll();
				var width = (this.options.width ? this.options.width : this.element.down("div").offsetWidth);
				var height = (this.options.height ? this.options.height : this.element.down("div").offsetHeight);
				var toTop = (arrayPageScroll[1] + ((arrayPageSize[3] - height) / 2));
				var top = (this.options.top ? this.options.top : toTop > 0? toTop : 0);
				var left = (this.options.left ? this.options.left : (((arrayPageSize[0] - width) / 2)));
				this.element.setStyle({position:"absolute", display:"block", width:width+"px", height:height+"px",
									top:(height > 0 ? top : 0)+"px", left:(width > 0 ? left : 0)+"px", zIndex:this.options.overlay ? this.overlay.options.zIndex+1 : 2000});
				this.startDrag();
				this.element.select(".popupClose").invoke("observe", "click", this.closeHandler).invoke("setStyle",{cursor:"pointer"});
				if(typeof this.options.onLoad == "function") this.options.onLoad();
			}.bind(this),

			onFailure : function(request) {
				trace(request.responseText);
			}
		});
	},

	// 현재 팝업에 드래그 속성을 부여한다.
	startDrag : function() {
		this.draggable = new Effect.Drag(this.element);
	},

	// 현재 팝업에 드래그 속성을 삭제한다.
	stopDrag : function() {
		this.draggable.stop();
	},
	
	// 현재 팝업을 삭제한다.
	close : function() {
		if(this.options.overlay) this.overlay.hide();
		this.element.select(".popupClose").invoke("stopObserving", "click", this.closeHandler);
		if(typeof this.options.onCloseBefore == "function") this.options.onCloseBefore();
		this.element.remove();
		if(typeof this.options.onClose == "function") this.options.onClose();
	}
});