(function () {
	var $ = function (id) { return document.getElementById(id); };
	
	var panel = $($('frame_panel_client_id').value);
	var frame = $('transitionFrame');
	var preloader = $('iframe_preload_transitionFrame');
	
	frame.style.display = 'none';
	
	window.openPopupFrame = function (evt, linkId, title, callBackName, src, width, height, x, y) {
		callBackName = (callBackName || '').match(/^(?:undefined)?$/i) ? 'actionPerformedCommonJsCallback' : callBackName;
		src = (src || $(linkId).href) + '&jsCallback=' + callBackName + '&rnd=' + Math.random();

		(function () {
			if (title && title != '') {
				$('popupTitle').innerHTML = title;
			}

			preloader.style.display = '';

			frame.style.height = '0';

			var style = panel.style;

			style.display = 'block';
			style.visibility = 'visible';

			if ('undefined' == typeof(NI) || !NI.ViewportGeometry) {
				throw new Error('NI.ViewportGeometry not found');
			} else {
				style.top = (function () {
					return (null != y) ? y : (NI.ViewportGeometry.getY() + 150);
				})() + 'px';

				style.left = (function () {
					if (null != x) {
						return x;
					} else if (null != width) {
						return NI.ViewportGeometry.getX() + (NI.ViewportGeometry.getWidth() - width) / 2;
					} else {
						return NI.ViewportGeometry.getX() + NI.ViewportGeometry.getWidth() / 4;
					}
				})() + 'px';

				style.width = ((width != null) ? width : (NI.ViewportGeometry.getWidth() / 2)) + 'px';
				style.height = ((height != null) ? height : (NI.ViewportGeometry.getHeight() / 2)) + 'px';
			}
		})();

		frame.onloadCallback = function () {
			preloader.style.display = 'none';

			this.style.height = panel.style.height;

			var style = this.style;
			style.display = 'block';
			style.visibility = 'visible';

			this.onloadCallback = null;
		};

		window.setTimeout(
			function () { frame.src = src; }, 
			100
		);
	};

	window.actionPerformedCommonJsCallback = function (doPostback, callbackArgs) {
		var style = panel.style;
		style.display = 'none';
		style.visibility = 'hidden';

		frame.src = 'blank.html';

		if (doPostback && window.popupFrameRefresh instanceof Function) {
			window.popupFrameRefresh();
		}
	};

})();