$(function()
	{
		// Activate clipboard support for class "mbd-clipboard"; be sure to run it on DOM-ready (else clipboard.js won't be happy)
		new ClipboardJS('.mbd-clipboard');

		// Localize dates to browser's settings
		$('.mbd-localize-date').each(function()
			{
				$(this).text(new Date($(this).text()).toLocaleString());
		});

		// Show 'wait' cursor on load() (or other ajax functions)
		$(document)
			.ajaxStart(function(){$(document.body).css({'cursor': 'wait'});})
			.ajaxStop(function(){$(document.body).css({'cursor': 'default'});});
});

// alert on next event (test code, for reference only).
function mbd_event()
{
	$.ajax({url: "/static/events.attach", dataType: "json", complete: function(xhr, status)
		{
			console.log("EVENT", xhr);
			alert(JSON.stringify(xhr.responseJSON, null, 2));
	}});
}

function mbd_api_call(form, command, needs_confirmation)
{
	const api_uri = "/mini_buildd/api/";

	// Discard empty values mainly for convenience (shorter uris, nicer display) -- args w/ empty values are treated as 'not given' by API anyway.
	let args = $(form).serializeArray().filter(kv => kv.name != "output" && kv.name != "confirm" && kv.value != "");
	let args_serialized = jQuery.param(args);
	let args_text = "";
	for (let kv in args) { args_text += "  " + args[kv].name + "=" + args[kv].value + "\n"; }

	if (needs_confirmation == "False" || confirm("Please Confirm API Call: " + command + "\n" + args_text))
	{
		let get_uri = api_uri + command + "/?" + args_serialized;
		$("#mbd-api-popup-content").html("<details><summary>Running API Call <b>" + command + "</b></summary><pre>" + args_text + "</pre></details><progress style='width: 100%'></progress>");
		$("#mbd-api-popup-content").load(get_uri + "&confirm=" + command + "&output=snippet", function (response, status, xhr) { if (status == "error") { $("#mbd-api-popup-content").html(response);};});
		$("#mbd-api-popup").removeClass("mbd-hidden")
	};
	return false;  // Avoid non-js 'action=' to be performed
}
