
// hides dialog with buttons
function hideDialog() {
	new Effect.Fade("overlaywarning", { duration: 0.3, from: 0.7, to: 0 });
	new Effect.Fade("warning", { duration: 0.3, from: 1, to: 0 });
        document.body.removeChild($("warning"));
        document.body.removeChild($("overlaywarning"));
    // function defined in base.js
	showSelectBoxes();
}

// shows a dialog with buttons and executes javascript when button is pressed
// parameters: title  : title that will be displayed on top of dialog
//             text   : text that will be displayed in dialog
//             buttons: array of JSON objects with following elements:
//                      caption: text on button
//                      javascript: literal javascript text to perform when
//                                  button is clicked (will be appended to a
//                                  method that closes dialog)
//
// example:
// showDialog("Confirmation", "Are you sure you want to do this?", [ {
//     caption: "OK",
//     javascript: "alert('OK pressed!');"
// }, {
//     caption: "Cancel",
//     javascript: "" // will do nothing, although dialog will be hidden
// } ]);
function showDialog(title, text, buttons) {
	// hide select boxes so they won't show through overlay
	// function defined in base.js
	hideSelectBoxes();

	// create LI-elements that will appear to be buttons
	var functions = new Array(buttons.length);
	for(var i = 0; i < buttons.length; i++) {
		functions[i] = Builder.node("li", {}, [
			Builder.node("a", { id: "showDialogHandler" + i, href: "#", onclick: "hideDialog(); " + buttons[i].javascript }, [
				Builder.node("span", { }, [ buttons[i].caption ])
			])
		]);
	}

	// create DIV-elements that will be added to DOM (body)
	var nodeOverlay = Builder.node("div", { id: "overlaywarning", style: "display: none;" });
	var nodeWarning = Builder.node("div", { id: "warning", style: "display: none;" }, [
		Builder.node("div", { id: "warningtop", style: "display: block;" }, [title]),
		Builder.node("div", { id: "warningcontent", style: "display: block;" }, [text, Builder.node("br")]),
		Builder.node("div", { id: "warningbuttons", style: "display: block;" }, [
			Builder.node("div", { "class": "submitbuttons", style: "display: block;" }, [
				Builder.node("ul", {}, functions)
			])
		])
	]);

	// add html to BODY tag
	document.body.appendChild(nodeOverlay);
	document.body.appendChild(nodeWarning);

	// show html (using fade effect)
	new Effect.Appear("overlaywarning", { duration: 0.3, from: 0, to: 0.7 });
	new Effect.Appear("warning", { duration: 0.3, from: 0, to: 1 });

	// set focus on first button (after 0.1 second to give appear effect time
	// to show buttons)
	setTimeout("$('showDialogHandler0').focus();", 100);
}

// dialog methods that use extjs MessageBox
function showAlertDialog(title, text, callbackOnClose, buttons) {
	Ext.MessageBox.show({
		title: title,
		msg: text,
		buttons: buttons == null ? Ext.MessageBox.OK : buttons,
		fn: callbackOnClose != null ? callbackOnClose : '',
		icon: Ext.MessageBox.WARNING
	});
}

function showConfirmDialog(title, text, callbackOnConfirm) {
	Ext.Msg.confirm(title, text, function(button) {
		if (button == 'yes') {
			callbackOnConfirm();
		}
	}.bind(this));
}
