var Dialog = {
	/**
	 * Display a dialog box containing content received via an AJAX request.
	 *
	 * @param string url The url containing the content that should be displayed in the dialog.
	 * @param string title Optional title/caption to be displayed in the dialog box.
	 * @param mixed ok Optional string for the confirmation button, or boolean false if it shouldn't be displayed.
	 * @param mixed cancel Optional string for the cancel button, or boolean false if it shouldn't be displayed.
	 */
	show: function(url, title, ok, cancel) {
		if (typeof(title) == 'undefined') title = '';
		if (typeof(ok) == 'undefined') ok = 'Okay';
		if (typeof(cancel) == 'undefined') cancel = 'Cancel';
		$dialog = Dialog.get();
		var options = {
			modal: true,
			resizable: false,
			position: 'top',
			title: title,
			width: 500,
			buttons: {}
		};
		if (ok !== false) {
			options.buttons[ok] = function() {
				$form = $('#dialog form');
				$.ajax({
					type: $form.attr('method'),
					url: $form.attr('action'),
					data: $form.serialize(),
					success: function() {
						// @todo parse the json, and update screen elements
						alert('request sent to server. @todo update screen elements and notify user with friendly message.');
					}
				});
				$dialog.dialog('close');
			};
		}
		if (cancel !== false) {
			options.buttons[cancel] = function() {
				$dialog.dialog('close');
			};
		}
		$dialog.html('<p>Loading...</p>').load(url).dialog(options).dialog('open');
		return false;
	},
	
	/**
	 * Hide the dialog box if it's already being displayed.
	 */
	hide: function() {
		Dialog.get().dialog('close');
	},
	
	/**
	 * Retrieve a reference to the dialog box, creating one if it doesn't already exist.
	 */
	get: function() {
		if ($('#dialog').length == 0) {
			$('<div id="dialog"></div>').appendTo($('body'));
		}
		return $('#dialog');
	}
};
