﻿// <script language='javascript'>

function Dialog_OpenDialog(host, url, title, args, w96dpi, h96dpi, scroll, resize, postback, id)
{
	var dialogInfo = { postback: postback, id: id };
	self.closeDialogArgs = dialogInfo;

	var supportsScaling = typeof (screen.deviceXDPI) == 'number';
	var w = w96dpi * (supportsScaling ? (screen.deviceXDPI / 96.0) : 1.0);
	var h = h96dpi * (supportsScaling ? (screen.deviceYDPI / 96.0) : 1.0);

	var container = $("#vc3-dialog-container");

	// Cleanup any prior dialog.  Removing it during close event
	// causes problems in IE due to non-dialog event handlers trying to access the
	// removed iframe.
	if (container.length != 0)
		container.remove();

	$("body").append(
		"<div id='vc3-dialog-container' style='width: " + w + "px; height: " + h + "px; overflow: hidden; padding: 0px;'>" +
		"<iframe id='vc3-dialog-frame' name='vc3-dialog-frame' frameborder='0' hspace='0' style='width: " + w + "px; height: " + h + "px; margin: 0px;'></iframe>" +
		"</div>");

	container = $("#vc3-dialog-container");
	dialogInfo.container = container;

	var frame = $("#vc3-dialog-frame");

	var calcSizes = function()
	{
		var parent = container.parent();

		var titleBar = $('.ui-dialog-titlebar');
		var resizer = titleBar.offset().top - parent.offset().top;
		var hFiller = (2 * resizer) + titleBar.outerHeight(true);

		return {
			w: parent.outerWidth(false) - (2 * resizer),
			h: parent.outerHeight(false) - hFiller,
			hFiller: hFiller
		}
	}
	
	var doAutoResize = function()
	{
		// only resize once
		frame.unbind('load', arguments.callee);
		
		var doc = frame.get(0).contentWindow.document;

		// auto resize
		var b = doc.body
		var dx = Math.max(b.clientWidth, b.scrollWidth) - b.clientWidth;

		if (dx > 0)
		{
			container.dialog('option', 'width', container.dialog('option', 'width') + dx);
			container.width(container.width() + dx);
			frame.width(frame.width() + dx);
		}

		var dy = Math.max(b.clientHeight, b.scrollHeight) - b.clientHeight;

		if (dy > 0)
		{
			if ($.browser.msie)
			{
				var dialogSize = calcSizes();
				container.dialog('option', 'height', container.dialog('option', 'height') + dy + dialogSize.hFiller);
			}
			else
			{
				container.dialog('option', 'height', container.dialog('option', 'height') + dy);
			}

			container.height(container.height() + dy);
			frame.height(frame.height() + dy);
		}
	};
	
	
	// iframe overlay to prevent iframe from messing up drag and resize events
	var startFrameOverlay = function()
	{
		// block drag events from iframe during resizing.
		// Div must not be completely transparent for this to work in IE.
		$('<div id="vc3-dialog-frameoverlay" style="position: absolute; z-index: 100000; background: white;"/>')
			.fadeTo(0, 0.10)
			.appendTo("body");
			
		updateFrameOverlay();
	};

	var updateFrameOverlay = function()
	{
		var target = container.parent();
		var offset = target.offset();

		$("#vc3-dialog-frameoverlay").css({
			top: offset.top,
			left: offset.left,
			width: target.width(),
			height: target.height()
		})
	}

	var stopFrameOverlay = function()
	{
		$("#vc3-dialog-frameoverlay").remove();
	}



	var resizeIFrame = function(parentToo)
	{
		var size = calcSizes();

		frame.width(size.w).height(size.h);
	}

	frame.load(function()
	{
		var doc = frame.get(0).contentWindow.document;

		if (doc.title != null && doc.title.length > 0)
			container.dialog('option', 'title', doc.title);
	});

	// initialize, but don't show, the dialog
	container.dialog({
		autoOpen: false,
		title: title,
		width: w,
		height: h,
		minWidth: w,
		minHeight: h,
		modal: true,
		resizable: true,
		bgiframe: true,
		position: 'center',

		open: resizeIFrame,

		resizeStart: startFrameOverlay,
		resize: function()
		{
			updateFrameOverlay();			
			resizeIFrame();
		},
		resizeStop: stopFrameOverlay,

		dragStart: startFrameOverlay,
		drag: $.browser.msie 
			? function()
			{
				updateFrameOverlay();

				if ($.browser.msie)
					resizeIFrame();
			}
			: updateFrameOverlay,
		dragStop: $.browser.msie 
			? function()
			{
				stopFrameOverlay();
				resizeIFrame();
			}
			: stopFrameOverlay
	});

	// load the frame contents
	if (args == null || args.length == 0)
	{
		frame.load(doAutoResize);
		frame.attr("src", url);
		container.dialog('open');
	}
	else
	{
		// Build up a form to post args to dialog page with
		if ($("#vc3-dialog-argsField").length == 0)
		{
			$(document.body).append(
				"<form id='vc3-dialog-argsForm' target='vc3-dialog-frame' method='post' style='display: none'>" +
				"<input id='vc3-dialog-argsField' name='__DIALOG_ARG' type='hidden' value='' />" +
				"</form>");
		}

		$("#vc3-dialog-argsField").val(args);

		container.dialog('open'); // must show dialog before form post in FF

		frame.load(doAutoResize);
		$("#vc3-dialog-argsForm").attr("action", url).submit();
	}
}	
	

function Dialog_CloseDialog(returnValue, type)
{
	// Process return value
	if (type == null || typeof (type) == 'undefined')
		type = 'text';
		
	dialogInfo = self.closeDialogArgs;
	
	if (returnValue != null)
	{
		// hide dialog to give feedback but keep page overlay in place
		dialogInfo.container.parent().hide();
		
		if (type == 'refresh')
		{
			self.location.href = self.location.href;
		}
		else if (type == 'redirect')
		{
			self.location.href = returnValue;
		}
		else if (dialogInfo.postback)
		{
			__doPostBack(dialogInfo.id, type + '|' + returnValue);
		}
	}
	else
	{
		// Close dialog
		dialogInfo.container.dialog('close');
	}
}

// </script>