namespace("UPC.widget.modalWindow");

UPC.widget.modalWindow.Base = function () {};
UPC.widget.modalWindow.Base.prototype = {
	BaseDefaultOptions: {
		width: 400,
		height: 300,
		useAJAX: false,
		destroyOnClose: false,
		id: "ch_modal_window",
		className: "ch_modal_window",
		clickOverlayToClose: false,
		parentElement: null,
		okButton: {label: "Ok", show: true},
		cancelButton: {label: "Cancel", show: true},
		position: {},
		baseZIndex: 3000,
		//callback functions
		onShow: Prototype.emptyFunction,
		onHide: Prototype.emptyFunction,
		onClickOk: Prototype.emptyFunction,
		onClickCancel: Prototype.emptyFunction
	},
	content: "",
	contentURL: null,
	mainElement: null,
	overlay: null,
	active: false,
	baseInitialize: function (content, options) {
		this.options = Object.extend({}, this.BaseDefaultOptions);
		this.options.parentElement = document.body;
		if (typeof options == "object") {
			this.options = Object.extend(this.options, options);
		}
		if (this.options.useAJAX) {
			this.contentURL = content;
			this._loadContent();
		} else {
			this.content = content;
		}
		if (Element.getStyle(this.options.parentElement, "position") == "" || Element.getStyle(this.options.parentElement, "position") == "static") {
			Element.setStyle(this.options.parentElement, {"position": "relative"});
		}
		var ie = false /*@cc_on || true @*/;
		this.isIE6 = ie && (document.implementation != null) && (document.implementation.hasFeature != null);
		this.isIE55 = ie && (document.namespaces != null) && (!this.isIE6);  
		return true;
	},
	show: function () {
		this._disablePageScroll();
		if (this.overlay == null) {
			this._buildOverlay();			
		}
		if (this.mainElement == null) {
			this._buildWindow();
		}
		this.overlay.show();		
		this.mainElement.show();
		this._onShow();
		this.active = true;
		Event.observe(document.body, "resize", this._onResizeHandler.bindAsEventListener(this));
//		Event.observe(document.body, "keypress", this._onKeyPressHandler.bindAsEventListener(this));
		Event.observe(window, "resize", this._onResizeHandler.bindAsEventListener(this));
		Event.observe(window, "keypress", this._onKeyPressHandler.bindAsEventListener(this));		
	},
	hide: function () {
		this._enablePageScroll();
		if (this.options.destroyOnClose) {
			this._destroy();
		} else {
			this.overlay.hide();
			this.mainElement.hide();
		}
		this._onHide();
		this.active = false;
		Event.stopObserving(document.body, "resize", this._onResizeHandler.bindAsEventListener(this));
		Event.stopObserving(document.body, "keypress", this._onKeyPressHandler.bindAsEventListener(this));
		Event.stopObserving(window, "resize", this._onResizeHandler.bindAsEventListener(this));
		Event.stopObserving(window, "keypress", this._onKeyPressHandler.bindAsEventListener(this));		

	},
	_buildOverlay: function () {
		try {
			if ((this.overlay = $(this.options.id+"_overlay")) == null) {
				this.overlay = document.createElement("div");
				this.options.parentElement.insertBefore(this.overlay, this.options.parentElement.firstChild);
				this.overlay.id = this.options.id + "_overlay";
				Element.addClassName(this.overlay, this.options.className + "_overlay");
				var browser = this._getBrowserSize();
				this.overlay.setStyle({"width":(browser.width+50) + "px", 
					"height":(browser.height+50) + "px", 
					"position": "absolute", 
					"left": 0, 
					"top": 0, 
					"display": "none",
					"zIndex": this.options.baseZIndex-1 }
				);
				this.options.baseZIndex++;
				if (this.isIE6 || this.isIE55) {
					this.options.parentElement.select("select").invoke("setStyle", {"visibility":"hidden"});
				}
				if (this.options.clickOverlayToClose) {
					Event.observe(this.overlay, this._onClickCancel.bindAsEventListener(this));
				}
			}
		} catch (e) {
			this._dispatchError(e);
		}
	},
	_buildWindow: function () {
		try {
			var container = document.createElement("div");
			container.id = this.options.id + (Math.round(Math.random()*100000));
			container.className = this.options.className;
			if (this.position == null) {
				this._getPosition();
			}
			var content = document.createElement("div");
			content.id = container.id + "_content";
			content.className = this.options.className + "_content";
			content.innerHTML = this.content;
			if (this.options.okButton.show) {
				var okButton = document.createElement("a");
				okButton.id = container.id + "_ok";
				okButton.className = this.options.className + "_ok";				
				okButton.innerHTML = this.options.okButton.label;
				okButton.href = "#";
				this.okButton = content.appendChild(okButton);
				Event.observe(this.okButton, "click", this._onClickOk.bindAsEventListener(this))
			}
			if (this.options.cancelButton.show) {
				var cancelButton = document.createElement("a");
				cancelButton.id = container.id + "_cancel";
				cancelButton.className = this.options.className + "_cancel";
				cancelButton.innerHTML = this.options.cancelButton.label;
				cancelButton.href = "#";
				this.cancelButton = content.appendChild(cancelButton);
				Event.observe(this.cancelButton, "click", this._onClickCancel.bindAsEventListener(this))				
			}
			
			container.appendChild(content);

			//this.mainElement = this.options.parentElement.appendChild(container);
			this.mainElement = this.options.parentElement.insertBefore(container, this.options.parentElement.firstChild);
			var style = {"width":this.options.width+"px", 
				"height":(this.options.height=="auto"?this.options.height:(this.options.height + "px")), 
				"display": "none", 
				"position": "absolute", 
				"top":this.position.top, 
				"left":this.position.left,
				"zIndex":this.options.baseZIndex};
			Element.setStyle(container, style);	
					
			if (this.options.height=="auto") {
				var height = this.mainElement.getHeight();
				if (this.parentElement == document.body) {
					var parentSize = this._getBrowserSize();
				} else {
					var parentSize = this.options.parentElement.getDimensions();
				}				
				this.mainElement.setStyle({"top":Math.round((parentSize.height-height)/2) + "px"});
			}
		} catch (e) {
			alert(e.number + " " + e.message);
		//	this._dispatchError(e);
		}
	},
	_destroy: function () {
		if (this.options.okButton.show) {
			Event.stopObserving(this.okButton, "click", this._onClickOk.bindAsEventListener(this));
			delete this.okButton;
		}
		if (this.options.cancelButton.show) {
			Event.stopObserving(this.cancelButton, "click", this._onClickCancel.bindAsEventListener(this));
			delete this.cancelButton;
		}
		if (this.isIE6 || this.isIE55) {
			this.options.parentElement.select("select").invoke("setStyle", {"visibility":"visible"});
		}
		this.options.parentElement.removeChild(this.mainElement);
		this.options.parentElement.removeChild(this.overlay);
		
		delete this.mainElement;
		delete this.overlay;
	},
	_disablePageScroll: function () {
		this._pageScrollStyle = {"overflow": document.body.getStyle("overflow"),
		"height": document.body.getStyle("height"),
		"width": document.body.getStyle("width")};
		var browser = this._getBrowserSize();
		var pageScrollStyle = {"overflow":"hidden",
		"height":browser.height + "px",
		"width":browser.width + "px"};
		document.body.setStyle(pageScrollStyle);
	},
	_enablePageScroll: function () {
		if (typeof this._pageScrollStyle != "undefined") {
			document.body.setStyle(this._pageScrollStyle);
			$$("html").first().setStyle({"overflow":"auto"});
		}
	},
	_fixOverlayIE: function () {
		if (navigator.appVersion.indexOf("MSIE") > 0 && navigator.userAgent.indexOf("Opera") < 0) {
			var iframe = document.createElement("iframe");
			iframe.src = "about:blank";
			iframe.frameBorder = 0;
			iframe.scrolling = "no";
			var browser = this._getBrowserSize();
			iframe.setStyle({"width":(browser.width+50) + "px", 
				"height":(browser.height+50) + "px", 
				"position": "absolute", 
				"left": 0, 
				"top": 0, 
				"display": "none",
				"zIndex": this.options.baseZIndex }
			);
			this.options.baseZIndex++;

			//this.iframe = document.appendChild(iframe);
			this.iframe = this.options.parentElement.insertBefore(iframe, this.options.parentElement.firstChild);
		}
	},
	_getBrowserSize: function () {
		if (typeof window.innerHeight != "undefined") {
			return {"width": window.innerWidth, "height": window.innerHeight};
		} else if (typeof document.documentElement != "undefined") {
			return {"width": document.documentElement.clientWidth, "height": document.documentElement.clientHeight}
		} else {
			return {"width": document.body.clientWidth, "height": document.body.clientHeight}
		}
		
	},
	_loadContent: function () {
		new Ajax.Request(this.contentURL, {
			"onSuccess": this._processAjaxContent.bind(this),
			"onFailure": this._dispatchError.bind(this)
		})
	},
	_processAjaxContent: function (transport) {
		this.content = transport.responseText;
		this.show();
	},
	_onShow: function () {
		if (this.options.onShow != Prototype.emptyFunction) {
			this.options.onShow();
		}
	},
	_onHide: function () {
		if (this.options.onHide != Prototype.emptyFunction) {
			this.options.onHide();
		}
	},
	_onClickOk: function () {
		if (this.options.onClickOk != Prototype.emptyFunction) {
			this.options.onClickOk();
		}
		this.hide();
	},
	_onClickCancel: function () {
		if (this.options.onClickCancel != Prototype.emptyFunction) {
			this.options.onClickCancel();
		}
		this.hide();
	},
	_onResizeHandler: function (e) {
		var browser = this._getBrowserSize();
		if (this.overlay != null) {
			Element.setStyle(this.overlay, {"width":browser.width + "px", 
				"height":browser.height + "px"});
		}
		if (this.mainElement != null) {
			this._getPosition();
			Element.setStyle(this.mainElement, {"top":this.position.top, 
				"left":this.position.left			
			});
		}
	},
	_onKeyPressHandler: function (e) {
		if (this.active) {
			keyPressed = e.keyCode || e.which;
			if (keyPressed == Event.KEY_ESC && this.options.cancelButton.show) {
				this._onClickCancel();
			}
			if (keyPressed == Event.KEY_RETURN && this.options.okButton.show) {
				this._onClickOk();
				//alert("bla");
			}
		}
	},
	_getPosition: function () {
		if (this.position == null) {
			this.position = {};
		}
		// Get center position, based on width and height of element
		if (this.parentElement == document.body) {
			var parentSize = this._getBrowserSize();
		} else {
			var parentSize = this.options.parentElement.getDimensions();
		}
		var posLeft, posTop;
		if (typeof this.options.position.left != "undefined") {
			posLeft = this.options.position.left;
		} else {
			posLeft = Math.round((parentSize.width-this.options.width)/2) + "px";
		}
		if (typeof this.options.position.top != "undefined") {
			posTop = this.options.position.top;
		} else {
			if (this.options.height == "auto") {
				posTop = Math.round(parentSize.height/4) + "px";
			} else {
				posTop = Math.round((parentSize.height-this.options.height)/2) + "px";
			}
		}
		this.position = Object.extend(this.position, {"left": posLeft,
			"top":  posTop});
		return true;
	},
	_dispatchError: function (e) {
		//alert(e.line);
	}
}

UPC.widget.modalWindow.Confirm = Class.create();
UPC.widget.modalWindow.Confirm.prototype = Object.extend(new UPC.widget.modalWindow.Base(), {
	initialize: function (content, options) {
		this.baseInitialize(content, options);	
		return this;	
	}	
});

UPC.widget.modalWindow.Alert = Class.create();
UPC.widget.modalWindow.Alert.prototype = Object.extend(new UPC.widget.modalWindow.Base(), {
	initialize: function (content, options) {
		if (typeof options == "undefined") options = {};
		options.cancelButton = {label:"", show:false};
		this.baseInitialize(content, options);
		return this;		
	},
	_onKeyPressHandler: function (e) {
		if (this.active) {
			keyPressed = e.keyCode || e.which;
			if (keyPressed == Event.KEY_ESC || keyPressed == Event.KEY_RETURN) {
				this._onClickOk();
			}
		}
	}

});
