$(document).ready(function() {
$.extend({
	msg: function(options) { // Simple message display system
		$._askoptions = $.extend({
			title: 'Alert', // The title of the dialog
			text: 'Hello World', // HTML text of the dialogue
			icon: null, // Optional icon src to accompany the message. Can also be: question, ok, alert
			success: function() {}, // Call this callback on close
			show: null, // What effect to use when showing (e.g. slide)
			hide: null, // What effect to use when hiding (e.g. slide)
			effect: null, // Shorthand function to set both show & hide at the same time
			model: true, // Whether this dialog is modal?
			buttons: {
				Ok: function() {
					$('#' + $._askoptions['dialogid']).dialog('close');
					$._askoptions['success']();
				}
			}, // Override the existing 'ok' button with this set

			// Less interesting options
			dialogid: 'dialog', // What to call the dialog we need to create (<div id="?">TEXT</div>)
			textboxid: 'dialogquest' // What to call the dialog text box
		}, options);
		if ($._askoptions['effect'])
			$._askoptions['show'] = $._askoptions['hide'] = $._askoptions['effect'];
		if ($._askoptions['icon']) {
			if ($._askoptions['icon'] == 'ok') {
				$._askoptions['text'] = '<table width="100%"><tr><td width="30px" style="padding: 20px"><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span></td><td>' + $._askoptions['text'] + '</td></tr></table>';
			} else if ($._askoptions['icon'] == 'question') {
				$._askoptions['text'] = '<table width="100%"><tr><td width="30px" style="padding: 20px"><span class="ui-icon ui-icon-help" style="float:left; margin:0 7px 50px 0;"></span></td><td>' + $._askoptions['text'] + '</td></tr></table>';
			} else if ($._askoptions['icon'] == 'alert') {
				$._askoptions['text'] = '<table width="100%"><tr><td width="30px" style="padding: 20px"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"></span></td><td>' + $._askoptions['text'] + '</td></tr></table>';
			} else
				$._askoptions['text'] = '<table width="100%"><tr><td width="30px" style="padding: 20px"><img src="' + $._askoptions['icon'] + '"/></td><td>' + $._askoptions['text'] + '</td></tr></table>';
		}

		$('body').append('<div id="' + $._askoptions['dialogid'] + '">' + $._askoptions['text'] + '</div>');
		$('#' + $._askoptions['dialogid']).dialog({
			modal: $._askoptions['model'],
			title: $._askoptions['title'],
			show: $._askoptions['show'],
			hide: $._askoptions['hide'],
			buttons: $._askoptions['buttons'],
			close: function() {
				$('#' + $._askoptions['dialogid']).remove();
				$('#' + $._askoptions['dialogid']).dialog('close');
			}
		});
	},
	yesno: function(options) { // Ask a yes no question. Trigger 'yes' or 'no' on the answer (closes are assumed as 'no')
		$._askoptions = $.extend({
			icon: 'question',
			yes: function() {},
			no: function() {}
		}, options);
		$._askoptions['buttons'] = {
			Yes: function() {
				$('#' + $._askoptions['dialogid']).dialog('close');
				$._askoptions['yes']();
			},
			No: function() {
				$('#' + $._askoptions['dialogid']).dialog('close');
				$._askoptions['no']();
			}
		};
		$.msg($._askoptions);
	},
	ask: function(options) { // Ask a text based question and call a callback on answering
		$._askoptions = $.extend({
			title: 'Question', // The title of the dialog
			text: 'What is your name?', // The actual HTML question asked
			after: '', // HTML ouput after the input area (and before the buttons)
			preclose: function() {}, // Call this function before the dialog is destroyed (useful to store values before the DOM element gets destroyed)
			success: function() {}, // Call this callback on success
			fail: function() {}, // Call this callback on fail
			value: '', // The default value
			prefix: '', // The prefix of the prompt
			suffix: '', // The suffix of the prompt

			// Less interesting options
			dialogid: 'dialog', // What to call the dialog we need to create (<div id="?">TEXT</div>)
			textboxid: 'dialogquest' // What to call the dialog text box
		}, options);

		$('body').append('<div id="' + $._askoptions['dialogid'] + '">' + $._askoptions['text'] + '<br/><div class="spaced">' + $._askoptions['prefix'] + '<input type="text" id="' + $._askoptions['textboxid'] + '" name="' + $._askoptions['textboxid'] + '" value="' + $._askoptions['value'] + '"/>' + $._askoptions['suffix'] + '</div>' + $._askoptions['after'] + '</div>');
		$('#' + $._askoptions['dialogid']).dialog({
			modal: true,
			title: options['title'],
			buttons: {
				Ok: $._askok,
				Cancel: $._askcancel
			},
			close: function() {
				$._askoptions['preclose']();
				$('#' + $._askoptions['dialogid']).remove();
			}
		});
		$('#' + $._askoptions['textboxid']).bind('keypress', function(event) {
			var key = (event.keyCode ? event.keyCode : event.which);
			if(key == 13)
				$._askok();
		}).select();
	},
	_askok: function() {
		var answer = $('#' + $._askoptions['textboxid']).val();
		$('#' + $._askoptions['dialogid']).dialog('close');
		$._askoptions['success'](answer);
	},
	_askcancel: function() {
		$('#' + $._askoptions['dialogid']).dialog('close');
		$._askoptions['fail']();
	}
});
});

