
/**
 * Figure out which dialog to use next, creating a new one
 * if necessary.
 * 
 * @return string The ID of the dialog to use (includes # for jquery)a
 */
function whichDialog()
{
	for(i = 1; i <= 10; i++)
	{
		id = "dialog"+i;
		
		if(!$("#"+id).size())
		{
			$("body").append("<div id='"+id+"' class='jqueryDialog'></div>");
			makeDialog("#"+id);
			return("#"+id);
		}
		if(!$("#"+id).dialog("isOpen"))
			return("#"+id);
	}
}

/**
 * Close the top-most dialog box
 */
function dialogCloseTop()
{
	for(i = 10; i > 0; i--)
	{
		id = "#dialog"+i;
		if($(id).size() && $(id).dialog("isOpen"))
		{
			$(id).dialog("close");
			return;
		}
	}
}

/**
 * Reset the closeOnEscape attribute for each dialog so that only
 * the top one closes on the escape key.
 */
function dialogSetCloseKey()
{
	closeOnEscape = true;
	
	for(i = 10; i > 0; i--)
	{
		id = "#dialog"+i;
		if($(id).size() && $(id).dialog("isOpen"))
		{
			$(id).dialog("option", "closeOnEscape", closeOnEscape);
			closeOnEscape = false;
		}
	}
}


var jqueryConfirmClick = false;

/**
 * Show a dialog box with Ok/Cancel buttons.
 * 
 * @param string $title The title of the dialog
 * @param string $html The HTML to put inside the dialog
 * @param object $options The options for the dialog box
 */
function jqueryConfirm(title, html, options)
{
	_jqueryDialog(title, html, true, options);
}

/**
 * Show a dialog box.
 * 
 * @param string $title The title of the dialog
 * @param string $html The HTML to put inside the dialog
 * @param object $options The options for the dialog box
 */
function jqueryDialog(title, html, options)
{
	_jqueryDialog(title, html, false, options);
}

/**
 * Show a dialog box.
 * 
 * @param string $title The title of the dialog
 * @param string $html The HTML to put inside the dialog
 * @param bool $confirm Whether or not to use Ok/Cancel buttons
 * @param object $options The options for the dialog box
 */
function _jqueryDialog(title, html, confirm, options)
{
	jqueryConfirmClick = false;
	
	
	var width;
	var buttons;
	if(typeof(options) != "undefined")
	{
		width = options.width;
		buttons = options.buttons;
	}
	
	if(!width)
		width = 500;
	if(!buttons)
	{
		var buttonName = (options && options.buttonName ? options.buttonName : "Ok");
		
		buttons = {};
		buttons[buttonName] = function() {
			$(this).dialog("close");
		};
	}
	
	id = whichDialog();

	$(id).html("<div class='innerDialog'>" + html + "</div>");
	$(id).dialog("option", "width", width);
	$(id).dialog("option", "title", title);

	$(id).dialog("option", "close", function() {
		if(!jqueryConfirmClick)
		{
			if(confirm && options.onFalse)
				setTimeout(options.onFalse, 1);
			else if(options.onClose)
				setTimeout(options.onClose, 1);
		}
		setTimeout('dialogSetCloseKey();', 1);
	});

	if(confirm)
	{
		$(id).dialog("option", "buttons", {
			"Ok": function() {
				if(options && options.onTrue)
					setTimeout(options.onTrue, 1);
				jqueryConfirmClick = true;
				$(this).dialog("close");
			},
			Cancel: function() {
				if(options && options.onFalse)
					setTimeout(options.onFalse, 1);
				jqueryConfirmClick = true;
				$(this).dialog("close");
			}
		});
	}
	else
	{
		$(id).dialog("option", "buttons", buttons);
	}

	$(id).dialog("open");
	dialogSetCloseKey();
}

/**
 * Turn an element into a dialog box
 * 
 * @param string $id The ID of the dialog (including # for jquery)
 */
function makeDialog(id)
{
	$(id).dialog({
		bgiframe: true,
		modal: true,
		resizable: false,
		autoOpen: false,
		overlay: {
			backgroundColor: "#000",
			opacity: 0.5
		},
		buttons: {
			"Close": function() {
				$(this).dialog("close");
			}
		}
	});
}

