(function () {

var openDialogs = {};

window.getEntry = function (title, message, isSecure, cb) {
	var dh = $("#entryDialog").dialog({
		title: title,
		resizable: false,
		closeOnEscape: false,
		closable: false,
		modal: true,
		dialogClass: "no-close",
		open: function () {
			$("#screen").hide();	

			openDialogs["#entryDialog"] = true;
		},
		close: function () {
			$("#screen").show();

			openDialogs["#entryDialog"] = false;
		},
		buttons: {
			Cancel: function () {
				cb(0);

				dh.dialog("close");
			},
			Enter: function () {
				var text = $("#entryText").val();

				cb(1, text);

				dh.dialog("close");
			}
		}
	});

	$("#entryText").val("");
	$("#entryText").get(0).type = (isSecure) ? "password" : "input";
	$("#entryMessage").text(message);

	closeDialog("#msgDialog");

	return dh;
}

window.getYesNoQuestionResponse = function (title, message, cb) {
	var dh = $("#msgDialog").dialog({
		title: title,
		resizable: false,
		closeOnEscape: false,
		closable: false,
		modal: true,
		minWidth: 520,
		dialogClass: "no-close",
		open: function () {
			$("#screen").hide();	

			openDialogs["#msgDialog"] = true;
		},
		close: function () {
			$("#screen").show();

			openDialogs["#msgDialog"] = false;
		},
		buttons: {
			No: function () {
				cb(0);

				dh.dialog("close");
			},
			Yes: function () {
				cb(1);

				dh.dialog("close");
			}
		}
	});

	$("#msgText").text(message);

	closeDialog("#entryDialog");
	
	return dh;
}

window.showFatalError = function (message, cb) {
	var dh = $("#msgDialog").dialog({
		title: "Error",
		resizable: false,
		modal: true,
		dialogClass: "fatal-error",
		open: function () {
			$("#screen").hide();	

			$("div.fatal-error a.ui-dialog-titlebar-close").remove();

			openDialogs["#msgDialog"] = true;
		},
		close: function () {
			if (cb) {
				cb();
			}

			$("#screen").show();

			openDialogs["#msgDialog"] = false;
		},
		buttons: {
			Okay: function () {
				dh.dialog("close");
			}
		}
	});

	$("#msgText").text(message);

	closeDialog("#entryDialog");

	return dh;
}

window.closeDialog = function (id) {
	if (openDialogs[id])
		$(id).dialog("close");
}

})();