var dropdowntimeout;
var ajaxDropdown = function(name, file) {
	clearTimeout(dropdowntimeout);
	start = function() {
		new Ajax.Updater(name + "[list]", file, {
			method: 'get',
			evalScripts: true,
			parameters: { q: $(name + "[input]").value },
			onCreate: function() { wait(name + "[list]"); $(name + "[list]").style.display = 'block'; }
		});
	}
	dropdowntimeout = setTimeout("start()", 500);
}
var ajaxDropdownSelect = function(name, value, label) {
	if($(name + "[hidden]")) { $(name + "[hidden]").value = value; }
	$(name + "[input]").value = label;
	$(name + "[list]").style.display = "none";
}
var calendar = function(target, y, m, d, doafter) {
	new Ajax.Updater("overlaybox", "snippets/tools/calendar.php", {
			method: 'post',
			evalScripts: true,
			parameters: { target: target, doafter: doafter, y: y, m: m, d: d },
			onSuccess: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "436px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
function center(el) {
	var h = document.viewport.getHeight();
	var w = document.viewport.getWidth();
	var o = document.viewport.getScrollOffsets();
	if(h < w) { m = h; } else { m = w; }
	el.style.position = "absolute";
	el.style.top = Math.round((document.viewport.getHeight()-el.getHeight())/5) + o["top"] +  "px";
	el.style.left = Math.round((document.viewport.getWidth()-el.getWidth())/2) + o["left"] + "px";
}
var clearOverlay = function() { $("overlay").style.display = "none"; }
var composeMessage = function(to) {
	new Ajax.Updater("overlaybox", "snippets/community/messages/compose.php", {
			method: 'get',
			evalScripts: true,
			parameters: { to: to },
			onSuccess: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "600px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
function changeNickname() {
	if(jQuery("#modal").size()) { jQuery("#modal").remove(); }
	jQuery("body").append('<div class="modal" id="modal" />');
	var modal = jQuery("#modal");
	modal.hide().load("snippets/chat/changenickname.php", "cachekiller=" + Math.random(), function() {
		viewportWidth = jQuery(document).width();
		viewportHeight = jQuery(document).height();
		boxWidth = jQuery(this).outerWidth();
		boxHeight = jQuery(this).outerHeight();
		jQuery(this)
			.css("top", (viewportHeight-boxHeight+jQuery(window).scrollTop())/3)
			.css("left", (viewportWidth-boxWidth)/2)
			.wrap('<div class="modal-wrapper"></div>')
			.fadeIn(300);
	});
}
var datePicker = function(prefix, id, y, m, d, doafter) {
	new Ajax.Updater(id + "datepicker", "snippets/tools/datepicker.php", {
			method: 'post',
			evalScripts: true,
			parameters: { prefix: prefix, id: id, doafter: doafter, y: y, m: m, d: d },
			onSuccess: function() { if(!$(id + "datepicker").visible()) { Effect.BlindDown(id + "datepicker", { "direction": "top-left", "duration": .3}); }; },
			onCreate: function() { }
		});
}
function deleteComment(id) {
	jQuery.ajax({
		url: "/snippets/tools/comments/delete.php",
		data: { id: id },
		type: "post",
		success: function() { jQuery(".comment_" + id).slideUp(function() { $(this).remove(); }); }
	});
}
function disablerOff() {
	document.getElementById("disabler").style.display = "none";
}
function doBookmark() {
	ap = navigator.appName;
	url = location.href;
	t = document.getElementsByTagName("title")[0].innerHTML;
	
	if(window.sidebar) { window.sidebar.addPanel(t, url, ''); }
	else if(window.external) { window.external.AddFavorite(url, t); }
	else if (window.addNet) { addNet(url, t); }
	else { alert("Ihr Browser unterst&uuml;tzt diese Funktion nicht.\nBitte Legen Sie das Lesezeichen manuell an."); }
}
var editImageText = function(id) {
	new Ajax.Updater("overlaybox", "snippets/tools/imageadder/edittext.php", {
			method: 'post',
			evalScripts: true,
			parameters: { id: id },
			onSuccess: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "600px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
var editText = function(id) {
	new Ajax.Updater("text_" + id, "/snippets/admin/texts/edit.php", {
		parameters: { id: id, nocache: Math.random(0, 100000) },
		evalScripts: true,
		method: "post",
		onComplete: function() { htmltextareas(); }
	});
}
var favArtistUi = function(artistid) {
	new Ajax.Updater("favartist_" + artistid, "snippets/tools/artists/fav/ui.php", {
			method: 'post',
			evalScripts: true,
			parameters: { artistid: artistid },
			onCreate: function() { wait("favartist_" + artistid); }
		});
}
var friendrq = function(to) {
	new Ajax.Updater("overlaybox", "snippets/community/friends/request.php", {
			method: 'get',
			evalScripts: true,
			parameters: { to: to },
			onSuccess: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "400px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
var geoSetPos = function(q, target) {
	new Ajax.Updater("overlaybox", "snippets/tools/geo/setpos.php", {
			method: 'get',
			evalScripts: true,
			parameters: { q: q, target: target },
			onSuccess: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "600px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
var geoShowPos = function(q, header) {
	new Ajax.Updater("overlaybox", "snippets/tools/geo/showpos.php", {
			method: 'get',
			evalScripts: true,
			parameters: { q: q, header: header },
			onComplete: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "600px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
function htmltextareas(theme) {
	theme = typeof(theme) != 'undefined' ? theme : "advanced";
	tinyMCE.init({
		mode : "textareas",
		auto_resize: true,
		theme : theme,
		content_css : "/css/import.css",
		forced_root_block : "p",
		theme_advanced_buttons1 : "bold,italic,sup,|,undo,redo,|,bullist,numlist,|,link,unlink,|,code",
		theme_advanced_buttons2 : "styleselect,formatselect",
		theme_advanced_buttons3 : "",
		theme_advanced_blockformats : "h1,h2,p,.pullquote",
		editor_deselector : "plaintext",
		convert_urls : false
	});
}
var myPresenceUi = function(eventid) {
	new Ajax.Updater("mypresence[" + eventid + "]", "snippets/tools/presence/ui.php", {
			method: 'post',
			evalScripts: true,
			parameters: { eventid: eventid },
			onCreate: function() { wait("mypresence[" + eventid + "]"); }
		});
}
var newsletter = function() {
	var h = jQuery(window).height();
	var w = jQuery(window).width();
	var o = { "top": jQuery(document).scrollTop(), "left": jQuery(document).scrollLeft() };
	if(h < w) { m = h; } else { m = w; }
	if(jQuery("#newsletterbox").size()) { jQuery("#newsletterbox").remove(); }
	jQuery("body").append("<div id=\"newsletterbox\" style=\"display:none;\"></div>");
	jQuery("#newsletterbox").load(
		"/snippets/newsletter/inputs.php",
		null,
		function() {
			jQuery(this).fadeIn(300, function() {
				jQuery(this).offset({
					top: Math.round((jQuery(window).height()-jQuery(this).height())/4),
					left: Math.round((jQuery(window).width()-jQuery(this).width())/2)
				});
			});
		}
	);
}
var keepOnline = function() {
	new Ajax.Request("snippets/community/keep_online.php", {
			method: 'post',
			evalScripts: true
		});
}
var previewText = function(text) {
	new Ajax.Updater("overlaybox", "snippets/tools/previewtext.php", {
			method: 'post',
			evalScripts: true,
			parameters: { text: text },
			onSuccess: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "400px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
var rate = function(table, id, rating) {
	new Ajax.Updater("test", "snippets/tools/rating/rate.php", {
			method: 'post',
			evalScripts: true,
			parameters: { table: table, id: id, rating: rating },
			onSuccess: function() { ratingBox(table, id); },
			onCreate: function() { wait("test"); }
		});
}
var ratingHover = function(table, id, rating, icon) {
	for(i = 1; i <= 5; i++) {
		$("rate_" + table + "_" + id + "_" + i).src = "imgs/icons/" + icon + ".dimmed";
	}
	for(i = 1; i <= rating; i++) {
		$("rate_" + table + "_" + id + "_" + i).src = "imgs/icons/" + icon;
	}
}
var ratingBox = function(table, id) {
	new Ajax.Updater("rbox_" + table + "_" + id, "snippets/tools/rating/ratingbox.php", {
			method: 'post',
			evalScripts: true,
			parameters: { table: table, id: id },
			onCreate: function() { wait("rbox_" + table + "_" + id); }
		});
}
var refreshComments = function(tablename, itemid, offset) {
	new Ajax.Updater("comments_" + tablename + "_" + itemid, "snippets/tools/commentarea/comments.php", {
		method: 'post',
		evalScripts: true,
		parameters: { tablename: tablename, itemid: itemid, offset: offset },
		onCreate: function() { wait("comments_" + tablename + "_" + itemid); }
	});
}
var replaceHeaders = function() {
	/* var headers = $$('h1');
	for(i = 0; i < headers.length; i++) {
		var label = headers[i].innerHTML;
		headers[i].replace("<img src=\"php/imgs/h1.php?t=" + escape(label) + "\" class=\"h1\" alt=\"" + label + "\" />");
	} */
}
function resizeTextarea(el) {
	if(!jQuery("#otg").size()) { jQuery("body").append("<div id=\"otg\" />"); }
	var otg = jQuery("#otg");
	otg.html(nl2br(el.val(), 1))
		.width(el.width())
		.css("position", "absolute")
		.css("left", "-9999px")
		.css("font-size", el.css("font-size"));
	el.animate({ height: otg.height()*.83 }, 300, "swing");
}
var save = function(form, href) {
	new Ajax.Updater("test", href, {
		method: 'post',
		evalScripts: true,
		parameters: $(form).serialize(),
		encoding: "utf-8",
		onSuccess: function() { alert("Einstellungen gespeichert."); },
		onFailure: function() { alert("Es ist ein Fehler aufgetreten."); }
	});
}
var savePresence = function(eventid) {
	new Ajax.Request("snippets/tools/presence/save.php", {
			method: 'post',
			evalScripts: true,
			parameters: $("presenceform[" + eventid + "]").serialize(),
			onCreate: function() { $("presencecheckbox[" + eventid + "]").replace("<img src=\"imgs/icons/wait16trans.gif\" class=\"ajaxWait\" />"); },
			onComplete: function() { myPresenceUi(eventid); }
		});
}
function saveText(id) {
	new Ajax.Request("/snippets/admin/texts/save.php", {
		parameters: $("edit_text_" + id).serialize(),
		method: "post",
		onComplete: function() { updateText(id); }
	})
}
function sendThisPage() {
	document.getElementById("disabler").style.display = "block";
}
function showImage(file) {
	var h = document.viewport.getHeight();
	var w = document.viewport.getWidth();
	var o = document.viewport.getScrollOffsets();
	if(h < w) { m = h; } else { m = w; }
	if($("bigImage")) { $("bigImage").remove(); }
	if($("loading")) { $("loading").remove(); }
	$$("body")[0].insert("<div id=\"loading\">Einen Moment bitte...</div>");
	$$("body")[0].insert("<img src='/php/imgs/scale.php?f=" + file + "&amp;m=" + Math.round(m*.95) + "' id='bigImage' style='display:none;' onload=\"\" onclick=\"$('bigImage').fade({ duration : .3 })\" />");
	$("loading").style.top = Math.round((document.viewport.getHeight()-$("loading").getHeight())/2) + o["top"] +  "px";
	$("loading").style.left = Math.round((document.viewport.getWidth()-$("loading").getWidth())/2) + o["left"] + "px";
	$("bigImage").observe("load", function() {
		if($("loading")) { $("loading").remove(); }
		$("bigImage").style.top = Math.round((document.viewport.getHeight()-$("bigImage").getHeight())/2) + o["top"] +  "px";
		$("bigImage").style.left = Math.round((document.viewport.getWidth()-$("bigImage").getWidth())/2) + o["left"] + "px";
		$("bigImage").appear({ duration : .3 });
	});
}
function submitComment(table, itemid) {
	jQuery.ajax({
		url: "/snippets/tools/comments/add.php",
		data: jQuery("#addcomment_" + table + "_" + itemid).serialize(),
		type: "post",
		success: function(data) { jQuery("#commentarea_" + table + "_" + itemid).load("/snippets/tools/comments/commentarea.php", "table=" + table + "&itemid=" + itemid + "&cachekiller=" + Math.random()); }
	});
}
var switchview = function(elementid) {
	if($(elementid).className == "vTiles") { $(elementid).className = "vBiglist"; }
	if($(elementid).className == "vBiglist") { $(elementid).className = "vList"; }
	if($(elementid).className == "vList") { $(elementid).className = "vTiles"; }
}
function trim (string) {
	return string.replace(/^\s+/, '').replace (/\s+$/, '');
}
function updateText(id) {
	new Ajax.Updater("text_" + id, "/snippets/tools/texts/text.php", {
		evalScripts: true,
		method: "post",
		parameters: { id: id }
	});
}
function validate(el, type) {
	el.bind("blur", function() {
		var value;
		if(el.attr("type") == "radio") {
			value = jQuery("[name='" + el.attr("name") + "']:checked").val();
		}
		else { value = el.val(); }
		if(!jQuery("#validate" + el.attr("id")).size()) { el.parents(".i-pair").append("<div id=\"validate" + el.attr("id") + "\" class=\"validate\"></div>"); }
		jQuery("#validate" + el.attr("id")).empty();
		jQuery.ajax({
			url: "validate/v.php",
			type: "get",
			data: { value: value, type: type, cachekiller: Math.random() },
			success: function(data) {
				if(data == 1) { jQuery("#validate" + el.attr("id")).append("<img src=\"/imgs/icons/silk/tick.png\" />").removeClass("err"); }
				else { jQuery("#validate" + el.attr("id")).append(data).addClass("err"); }
			}
		});
	})
	.focus(function() { jQuery("#validate" + el.attr("id")).empty(); });
}
function vote(table, itemid, score) {
	jQuery("." + table + "_" + itemid).load("/snippets/tools/voting.php", { table: table, itemid: itemid, score: score, cachekiller: Math.random() });
}
var wait = function(elementid) {
	$(elementid).innerHTML = "<span class=\"wait\"><img src=\"imgs/icons/wait16trans.gif\" /></span>";
}
var writeNews = function() {
	new Ajax.Updater("overlaybox", "snippets/news/compose.php", {
			method: 'get',
			evalScripts: true,
			onSuccess: function() { $("overlay").style.display = "block"; $("overlaybox").style.width = "400px"; },
			onCreate: function() { wait("overlaybox"); }
		});
}
<!-- setInterval("keepOnline()", 60000); -->

$(function() {
	jQuery("textarea.organic").live("keyup", function() { resizeTextarea(jQuery(this)); });
	// Comments forms
	jQuery(".commentarea form textarea").live("focus", function() {
		jQuery(this).parent("form").find("input[type=submit]").show();
		if(jQuery(this).val() == "Kommentieren...") { jQuery(this).val("").removeClass("empty"); }
	});
	jQuery(".commentarea form textarea").live("blur", function() {
		var el = jQuery(this);
		setTimeout(function() {
			el.parent("form").find("input[type=submit]").hide();
			/* if(trim(el.val()) == "") { el.val("Kommentieren...").addClass("empty").height("1.5em"); } */
		}, 200);
	});
});
