$(function() {
	(function() {
		$.extend($.ui.dialog.defaults, {
			bgiframe: true,
			closeOnEscape: true,
			resizable: false,
			width: 500
		});

		$('a.dialog').click(function(e) {
			e.preventDefault();

			var that = this,
				dialogOptions = $.extend($(this).metadata().dialog, {
					open: function() {
						var newDialog = this;
						setElementsVisibility(document.getElementById("body-container"), ['select'], 'hidden');
						$('div.ui-dialog-content')
									.filter(function() { return this !== newDialog; })
										.dialog('destroy')
										.remove();
					},
					close: function() {
						setElementsVisibility(document.getElementById("body-container"), ['select'], 'visible');
						$(this).dialog('destroy').remove();
					}
				});

			$.ajax({
				dataType: 'html',
				url: this.href,
				success: function(html) {
					var $content = $('<div>' + html + '</div>');

					// Remove noscript tags (Webkit can't insert noscript inputs correctly)
					$content.find('noscript').remove();

					var root = $content.appendTo('body').get(0);
					$(root).dialog(dialogOptions);
				}
			});
		});
	})();
});
