$(document).ready(function () {
	var option_idx = $("#slide-options .slide-option").length;

	$("#slide-add-option").off("click");
	$("#slide-add-option").on("click", function () {
		option_idx++;

		var option_template = $("#slide-option-template").html();
		option_template = option_template.replace("[[Idx]]", option_idx);
		option_template = option_template.replace("[-1]", "[" + option_idx + "]");

		$("#slide-options").append(option_template);
		OptionAction(true);
	});

	OptionAction(false);
});

function OptionAction(IsRefresh) {
	if (IsRefresh) {
		$("#slide-options").sortable("destroy");
	}

	$("#slide-options").sortable({
		axis: "y",
		handle: ".sort-handle",
		update: function (event, ui) {
			$(document).trigger("EmpigrafPresentationBoardAutoSave");
		}
	});
	$("#slide-options").disableSelection();

	$(".slide-option *[data-type='slide-option-remove']").off("click");
	$(".slide-option *[data-type='slide-option-remove']").on("click", function () {
		$(this).closest(".slide-option").remove();
		$(document).trigger("EmpigrafPresentationBoardAutoSave");
	});

	$("input, select, textarea", $("#EPBoardForm")).off("change");
	$("input, select, textarea", $("#EPBoardForm")).on("change", function () {
		$(document).trigger("EmpigrafPresentationBoardAutoSave");
	});
}