﻿window.OverlayPanel = {
	insts: {},
	init: function(panelId, openOverlayOnServer, isFixed) {
		var panel = $(panelId + '_holder');
		if (!panel) return;

		if (openOverlayOnServer) panel.show()
		this.createPanel(panelId, isFixed);
	},

	createPanel: function(objID, isFixed) {
		var OverlayPanelClass = function(obj, isFixed) { this.id = obj; this.isFixed = isFixed; };
		OverlayPanelClass.prototype = this;
		this.insts[objID] = new OverlayPanelClass(objID, isFixed);
		this.insts[objID].ajust();
	},

	ajust: function() {
		if (Prototype.Browser.IE6) {
			var objHolder = $(this.id + "_holder");
			var objContentHolder = $(this.id + "_content");
			var objShadow = $(this.id + "_shadow");
			var objMate = $(this.id + "_mate");

			objHolder.style.position = "absolute";
			objMate.style.position = "absolute";
			objShadow.style.position = "absolute";

			objHolder.clonePosition($(document.body), { setWidth: true, setHeight: true, setLeft: false, setTop: false });

			objMate.clonePosition(objHolder, { setWidth: true, setHeight: true, setLeft: false, setTop: false });
			objShadow.clonePosition(objHolder, { setWidth: true, setHeight: true, setLeft: false, setTop: false });

			objMate.style["filter"] = "alpha(opacity: 10)";

			var node = objHolder.offsetParent;
			var dPos = { dx: 0, dy: 0 };
			while (node) {
				dPos.dx -= node.offsetLeft;
				dPos.dy -= node.offsetTop;
				node = node.offsetParent;
			}
			objHolder.style.left = dPos.dx + "px";
			objHolder.style.top = dPos.dy + "px";
			objShadow.style.left = "0px";
			objShadow.style.top = "0px";
		}

		this._resize();
		if (!this.resizeinit) {
			this.resizeinit = true;
			Event.observe(window, 'resize', this._resize.bind(this));
			if (this.isFixed && Prototype.Browser.IE6)
				Event.observe(window, 'scroll', this._scrollOverlay.bind(this));
		}
	},

	visibleFlashForOverlayPanel: function(visible) {
		function visibleFlash(flashObj) {
			flashObj.style.visibility = visible ? "visible" : "hidden";
		}
		$(document.body).select("object").each(visibleFlash);
		$(document.body).select("embed").each(visibleFlash);
	},

	_resize: function() {
		var container = $(this.id + '_content');

		if (container && container.getHeight() != 0) {
			var scrollTop =
				this.isFixed ? 0 : Math.max(document.documentElement.scrollTop, document.body.scrollTop);

			var viewport = document.viewport.getDimensions();
			var top = scrollTop + Math.max(parseInt((viewport.height - container.getHeight()) / 2) - 10, 50 - 20);

			if (!this.isFixed && !Prototype.Browser.IE6) {
				for (var instId in this.insts) {
					var instContainer = $(instId + '_content');
					if (instId != this.id && instContainer && container.descendantOf(instContainer)) { // overlay is in another overlay
						top -= parseInt(instContainer.style.top);
					}
				}
			}

			container.setStyle({ top: top + "px" });
		}

		if (Prototype.Browser.IE6) {
			var objHolder = $(this.id + "_holder");

			if (objHolder.getWidth() != 0) {
				var objShadow = $(this.id + "_shadow");
				var objMate = $(this.id + "_mate");

				var height = this._getDocHeight();
				if (objShadow.getHeight() == height) return;

				objShadow.clonePosition($(document.body), { setWidth: true, setHeight: false, setLeft: false, setTop: false });
				objShadow.setStyle({ 'height': height });
				objMate.setStyle({ 'height': height });
			}
		}
	},

	_scrollOverlay: function() {
		this._resize();
	},

	_getDocHeight: function() {
		var D = document;
		return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
	}
}
