
	window.hpPopupTools = Singleton({
		
		setPosition: function(popup, diff, center) {
			popup = $(popup);
			if (self.pageYOffset) {
				offset = self.pageYOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {
				offset = document.documentElement.scrollTop;
			} else if (document.body) {
				offset = document.body.scrollTop;
			}
			if (center) {
				var docElem, winHeight = 0;
				if (self.innerHeight) {
					winHeight = self.innerHeight;
				} else if ((docElem = document.documentElement) && docElem.clientHeight) {
					winHeight = docElem.clientHeight;
				} else if (document.body) {
					winHeight = document.body.clientHeight;
				}
				diff = Math.round((winHeight-Geometry.dimension(popup)[1])/2);
			} else {
			if (typeof diff == "undefined") diff = 100;
			}
			popup.style.top = offset+diff+"px"
		},
		
		getViewportY: function() {
			var docElem;
			if (self.innerHeight) {
				return [window.pageYOffset, window.pageYOffset+self.innerHeight];
			} else if ((docElem = document.documentElement) && docElem.clientHeight) {
				return [docElem.scrollTop, docElem.scrollTop+docElem.clientHeight];
			} else if (document.body) {
				return [document.body.scrollTop, document.body.scrollTop+document.body.clientHeight];
			}
		},
		
		fadeScreen: function(show, _params) {
			var params = {
				opacity: 0.8,
				zIndex: 40,
				color: "#abc600",
				minHeight: 0
			};
			extend(params, _params || {});
			if (!this.fader) {
				this.fader = document.createElement("div");
				Style.set(this.fader, {
					position: "absolute",
					top: 0,
					left: 0,
					overflow: "hidden",
					display: "none"
				});
				document.body.insertBefore(this.fader, document.body.firstChild);
			}
			if (show) {
				var dim, docElem, winHeight = 0;
				if (self.innerHeight) {
					winHeight = self.innerHeight;
				} else if ((docElem = document.documentElement) && docElem.clientHeight) {
					winHeight = docElem.clientHeight;
				} else if (document.body) {
					winHeight = document.body.clientHeight;
				}
				var oldHeight = document.body.style.height;
				document.body.style.height = "auto";
				if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
					dim = [document.body.scrollWidth+"px", Math.max(document.body.scrollHeight, params.minHeight, winHeight)+"px"];
				} else if( document.body.offsetWidth ) {
					dim = [document.body.offsetWidth+"px", Math.max(document.body.offsetHeight, params.minHeight, winHeight)+"px"];
				} else {
					dim = ["100%", "100%"];
				}
				document.body.style.height = oldHeight;
				DatePicker && DatePicker.actual && DatePicker.actual.close();
				Style.set(this.fader, {
					opacity: params.opacity,
					zIndex: params.zIndex,
					backgroundColor: params.color,
					width: dim[0],
					height: dim[1]
				});
				if (IE) {
					forEach($$("select"), function(sel) {
						sel.style.visibility = "hidden";
					});
				}
				Style.show(this.fader);
			} else {
				Style.hide(this.fader);
				if (IE) {
					forEach($$("select"), function(sel) {
						sel.style.visibility = "visible";
					});
				}
			}
		},
		
		tabChain: function() {
			var chain = arguments;
			for (var i=0; i<chain.length-1; i++) {
				Event.observe(chain[i], "keydown", bindAsEventListener(function(event, index) {
					if (event.keyCode == Event.KEY_TAB) {
						try {
						if (event.shiftKey) {
							if (index) $(chain[index-1]).focus();
						} else {
							$(chain[index+1]).focus();
						}
						} catch (e) {}
						Event.stop(event);
					}
				}, this, i));
			}
		},
		
		Confirm: Class({
			
			constructor: function(title, msg, onOk, onCancel) {
				this.onOk = onOk;
				this.onCancel = onCancel || nullFunc;
				$("hp-confirm-title").innerHTML = title;
				$("hp-confirm-msg").innerHTML = msg;
				this._cancel = Event.bindStop(this.cancel, this);
				Event.observe("hp-confirm-close", "click", this._cancel);
				Event.observe("hp-confirm-cancel", "click", this._cancel);
				this._ok = Event.bindStop(this.ok, this);
				Event.observe("hp-confirm-form", "submit", this._ok);
				this._onkey = Event.bindStop(this.onkey, this);
				Event.observe(document, "keypress", this._onkey);
				hpPopupTools.fadeScreen(true);
				var elem = $("hp-confirm");
				Style.set(elem, {
					visibility: "hidden",
					top: 0
				});
				Style.show(elem);
				hpPopupTools.setPosition(elem, 0, true);
				elem.style.visibility = "visible";
				this.shadow = new hpPopupTools.Shadow("hp-confirm-popup");
			},
			
			onkey: function(e) {
				if (e.keyCode == Event.KEY_RETURN) {
					this.ok();
				} else if (e.keyCode == Event.KEY_ESC) {
					this.cancel();
				}
			},
			
			cancel: function() {
				this.remove();
				this.onCancel();
			},
			
			ok: function() {
				this.remove();
				this.onOk();
			},
			
			remove: function() {
				this.shadow.remove();
				Style.hide("hp-confirm");
				hpPopupTools.fadeScreen(false);
				Event.stopObserving("hp-confirm-close", "click", this._cancel);
				Event.stopObserving("hp-confirm-cancel", "click", this._cancel);
				Event.stopObserving("hp-confirm-form", "submit", this._ok);
				Event.stopObserving(document, "keypress", this._onkey);
			}
			
		}),
		
		Shadow: Class({
			
			constructor: function(elem, params) {
				params = params || {};
				this.params = {
					offsetX: 4,
					offsetY: 4,
					opacity: 0.25,
					color: "#9d9d9d",
					zIndex: 55,
					img: "/homepage/img/hp-shadows.png"
				};
				if ((params.color == "#c9db58") && !params.img) {
					params.img = "/homepage/img/hp-shadow-green.png";
				}
				params = extend(this.params, params);
				var
					dim = Geometry.dimension(this.elem = elem = $(elem)),
					pos = Geometry.position(elem),
					html = [];
				Style.hide(this.div = document.createElement("div"));
				document.body.insertBefore(this.div, document.body.firstChild);
				Style.set(this.div, {
					opacity: params.opacity,
					zIndex: params.zIndex
				});
				(this.inner = document.createElement("div")).style.position = "relative";
				this.div.appendChild(this.inner);
				if (dim[0] > 20) html.push(
					'<div style="height:20px;width:', dim[0]-20, 'px;position:absolute;background:', params.color, ';right:20px;bottom:0"></div>'
				);
				if (dim[1] > 20) html.push(
					'<div style="width:', dim[0],
					'px;height:', dim[1]-20, 'px;position:absolute;background:', params.color, ';bottom:20px;right:0"></div>'
				);
				html.push('<div style="width:', Math.min(dim[0], 20), 'px;height:', Math.min(dim[0], 20), 'px;position:absolute;bottom:0;right:0;');
				if (IE) {
					html.push('filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'', params.img, '\', sizingmethod=\'crop\')');
				} else {
					html.push('background:url(', params.img, ') 100% 100%;');
				}
				html.push('"></div>');
				this.inner.innerHTML = html.join("");
				Style.set(this.inner, {
					height: dim[1]+"px",
					width: dim[0]+"px"
				});
				Style.set(this.div, {
					position: "absolute",
					display: "block",
					left: pos[0]+params.offsetX+"px",
					top: pos[1]+params.offsetY+"px"
				});
			},
			
			remove: function() {
				this.div.removeChild(this.inner);
				document.body.removeChild(this.div);
				delete this.inner;
				delete this.div;
				this.remove = nullFunc;
			}
			
		})
		
	});
