(function($)
{

// Private

var dialog_array = new Object();
var message_dialog = 'dialogutils-message-dialog';
var confirm_dialog = 'dialogutils-confirm-dialog';
var confirm_twice_dialog = 'dialogutils-confirm-twice-dialog';

// Public

jQuery.dialogutils = {

	dialog_exists: function(name)
	{
		return (dialog_array[name] != undefined);
	},

	create_dialog: function(name, options)
	{
		if (dialog_array[name] != undefined) {
			return false;
		}
			
		if ($('#'+name).length > 0)
		{
			alert('Already an item with the same id, can\'t create dialog');
			return false;
		}
	
		var buttons = new Object();
		buttons[$.dialogutils.texts.ok_button] = function() {
			dialog_array[$(this).attr('id')].result = true;
			$(this).dialog('close');
		};
		var dialog_options = $.extend({
			buttons: buttons,
			open: function(event, ui) {
				$('button:contains("'+$.dialogutils.texts.ok_button+'")',
					$(this).parent()).addClass('confirm_button');
			}
		}, $.dialogutils.dialog_defaults, options);
		
		$('body').append($('<div id="'+name+'" class="dialog"></div>'));
		
		var dialog = new Object();
		dialog.dialog = $('#'+name);
		dialog.callback = false;
		dialog.data = undefined;
		dialog.autofocus = dialog_options.autoFocus;
		dialog.result = undefined;
		
		dialog_array[name] = dialog;
		$(dialog.dialog).dialog(dialog_options);
		
		return dialog.dialog;
	},
	
	remove_dialog: function(name)
	{
		$('#'+name).dialog('destroy').remove();
	},
	
	create_confirm_dialog: function(name, confirm_text, options, cancel_text)
	{
		if (!cancel_text) {
			cancel_text = $.dialogutils.texts.cancel_button;
		}
		
		if (!confirm_text) {
			confirm_text = $.dialogutils.texts.confirm_button;
		}
	
		var buttons = new Object();
		buttons[confirm_text] = function() {
			dialog_array[$(this).attr('id')].result = true;
			$(this).dialog('close');
		};
		buttons[cancel_text] = function() {
			dialog_array[$(this).attr('id')].result = false;
			$(this).dialog('close');
		};
		
		var dialog_options = $.extend({
			buttons: buttons,
			open: function(event, ui) {
				$('button:contains("'+confirm_text+'")',
					$(this).parent()).addClass('confirm_button');
				$('button:contains("'+cancel_text+'")',
					$(this).parent()).addClass('cancel_button');
			}
		}, $.dialogutils.confirm_defaults, options);
		
		$.dialogutils.create_dialog(name, dialog_options);
	},
	
	show_dialog: function(name, title, message, callback, data)
	{
		if (typeof(name) != 'string') {
			name = $(name).attr('id');
		}
		
		var dialog = dialog_array[name];
		if (dialog != undefined)
		{
			$(dialog.dialog).dialog('option', 'title', title);
			$(dialog.dialog).html(message);
			dialog.callback = callback;
			dialog.data = data;
			dialog.result = undefined;
			$(dialog.dialog).dialog('open');
			$(dialog.dialog).dialog('moveToTop');
			$.dialogutils.autofocus_dialog(name);
		}
	},
	
	close_dialog: function(name)
	{
		var dialog = dialog_array[name];
		if (dialog != undefined) {
			$(dialog.dialog).dialog('close');
		}
	},
	
	remove_dialog: function(name)
	{
		if (name == message_dialog || name == confirm_dialog ||
			name == confirm_twice_dialog)
		{
			return false;
		}
	
		var dialog = dialog_array[name];
		if (dialog != undefined)
		{
			$(dialog.dialog).dialog('destroy');
			$(dialog.dialog).remove();
			delete dialog_array[name];
			return true;
		}
		
		return false;
	},
	
	message_dialog: function(title, message, callback, data)
	{
		if (dialog_array[message_dialog] == undefined) {
			$.dialogutils.create_dialog(message_dialog);
		}
		
		$.dialogutils.show_dialog(message_dialog,
			title, message, callback, data);
	},
	
	message_dialog_id: function() {
		return message_dialog;
	},
	
	confirm_dialog: function(title, message, callback, data)
	{
		if (dialog_array[confirm_dialog] == undefined)
		{
			$.dialogutils.create_confirm_dialog(confirm_dialog,
				$.dialogutils.texts.yes_button);
		}
		
		var final_message = '<span class="confirm_msg">' +
			$.dialogutils.texts.confirm +
			' <span class="msg">' + message + '</span>?</span>';
		$.dialogutils.show_dialog(confirm_dialog,
			title, final_message, callback, data);
	},
	
	confirm_dialog_id: function() {
		return confirm_dialog;
	},
	
	confirm_twice_dialog: function(title, message, callback, data)
	{
		if (dialog_array[confirm_twice_dialog] == undefined)
		{
			var buttons = new Object();
			buttons[$.dialogutils.texts.yes_button] = function(event, ui) {
				if ($('.confirmed_once', this).length > 0)
				{
					dialog_array[$(this).attr('id')].result = true;
					$(this).dialog('close');
				}
				else
				{
					var message = $('.msg', $(this)).html();
					$('.confirm_msg', $(this)).fadeOut('fast', function()
					{
						$(this).html($.dialogutils.texts.confirm_twice +
							' <span class="msg confirmed_once">' +
							message +
							'</span>?').fadeIn();
					});
				}
			};
			buttons[$.dialogutils.texts.cancel_button] = function() {
				dialog_array[$(this).attr('id')].result = false;
				$(this).dialog('close');
			};
			
			$.dialogutils.create_confirm_dialog(confirm_twice_dialog,
				$.dialogutils.texts.yes_button, {buttons: buttons});
		}
		
		var final_message = '<span class="confirm_msg">' +
			$.dialogutils.texts.confirm +
			' <span class="msg">' + message + '</span>?</span>';
		$.dialogutils.show_dialog(confirm_twice_dialog,
			title, final_message, callback, data);
	},
	
	confirm_twice_dialog_id: function() {
		return confirm_twice_dialog;
	},
	
	autofocus_dialog: function(name)
	{
		if (typeof(name) != 'string') {
			name = $(name).attr('id');
		}
		
		var dialog = dialog_array[name];
		if (dialog != undefined)
		{
			var autofocus = $(dialog.autofocus, $(dialog.dialog).parent());
			if (autofocus.length > 0) {
				autofocus.focus();
			}
			else {
				$('.ui-dialog-titlebar-close',
					$(dialog.dialog).parent()).focus();
			}
		}
	},
	
	texts:
	{
		confirm: 'Are you sure you want to',
		confirm_twice: 'Are you really sure you want to',
		ok_button: 'Ok',
		yes_button: 'Yes',
		cancel_button: 'Cancel',
		confirm_button: 'Save'
	},
	
	dialog_defaults:
	{
		closeOnEscape: true,
		draggable: false,
		modal: true,
		resizable: false,
		autoOpen: false,
		bgiframe: true,
		beforeclose: function(event, ui) {
			var closeDialog = true;
			var dialog = dialog_array[$(this).attr('id')];
			if (dialog.callback)
			{
				if (dialog.data != undefined) {
					closeDialog = dialog.callback.apply(
						dialog.dialog, new Array(dialog.result, dialog.data));
				}
				else {
					closeDialog = dialog.callback.apply(
						dialog.dialog, new Array(dialog.result));
				}
			}
			if (closeDialog != undefined && !closeDialog)
			{
				dialog.result = undefined;
				return false;
			}
		},
		autoFocus: 'button.confirm_button'
	},
	
	confirm_defaults:
	{
	
	}
};

})(jQuery);