function admininterface_activate() {

	function generate_id() {
		return "uniqid_"+Math.floor(Math.random()*4294967296);
	}

	function add_edit_button(after, value) {
		id= generate_id();
		$(after).after("<button class=\"admininterface\" id=\""+id+"\" style=\"display:none;\">"+value+"</button>");
		$("#"+id).each (function (i, button) {
			$(button).fadeIn("fast");
			$(button).click(function() {
				switch ($(button).text()) {
				case "ändern":
					node= button.previousSibling;
					text= node.data;
					$(node).replaceWith("<textarea class=\"admininterface\" style=\"display:none;\" cols=\"100\" rows=\"10\">"+text+"</textarea>");
					$(button.previousSibling).fadeIn("fast");
					button.firstChild.data= "speichern";
					return false;
				case "speichern":
					textarea= button.previousSibling;
					text= textarea.value;
					$(textarea).fadeOut("fast", function () {
						$(textarea).replaceWith(text);
						button.firstChild.data= "ändern";
						return false;
					});
				}
			});
		});
	}

	function add_add_button(after, tag) {
		id= generate_id();
		$(after).after("<"+tag+" class=\"admininterface\"><button id=\""+id+"\" style=\"display:none;\">hinzufügen</button></"+tag+">");
		$("#"+id).each (function (i, button) {
			$(button).fadeIn("fast");
			$(button).click(function() {
				$(button.parentNode).before("<"+tag+"><textarea class=\"admininterface\" style=\"display:none;\" cols=\"100\" rows=\"10\"></textarea></"+tag+">");
				li= button.parentNode.previousSibling;
				textarea= li.firstChild;
				$(textarea).fadeIn("fast");
				add_edit_button(textarea, "speichern");
				add_delete_button(li);
				return false;
			});
		});
	}

	function add_delete_button(inside) {
		id= generate_id();
		$(inside).append("<button class=\"admininterface\" id=\""+id+"\" style=\"display:none;\">löschen</button></li>");
		$("#"+id).each (function (i, button) {
			$(button).fadeIn("fast");
			$(button).click(function() {
				node= button.parentNode;
				$(node).fadeOut("fast", function () {
					$(node).remove();
				});
				return false;
			});
		});
	}

	$("body *").contents().filter(function() {
		if (this.nodeType != Node.TEXT_NODE) {
			return false;
		}
		str= this.nodeValue;
		str= str.replace(/^[ \t\n]*/, "");
		if (str.length == 0) {
			return false;
		}
		return true;
	}).each(function(i, text) {
		add_edit_button(text, "ändern");
	});

	$("li").each(function(i, li) {
		add_delete_button(li);
	});

	$("p").each(function(i, p) {
		add_delete_button(p);
	});

	$("ul").each(function(i, ul) {
		child= ul.firstChild;
		li= undefined;
		while (child) {
			next= child.nextSibling;
			if (next && next.nodeName == "LI") {
				li= next;
			}
			child= next;
		}
		add_add_button(li, "li");
	});

	$("p").each(function(i, p) {
		child= p;
		while (child.nextSibling) {
			child= child.nextSibling;
			if (child.nodeName == "P") {
				return;
			}
		}
		add_add_button(p, "p");
	});

}

function admininterface_deactivate() {
	$("textarea.admininterface").each(function(i, textarea) {
		text= textarea.value;
		$(textarea).fadeOut("fast", function () {
			$(textarea).replaceWith(text);
			return false;
		});
	});
	$(".admininterface").each(function(i, node) {
		if (node.nodeName == "TEXTAREA") {
			return;
		}
		$(node).fadeOut("fast", function() {
			$(node).remove();
		});
	});
}

$(document).ready(function () {
	activated= false;
	$("#admininterface").each(function(i, node) {
		$(node).click(function() {
			switch (activated) {
			case false:
				admininterface_activate();
				activated= true;
				return false;
			case true:
				admininterface_deactivate();
				activated= false;
				return false;
			}
		});
	});
});

