if (!top.modalwin) {
	top.modalwin = {
		_shown: false,
		_loaded: false,
		_initted: false,

		_get_bg: function() {
			return self.document.getElementById('modalwin') || null;
		},

		_get_popup: function() {
			return self.document.getElementById('modalwin_popup') || null;
		},

		_get_container: function() {
			return self.document.getElementById('modalwin_container') || null;
		},

		_get_ifr: function() {
			return self.document.getElementById('modalwin_ifr') || null;
		},

		_reposition: function(e, force) {
			if (!this._shown && !force) return;
			e = e || this._get_bg() || null;
			if (e && e.style) {
				e.style.display = (e.style.display == 'block') ? '' : 'block';
			}
			e = this._get_popup() || null;
			if (e && e.style) {
				e.style.display = (e.style.display == 'block') ? '' : 'block';
			}
		},

		_resize: function(e, force, reset) {
			if (!this._shown && !force) return;
			e = e || this._get_bg() || null;
			if (!e) return;
			if (e.style) {
				e.style.left = '0px';
				e.style.top = '0px';
				// reset to viewport
				e.style.width = String(parseInt(self.document.body.clientWidth) || 0) + 'px';
				e.style.height = String(parseInt(self.document.body.clientHeight) || 0) + 'px';
				if (!reset) {
					var isIE = (self.document.uniqueID && self.createPopup) ? true : false;
					// calculate max dimensions
					e.style.width = String(top.util.arrayMax([
						//function() { return self.innerWidth; },
						function() { return self.document.innerWidth; },
						function() { return self.document.body.innerWidth; },
						function() { return self.document.clientWidth; },
						function() { return self.document.body.clientWidth; },
						function() { return self.document.scrollWidth; },
						function() { return self.document.body.scrollWidth; },
						function() { return self.document.documentElement.clientWidth; },
						function() { return self.document.documentElement.innerWidth; },
						//function() { return isIE ? null : self.document.documentElement.scrollWidth; },
						null
					])) + 'px';
					e.style.height = String(top.util.arrayMax([
						//function() { return self.innerHeight; },
						function() { return self.document.innerHeight; },
						function() { return self.document.body.innerHeight; },
						function() { return self.document.clientHeight; },
						function() { return self.document.body.clientHeight; },
						function() { return self.document.scrollHeight; },
						function() { return self.document.body.scrollHeight; },
						function() { return self.document.documentElement.clientHeight; },
						function() { return self.document.documentElement.innerHeight; },
						//function() { return isIE ? null : self.document.documentElement.scrollHeight; },
						null
					])) + 'px';
				}
			}
		},

		show: function(url, w, h) {
			var e = null;
			w = parseInt(w) || 0;
			h = parseInt(h) || 0;
			if (w > 0 || h > 0) {
				e = this._get_container();
				if (e && e.style) {
					var isIE = (self.document.uniqueID && self.createPopup) ? true : false;
					if (w > 0) {
						e.style.width = String(w) + 'px';
						if (!isIE) e.style.marginLeft = '-' + String(Math.round(w / 2)) + 'px';
					}
					if (h > 0) {
						e.style.height = String(h) + 'px';
						if (!isIE) e.style.marginTop = '-' + String(Math.round(h / 2)) + 'px';
					}
				}
			}
			e = this._get_bg() || null;
			if (!e) return;
			e.contentWindow.document.open();
			e.contentWindow.document.writeln('<html><body bgcolor="#000000"></body></html>');
			e.contentWindow.document.close();
			if (e.style) {
				e.style.display = 'block';
				this._resize(e, true);
			}
			e = this._get_popup() || null;
			if (e && e.style) {
				e.style.display = 'block';
			}
			var ifr = this._get_ifr() || null;
			if (ifr) {
				if (url) {
					setTimeout(function() {
						ifr.src = String(url);
					}, 1);
				}
				try {
					if (ifr.focus) {
						ifr.focus();
					}
					if (ifr.contentWindow && ifr.contentWindow.focus) {
						ifr.contentWindow.focus();
					}
				}
				catch (ex) { }
			}
			this._shown = true;
		},

		hide: function() {
			this._shown = false;
			var e = this._get_bg() || null;
			if (e && e.style) {
				e.style.display = 'none';
			}
			e = this._get_popup() || null;
			if (e && e.style) {
				e.style.display = 'none';
			}
			this._resize(e, null, true);
			var ifr = this._get_ifr() || null;
			if (ifr) {
				this._loaded = true;
				try { ifr.src = 'about:blank'; }
				catch (ex) { }
			}
			try { top.focus(); }
			catch (ex) { }
		},

		_onload: function(ifr) {
			if (this._loaded) return;
			this._loaded = true;
			this._resize(null, true);
			this.show(null);
			this._reposition(null, true);
		},

		show_onload: function(url) {
			var ifr = this._get_ifr() || null;
			if (!ifr) return;
			if (!this._initted) {
				this._initted = true;
				var _t = this;
				top.util.addListener(ifr, 'load', function() {
					_t._onload(this);
				});
			}
			if (url) {
				this._loaded = false;
				ifr.src = String(url);
			}
		}
	};

	if (top.util) {
		top.util.addListener(top, 'resize', function() {
			top.modalwin._resize();
		});
		top.util.addListener(top, 'scroll', function() {
			top.modalwin._reposition();
		});
	}
}
