
(function($) {
	var modaldialog = { };

	// Creates and shows the modal dialog
	function showDialog (msg, options) {
		// Make sure the dialog type is valid. If not assign the default one (the first)
		if(!$.inArray(options.type, modaldialog.DialogTypes)) {
			options.type = modaldialog.DialogTypes[0];
		};

		// Merge default title and default settings
		var settings = $.extend({ title: modaldialog.DialogTitles[options.type] }, modaldialog.defaults, options);
		settings.timeout = (typeof(settings.timeout) == "undefined") ? 0 : settings.timeout;

		// Check if the dialog elements exist and create them if not
		if (!document.getElementById('dialog')) {
			dialog = document.createElement('div');
			dialog.id = 'dialog';
			$(dialog).html(
				"<div id='dialog-header'>" +
					"<div id='dialog-title'></div>" +
					"<div id='dialog-close'></div>" +
				"</div>" +
				"<div id='dialog-content'>" +
					"<div id='dialog-content-inner' /></div>" +
				"</div>"
				);
			
			$(dialog).hide();
			
			document.body.appendChild(dialog);

			// "x" buttons			
			$("#dialog-close").click(modaldialog.hide);
		}

		var dl = $('#dialog');
		var dlh = $('#dialog-header');
		var dlc = $('#dialog-content');

		$('#dialog-title').html(settings.title);

		if (settings.type=="success"){
			$('#dialog-content-inner').html('<img src="images/solution/'+ msg +'.jpg"/>');
		} else {
			$('#dialog-content-inner').load(msg + '.html');
		}
		
		// Center the dialog in the window
		dl.css('width', settings.width);
		dl.css('height', settings.height);
		var dialogTop = Math.abs($(window).height() - dl.height()) / 2;
		// 25px from top
		dl.css('top', (dialogTop >= 25) ? dialogTop : 25);
		dl.css('left', ($(window).width() - dl.width()) / 2);

		// Clear the dialog-type classes and add the current dialog-type class		
		$.each(modaldialog.DialogTypes, function () { dlh.removeClass(this + "header") });
		dlh.addClass(settings.type + "header")
		$.each(modaldialog.DialogTypes, function () { dlc.removeClass(this) });
		dlc.addClass(settings.type);

		if (settings.timeout) {
			window.setTimeout("$('#dialog').stop(true, true).fadeOut('slow', 0);", (settings.timeout * 1000));
		}
		
		dl.stop(true, true).fadeIn("slow");
	};

	modaldialog.success = function $$modaldialog$error (msg, options) {
		if (typeof(options) == "undefined") {
			options = { };
		}
		options['type'] = "success";
		return(showDialog(msg, options));
	}
	modaldialog.prompt = function $$modaldialog$error (msg, options) {
		if (typeof(options) == "undefined") {
			options = { };
		}
		options['type'] = "prompt";
		return(showDialog(msg, options));
	}

	modaldialog.hide = function $$modaldialog$hide () {
		$('#dialog').stop(true, true).fadeOut("slow", function () { $(this).hide(0); });
	};

	modaldialog.DialogTypes = new Array("success", "prompt");
	modaldialog.DialogTitles = {
		"success": "Success"
		, "prompt": "Please Choose"
	};

	modaldialog.defaults = {
		timeout: 0
		, width: 700
		, height: 400
	};

	$.extend({ modaldialog: modaldialog });
})(jQuery);

