var Overlay = 
{
	_private_functions:
	{
		_addEventListener: function(elem, event, func, capture)
		{
			if (elem.addEventListener)
			{
				elem.addEventListener(event, func, capture);
			}
			else if (elem.attachEvent)
			{
				elem.attachEvent("on" + event, func);
			}
		},

		_getBaseDiv: function()
		{
			var div = document.getElementById(this._name);

			if (div == null)
			{
				div = document.createElement("DIV");
				Overlay._private_functions._setClass(div, "overlay_main");
				div.setAttribute("ID", this._name);
				document.body.appendChild(div);
			}
			return div;
		},

		_setClass: function (elem, iClass)
		{
			elem.setAttribute("class", iClass);
			elem.setAttribute("className", iClass);
		},

		_init: function()
		{
			if (this._ran_init)
				return;

			var base_div = this._getBaseDiv();

			this._original_body_class = document.body.className;

			this.hide();

			while (base_div.firstChild != null)
			{
				base_div.removeChild(base_div.firstChild);
			}

			var transparent = document.createElement("DIV");
			Overlay._private_functions._setClass(transparent, "transparent");
			base_div.appendChild(transparent);

			var outer = document.createElement("DIV");
			var middle = document.createElement("DIV");
			var inner = document.createElement("DIV");

			outer.setAttribute("style", 'display: table; height: 100%; #position: relative; margin: auto; overflow: hidden;');
			middle.setAttribute("style", '#position: absolute; #top: 50%; display: table-cell; vertical-align: middle;');
			inner.setAttribute("style", '#position: relative; #top: -50%');

			outer.appendChild(middle);
			middle.appendChild(inner);

			var content = document.createElement("DIV");
			Overlay._private_functions._setClass(content, "content");
			content.style.width = this._contentWidth + "px";
			content.style.height = this._contentHeight + "px";
			content.style.overflow = ( this._contentScrolling ? "visible" : "hidden" );

			inner.appendChild(content);

			var b = this;
			var f = function () { Overlay.closeWindow(b); return false; };
			
			if (!this._closeDisabled)
			{
				var close = document.createElement("SPAN");
				Overlay._private_functions._setClass(close, (this._contentScrolling ? "close_button_scrolling" : "close_button_noscrolling" ) );

				var anchor = document.createElement("A");
				anchor.href="#";
				anchor.innerHTML = "X";
				Overlay._private_functions._addEventListener(anchor, "click", f, false);
				close.appendChild(anchor);
			}

			if (!this._closeDisabled)
				content.appendChild(close);

			base_div.appendChild(outer);

			if (this._type == 'iframe')
			{
				var frame = document.createElement("IFRAME");

				frame.src = this._arg;
				frame.setAttribute("frameborder", "0");
				frame.setAttribute("scrolling", ( this._contentScrolling ? "auto" : "no" ) );
				frame.style.overflow = ( this._contentScrolling ? "visible" : "hidden" );
				content.appendChild(frame);
			}
			else if (this._type == 'function')
			{
				var div = document.createElement("DIV");
				div.style.overflow = ( this._contentScrolling ? "visible" : "hidden" );
				if (typeof(this._arg) == 'function')
				{
					this._arg(div);
				}
				content.appendChild(div);
			}
			else
			{
				// not implemented
			}

			this._ran_init = true;

		},

		_hide: function()
		{
			var base_div = this._getBaseDiv();

			base_div.style.display = "none";
			//Overlay._private_functions._setClass(document.body, this._original_body_class);
		},

		_show: function()
		{
			if (!this._ran_init)
				this._init();

			var base_div = this._getBaseDiv();
			base_div.style.display = "";
			//Overlay._private_functions._setClass(document.body, "overlay-body");
		}
	
	},

	inst: function(name, type, arg)
	{
		this._name = name;
		this._type = type;
		this._arg = arg;

		this._contentWidth = 200;
		this._contentHeight = 200;
		this._contentScrolling = false;
		this._closeDisabled = false;

		this._init = Overlay._private_functions._init;
		this._getBaseDiv = Overlay._private_functions._getBaseDiv;

		this.show = Overlay._private_functions._show;
		this.hide = Overlay._private_functions._hide;

		this.setWidth = function (width) { this._contentWidth = width + 0; this._ran_init = false; };
		this.setHeight = function (height) { this._contentHeight = height + 0; this._ran_init = false; };
		this.setScrolling = function (scrolling) { if (scrolling) this._contentScrolling = true; else this._contentScrolling = false; this._ran_init = false };
		this.setArgument = function (arg) { this._arg = arg; };
		this.disableClose = function () { this._closeDisabled = true; };
		this.enableClose = function () { this._closeDisabled = false; };

		this._ran_init = false;

		this.load = function (force) { if (force == undefined) force = false; if (force) this._ran_init = false; this._init(); return; };

		return this;
	},

	closeWindow: function(ajax)
	{
		ajax.hide();
	}
};


