/* 	Roter Rucksack Helper JS
	(C) 2010/2011 Henning Tillmann
	www.henning-tillmann.de
*/

var navstatus = 'closed';				// wird etwas angezeigt? (closed, closing, opened, opening, browser, browserclosed)
var contact_status = 'closed';			// wird das Kontaktfenster angezeigt? (closed, closing, opened, opening)
var visible_entries = ',';				// welche Eintraege aus dem RR werden angezeigt: ID,ID,ID,
var entry_count = 0;					// wieviele Eintraege werden angezeigt
var category_opened = 0;				// welche Kategorie ist geoeffnet?
var zindex = 10;						// aktueller ZIndex fuer neuen Eintrag (pro Oeffnen immer ++)
var comment_action = '';				// wird ein Kommentar angezeigt ('', showing, hiding)
var closing = false;					// wird gerade ein Eintrag geschlossen?
var draggables = new Array();			// Array mit allen verschiebbaren Elementen (Eintraege, die gerade Kommentare zeigen, koennen nicht verschoben werden)
var piwikTracker;
var touchSupported = (function () {
if (document && document.createEvent) {
  try {
	 document.createEvent('TouchEvent');
	 return true;
   } catch (e) {
	 return false;
   }
 }
})();

if (touchSupported) {
    try {
      document.write('<script type="text/javascript" src="js/webkitdragdrop.js"><\/script>');
    } catch(e) {
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = 'js/webkitdragdrop.js';
      document.getElementsByTagName('head')[0].appendChild(script);
	}
	try {
		document.getElementById('bg').style.display = 'none';
		
		document.write('<link rel="stylesheet" type="text/css" href="rr_iOS.css" />');
    } catch(e) {
		var css = document.createElement('link');
		css.rel = 'stylesheet';
		css.href = 'rr_iOS.css';
		css.type = 'text/css';
		document.getElementsByTagName('head')[0].appendChild(css);
	}
}

function init() {
	var zeigeWuerfel = true;
	
	try {
		piwikTracker = Piwik.getTracker("/piwik/piwik.php", 1);
		piwikTracker.trackPageView();
		piwikTracker.enableLinkTracking();
	} catch( err ) { }
	
	
	Effect.Appear('nav', { duration: 2.0 });
	newDraggable('nav');
	
	var h = location.hash;
	
	if (h.substr(0, 1) != '#')
		h = '#' + h;
		
	if (h.substr(1, 2) == '!/') {
		zeigeWuerfel = false;
		var e = h.split('/');
		if (isNaN(e[1])) {
			nav(e[1] + '.php');
		} else {
			nav('0');
			if (e[1] > 0) {
				loadEntry(e[1]);
				if (typeof(e[2]) != 'undefined' && e[2] == 'active')
					window.setTimeout("alert('Ihr Eintrag wurde freigeschaltet. Vielen Dank!')", 2500);
			}
		}
	} else {
		
	}
	
	if (zeigeWuerfel) {
		Effect.Appear('spdwuerfel', { duration: 0.5 });
		//showInfoPopup("Roter Rucksack on Tour: Wandertermine September / Oktober", "Die letzten beiden Wanderungen des Jahres 2011 finden am 10.09.11 in Unna-Lünern und am 07.10.11 in Bergkamen statt.", "ontour.php", "Mehr Infos hier...")
	}
}

function getSelectedTags() {
	var s = '';
	var n = 0;
	var cbe = document.forms.einwerfen.elements["tags"];

	for (i = 0; i < cbe.length; i++)
		if (cbe[i].checked) {
			n = n + Math.pow(2, cbe[i].value);
		}
	
	return n;
		
}

function sendItem() {
	new Ajax.Request('ajax-add.php', {
		method: 'post',
		parameters: { eintrag: document.getElementById('eintrag').value,
					  beschreibung: document.getElementById('beschreibung').value,
					  tags: getSelectedTags(),
					  name: document.getElementById('name').value,
					  mail: document.getElementById('mail').value,
					  strasse: document.getElementById('strasse').value,
					  plz: document.getElementById('plz').value,
					  ort: document.getElementById('ort').value },
		onSuccess: function(transport){
			var response = transport.responseText || "";
			if (response == "" || isNaN(response)) {
				alert('Es ist ein Fehler bei der AJAX-Response aufgetreten:\n' + response);
			} else {
				if (response == 0) {
					alert("Herzlichen Dank.\nSie erhalten eine E-Mail mit einem Aktivierungslink. Klicken Sie auf den Link, um Ihren Eintrag freizuschalten. Prüfen Sie ggf. Ihren Spam-Ordner, falls Sie die Mail nicht erhalten haben.");
					window.setTimeout("setStatusClosed()", 1100);
					nav('');
				} else if (response == -1) {
					alert("Es ist ein Fehler beim Datenbankzugriff aufgetreten.");
				} else {
					var s = "";
					if (response & 1)	s += "Bitte geben Sie ein, was Sie in den Roten Rucksack werfen möchten.\n";
					if (response & 2)	s += "Bitte geben Sie Ihren Namen an.\n";
					if (response & 4)	s += "Bitte geben Sie Ihre E-Mail-Adresse an.\n";
					if (response & 8)	s += "Bitte geben Sie Ihre Straße an.\n";
					if (response & 16)	s += "Bitte geben Sie Ihre Postleitzahl an.\n";
					if (response & 32)	s += "Bitte geben Sie Ihren Ort an.\n";
					if (response & 64)	s += "Bitte wählen Sie eine Kategorie aus.\n";
					alert(s);
				}
			}
		},
		onFailure: function(){ alert('Es ist ein Fehler beim AJAX-Request aufgetreten.'); }
	});		

}

function track_ajax(url, title) {
	try {
		piwikTracker.setCustomUrl('http://www.roter-rucksack.de/' + url);
		piwikTracker.setDocumentTitle(title);
		piwikTracker.trackPageView();
		piwikTracker.enableLinkTracking();
	}catch(err) {
		//Piwik funktioniert nicht
	}
}

function newDraggable(elem) {
	if (!touchSupported) {
		return new Draggable(elem);
	} else {
		return new webkit_draggable(elem);
	}
}


function loadAjax(what, append) {		// Externen Inhalt laden und ausklappen lassen
	var m = 'get';
	var p = { }
	if (what == 'add.php' && typeof(post_entry) != 'undefined' && post_entry != '') {
		m = 'post'
		p.eintrag = post_entry;
		post_entry = '';
	}
		
	new Ajax.Request(what, {
		method: m,
		parameters: p,
		onSuccess: function(transport){
			var response = transport.responseText || "";
			if (response == "") {
				alert('Es ist ein Fehler bei der AJAX-Response aufgetreten.');
			} else {
				if (append) {
					document.getElementById('content').innerHTML += response;
				} else {
					document.getElementById('content').innerHTML = response;
					Effect.BlindDown('content', { duration: 2.0 });
					window.setTimeout("animationFinished('', 'opened')", 2500);
				}
			}
		},
		onFailure: function(){ alert('Es ist ein Fehler beim AJAX-Request aufgetreten.'); animationFinished('', 'closed'); }
	});		
}

function setStatusClosed() {
	if (navstatus == 'closing') {
		navstatus = 'closed';
		document.getElementById('content').innerHTML = '';
	}
}

function toggleDescription(entry, content) {
	if (typeof(document.getElementById('entryd' + entry).innerHTML) == 'undefined' || document.getElementById('entryd' + entry).innerHTML == '') {
		document.getElementById('entryd' + entry).style.display = 'none';
		document.getElementById('entryd' + entry).innerHTML = content;
		Effect.BlindDown('entryd' + entry);
	} else if (document.getElementById('entryd' + entry).style.display == 'none') {
		Effect.BlindDown('entryd' + entry);
	} else {
		Effect.BlindUp('entryd' + entry);
	}
}

function toggleContact() {
	switch (contact_status) {
		case 'closing':
			break;
		case 'opening':
			break;
		case 'closed':
			contact_status = 'opening';
			new Effect.Morph('contact', {
				style: 'width:600px', 
				duration: 1.0
			});
			window.setTimeout("contact_status = 'opened'", 1000);
			break;
		case 'opened':
			contact_status = 'closing';
			new Effect.Morph('contact', {
				style: 'width:30px', 
				duration: 1.0
			});
			window.setTimeout("contact_status = 'closed'", 1000);
			break;
	}
}

function closeEntry(id) {
	if (!closing) {
		if (typeof(document.getElementById('entrycc' + id)) != 'undefined' && document.getElementById('entrycc' + id) != null && document.getElementById('entrycc' + id).style.display != 'none') {
			// wird Kommentar angezeigt, erst diesen schliessen, dann erneut aufrufen
			closing = true;
			hideComment(id);
			window.setTimeout('closing = false; closeEntry(' + id + ')', 2000);
		} else {
			visible_entries = visible_entries.replace(',' + id + ',', ',');
			Effect.DropOut('entry' + id);
		}
	}
}

function doIlikeThis(id) {
	if (document.cookie) {
		var c = document.cookie;
		if (c.indexOf("like" + id + "=1") >= 0) {
			return true;
		} else {
			return false;

		}
	} else {
		return false;

	}
}


function toggleLike(id) {
	alreadylike = doIlikeThis(id);
	var e = new Date();
	var expires = e.getTime() + (365 * 24 * 60 * 60 * 1000);
	e.setTime(expires);

	if (alreadylike) {
		document.cookie = "like" + id + "=0; expires=" + e.toGMTString();
		document.getElementById('entryb' + id).className = 'like';
		new Effect.Highlight('entry' + id, { startcolor: '#33cc33', endcolor: '#ffffff', restorecolor: '#ffffff' });
	} else {
		if (document.cookie) {
			var c = document.cookie;
			c = "like" + id + "=1; " + c;
			document.cookie = c + "; expires=" + e.toGMTString();
		} else {
			document.cookie = "like" + id + "=1; expires=" + e.toGMTString();
		}
		document.getElementById('entryb' + id).className = 'dislike';
		new Effect.Highlight('entry' + id, { startcolor: '#ffffff', endcolor: '#33cc33', restorecolor: '#22cc22' });
	}
	
	if (alreadylike)
		alreadylike = "1";
	else
		alreadylike = "0";
	
	new Ajax.Request('ajax-like.php', {
		method: 'post',
		parameters: { entry: id, dislike: alreadylike },
		onSuccess: function(transport){
			var response = transport.responseText || "";
			if (response == "") {
				alert('Es ist ein Fehler bei der AJAX-Response aufgetreten.');
			} else if (!isNaN(response)) {
				document.getElementById('entryls' + id).innerHTML = showLikes(response);
				document.getElementById('likecount' + id).innerHTML = response;
			} else {
				alert('Es ist ein Fehler bei der AJAX-Response aufgetreten:\n' + response);
			}
		},
		onFailure: function(){ alert('Es ist ein Fehler beim AJAX-Request aufgetreten.'); }
	});	
	
}

function displayCategories(c) {
	if (c == 5) {	// da nur 4 Kategorien existieren ist bei 5 Schluss. Warte aber noch Animation ab
		window.setTimeout("animationFinished('', 'browser')", 1000);
	} else {
		Effect.Appear('cat' + c, { duration: 1.0 });
		window.setTimeout('displayCategories(' + (c + 1) + ')', 500);
	}
}

function nav(what) {
	if (visible_entries == "0,") {
		visible_entries = ",";
		Effect.Fade('entry0', { duration: 0.5 });
	}
	
	switch(navstatus) {
		case 'closed':
			navstatus = 'opening';
			if (document.getElementById('spdwuerfel').style.display != 'none')
				Effect.DropOut('spdwuerfel');
				
			if (what == 0) {	// Kategorien anzeigen
				displayCategories(1);	// 1 = Beginn
				location.hash = '!/' + what;
				track_ajax('0/', 'Kategorien');
			} else {
				document.getElementById('content').style.top = '70px';		// Ruecksetzen der Position, fuer iOS-Geraete wichtig
				document.getElementById('content').style.right = '120px';
				loadAjax(what, false);
				location.hash = '!/' + what.substr(0, what.length - 4);
				track_ajax(what, what);
			}
			break;
		case 'opening':
			break;		// verwerfen
		case 'closing':
			break;		// verwerfen
		case 'opened':
			navstatus = 'closing';
			Effect.BlindUp('content', { duration: 1.0 });
			window.setTimeout("animationFinished('" + what + "', 'closed')", 1300);
			break;
		case 'browser':		// browser = Kategorien und Eintraege anzeigen
			navstatus = 'closing';
			if (category_opened != 0) {
				categoryMenu(category_opened);	// offene Kategorie schliessen
				window.setTimeout("animationFinished('" + what + "', 'browserclosed')", 2000);
			} else {
				navstatus = 'browserclosed';
				nav(what);
			}
			break;
		case 'browserclosed':	// Kategorien und Eintraege schliessen
			navstatus = 'closing';
				
			var s = visible_entries.substr(1, visible_entries.length - 1);
			visible_entries = ',';
			
			if (s != "") {
				var e = s.split(',');
				for (var i = 0; i < e.length - 1; i++) {
					Effect.Fade('entry' + e[i], { duration: 0.5 });
				}
			}
			
			Effect.Fade('cat4', { duration: 0.5 });
			window.setTimeout("Effect.Fade('cat3', { duration: 0.5 })", 500);
			window.setTimeout("Effect.Fade('cat2', { duration: 0.5 })", 1000);
			window.setTimeout("Effect.Fade('cat1', { duration: 0.5 })", 1500);
			window.setTimeout("animationFinished('" + what + "', 'closed')", 2200);
			
			break;
			
	}
	

}



function animationFinished(what, newstatus) {
	navstatus = newstatus;
	if (what != '')
		nav(what);
	else if (what == '' && newstatus == 'opened')
		newDraggable('content')
}


function showLikes(c) {
	var likes;
	if (c == 0) {
		likes = "Niemand stimmt dem zu.";
	} else if (c == 1) {
		likes = "Eine Person stimmt dem zu.";
	} else {
		likes = c + " Personen stimmen dem zu.";
	}
	return likes;
}

function showComment(id, c) {
	if (comment_action == '') {
		draggables[id].destroy();
		draggables[id] = null;
		document.getElementById('entry' + id).style.cursor = 'auto';
		c = "<b>Kommentar von Oliver Kaczmarek:</b><br />" + c;
		document.getElementById('entrycc' + id).innerHTML = c;
		comment_action = 'showing';
		Effect.Appear('entryc' + id);
		window.setTimeout("Effect.Appear('entrycl" + id + "')", 1000);
		window.setTimeout("Effect.Appear('entrycc" + id + "')", 1000);
		window.setTimeout("comment_action = ''", 2000);
		track_ajax(id + '/comment', 'Kommentar: ' + document.getElementById('entryh' + id));
	}
}

function hideComment(id) {
	if (comment_action == '') {
		comment_action = 'hiding';
		Effect.Fade('entrycl' + id);
		Effect.Fade('entrycc' + id);
		window.setTimeout("Effect.Fade('entryc" + id + "')", 1000);
		window.setTimeout("document.getElementById('entrycc" + id + "').innerHTML = '';", 1500);
		window.setTimeout("comment_action = ''", 2000);
		draggables[id] = newDraggable('entry' + id);
		document.getElementById('entry' + id).style.cursor = 'move';
	}
}

function showInfoPopup(title, msg, linknav, linktext) {
	visible_entries += "0,";
	window.scrollTo(0, 0);					
	var width = Math.floor(title.length * 2.5 + 300);
	if (window.innerWidth) {
		var randX = Math.floor((Math.random() * (window.innerWidth - width - 50) + 10));
		var randY = Math.floor((Math.random() * (window.innerHeight - 300) + 100));
	} else if (document.body && document.documentElement.clientWidth) { // IE
		var randX = Math.floor((Math.random() * (document.documentElement.clientWidth - width - 50) + 10));
		var randY = Math.floor((Math.random() * (document.documentElement.clientHeight - 300) + 100));						
	} else {
		var randX = Math.floor((Math.random() * (800 - width - 50) + 10));
		var randY = Math.floor((Math.random() * (600 - 200) + 100));
	}

	clickhandler = '';
	
	zindex++;

	var elem = Builder.node('div',
						{ id: 'entry0',
						  className: 'entryinfobox',
						  onclick: "updateZ('entry0');",
						  style: 'display:none; background-color: #e2001a; background-image: url(img/black.gif); background-position: bottom left; background-repeat: repeat-x; position:absolute; padding:10px; padding-top:2px; cursor:move; z-index:' + zindex + '; top:' + randY + 'px; left:' + randX + 'px; width:' + width + 'px; box-shadow: 0 0 20px black; -moz-box-shadow: 0 0 20px black; -webkit-box-shadow: 0 0 20px black; border-radius: 20px; -moz-border-radius: 20px;'
						  },
						[ Builder.node('a', { className: 'cross', onclick: 'closeEntry(0)' }, ' '),
						  Builder.node('h2', { id: 'entryh0', className: 'entry', style: 'color: white' }, title),
						  Builder.node('p', { id: 'entrya0', className: 'author', style: 'color: white' }, msg),
						  Builder.node('div', {
						  id: 'entryk0', className: 'commentOpener', style: 'text-align: center; width: 100%; color:white; margin-top:15px; margin-bottom:-4px; font: 10pt Tahoma, Geneva, sans-serif; font-weight: bold; cursor: pointer', onclick: "nav('" + linknav + "')" }, linktext)					  
						] );	

	document.getElementById('body').appendChild(elem);
	draggables[0] = newDraggable('entry0');
	Effect.BlindDown('entry0');
}

function loadEntry(forceload) {
	if (typeof(forceload) == 'undefined')
		forceload = 0;
	
	var sort = 'rand' // nur in voriger version: document.getElementById('sort').value; jetzt ueberfluessig, da immer forceload verwendet wird
	
	new Ajax.Request('ajax-getentry.php', {
		method: 'post',
		parameters: { exclude: visible_entries, load: forceload, order: sort },
		onSuccess: function(transport){
			var response = transport.responseText || "";
			
			if (response == "0") {
				alert('Es sind keine weiteren Einträge im Roten Rucksack.');
			} else if (response == "-1") {
				alert('Es ist ein Datenbankfehler aufgetreten');
			} else if (response.substr(0, 1) == '|') {
				response = response.substr(1, response.length - 1);
				x = response.split('|');

				window.scrollTo(0, 0);					
				visible_entries += x[0] + ',';
		
				var width = Math.floor(x[1].length * 2.5 + 300);
				if (window.innerWidth) {
					var randX = Math.floor((Math.random() * (window.innerWidth - width - 50) + 10));
					var randY = Math.floor((Math.random() * (window.innerHeight - 300) + 100));
				} else if (document.body && document.documentElement.clientWidth) { // IE
					var randX = Math.floor((Math.random() * (document.documentElement.clientWidth - width - 50) + 10));
					var randY = Math.floor((Math.random() * (document.documentElement.clientHeight - 300) + 100));						
				} else {
					var randX = Math.floor((Math.random() * (800 - width - 50) + 10));
					var randY = Math.floor((Math.random() * (600 - 200) + 100));
				}
				
				
				clickhandler = '';
				headerclass = '';
				
				if (x[2] != '') {
					clickhandler = "toggleDescription(" + x[0] + ", '" + x[2].replace("'", "\'") + "')";
					headerclass = ' clickable';
				}
				
				likes = showLikes(x[5]);
				document.getElementById('likecount' + x[0]).innerHTML = x[5];
				entry_count = x[7];
				
				
				if (doIlikeThis(x[0])) {
					like_it = 1;
					like_class = 'dislike';
				} else {
					like_it = 0;
					like_class = 'like';
				}
				
				zindex++;
				var tweet_text = x[1];
				if (tweet_text.length >= 110)
					tweet_text = tweet_text.substr(0, 100) + '...';
				// Builder.node('a', { className: 'twitter-share-button', href: 'http://twitter.com/share?url=http%3A%2F%2Froter-rucksack.de%2F' + x[0] + '&amp;via=RoterRucksack&amp;text=' + escape(tweet_text) }, 'Twittern' )
				if (x[6] == '*') {
					var elem = Builder.node('div',
										{ id: 'entry' + x[0],
										  className: 'entry' + like_class,
										  onclick: "updateZ('entry" + x[0] + "'); location.hash = '!/" + x[0] + "'",
										  style: 'display:none; position:absolute; padding:10px; padding-top:2px; cursor:move; z-index:' + zindex + '; top:' + randY + 'px; left:' + randX + 'px; width:' + width + 'px; box-shadow: 0 0 20px black; -moz-box-shadow: 0 0 20px black; -webkit-box-shadow: 0 0 20px black; border-radius: 20px; -moz-border-radius: 20px;' },
										[ Builder.node('a', { className: 'cross', onclick: 'closeEntry(' + x[0] + ')' }, ' '),
										  Builder.node('h2', { id: 'entryh' + x[0], className: 'entry' + headerclass, onclick: clickhandler }, x[1]),
										  Builder.node('div', { id: 'entryd' + x[0], style: 'dispay:none', className: 'description' }, ''),
										  Builder.node('p', { id: 'entrya' + x[0], className: 'author' }, x[3] + ' aus ' + x[4]),
										  Builder.node('div', { id: 'entryl' + x[0], className: 'likebox' },
											[ Builder.node('a', { id: 'entryb' + x[0], className: like_class , onclick: 'toggleLike(' + x[0] + ')' }, ' '),
											  Builder.node('div', { className: 'like', id: 'entryls' + x[0] }, likes)
											])
										]);				
				} else {
					x[6] = x[6].replace('"', "&quot;");
					var elem = Builder.node('div',
										{ id: 'entry' + x[0],
										  className: 'entry' + like_class,
										  onclick: "updateZ('entry" + x[0] + "'); location.hash = '!/" + x[0] + "'",
										  style: 'display:none; background-color: white; background-image: url(img/black.gif); background-position: bottom left; background-repeat: repeat-x; position:absolute; padding:10px; padding-top:2px; cursor:move; z-index:' + zindex + '; top:' + randY + 'px; left:' + randX + 'px; width:' + width + 'px; box-shadow: 0 0 20px black; -moz-box-shadow: 0 0 20px black; -webkit-box-shadow: 0 0 20px black; border-radius: 20px; -moz-border-radius: 20px;' },
										[ Builder.node('div', { id: 'entryc' + x[0], className: 'comment', style: 'display:none; position:absolute; left:0px; top:0px; width:100%; height:100%; background-color: #000000; color: #ffffff; border-radius: 20px; -moz-border-radius: 20px;' }, 
										    [ Builder.node('div', { id: 'entrycc' + x[0], onmousemove: 'return true;', className: 'commentContent', style: 'display:none; position:absolute;  left: 10px; right: 10px; top: 40px; bottom:10px; overflow:auto;' }, '') ]),
										  Builder.node('a', { id: 'entrycl' + x[0], className: 'left', onclick: 'hideComment(' + x[0] + ')', style: 'display:none' }, ' '),
										  Builder.node('a', { className: 'cross', onclick: 'closeEntry(' + x[0] + ')' }, ' '),
										  Builder.node('h2', { id: 'entryh' + x[0], className: 'entry' + headerclass, onclick: clickhandler }, x[1]),
										  Builder.node('div', { id: 'entryd' + x[0], style: 'dispay:none', className: 'description' }, ''),
										  Builder.node('p', { id: 'entrya' + x[0], className: 'author' }, x[3] + ' aus ' + x[4]),
										  Builder.node('div', { id: 'entryl' + x[0], className: 'likebox' },
											[ Builder.node('a', { id: 'entryb' + x[0], className: like_class , onclick: 'toggleLike(' + x[0] + ')' }, ' '),
											  Builder.node('div', { className: 'like', id: 'entryls' + x[0] }, likes) ]),
										  Builder.node('div', { id: 'entryk' + x[0], className: 'commentOpener', style: 'text-align: center; width: 100%; color:white; margin-top:15px; margin-bottom:-4px; font: 10pt Tahoma, Geneva, sans-serif; font-weight: bold; cursor: pointer', onclick: "showComment(" + x[0] + ", '" + x[6] + "')" }, 'Kommentar anzeigen')]
										);
				}
				document.getElementById('body').appendChild(elem);
				draggables[x[0]] = newDraggable('entry' + x[0]);
				Effect.BlindDown('entry' + x[0]);
				location.hash = '!/' + x[0];
				track_ajax(x[0], 'Eintrag: ' + x[1]);
			} else {
				alert('Es ist ein Fehler bei der AJAX-Response aufgetreten:\n' + response);
			}
			

		},
		onFailure: function(){ alert('Es ist ein Fehler beim AJAX-Request aufgetreten.'); }
	});		
}

function updateZ(obj) {	// ZIndex beim Klick aktualisieren
	zindex++;
	document.getElementById(obj).style.zIndex = zindex;
}

function categoryMenu(id) {		// Klick auf Kategorie
	if (category_opened == id) {
		if (document.getElementById('catcontent' + id).style.display == 'none') {	// Schliessen Teil 2
			new Effect.Morph('cat' + category_opened, {
				style: 'height:25px', 
				duration: 0.8
			});
			category_opened = 32767;		// so kann zwischenscheitlich nicht erneut geschlossen werden
			window.setTimeout('category_opened = 0;', 1000);
		} else {	// Schliessen Teil 1, erst Inhalt verschwinden lassen
			toggleCatContent(id);
			window.setTimeout('categoryMenu(' + id + ')', 1000);
		}
	} else if (category_opened == 0) {		// oeffnen
		category_opened = -1;
		new Effect.Morph('cat' + id, {
			style: 'height:350px', 
			duration: 0.8
		});
		window.setTimeout('toggleCatContent(' + id + ')', 1000);
	} else if (category_opened > 0 && category_opened < 32767) {	// schliessen
		if (document.getElementById('catcontent' + category_opened).style.display == 'none') { // Schließen Teil 2
			new Effect.Morph('cat' + category_opened, {
				style: 'height:25px', 
				duration: 0.8
			});
			category_opened = 32767;
			window.setTimeout('categoryMenu(' + (id * -1) + ')', 1000);
		} else { // Schliessen Teil 1
			toggleCatContent(category_opened);
			window.setTimeout('categoryMenu(' + id + ')', 1000);
		}
	} else {							// 32767, schliesst
		if (id < 0) {
			category_opened = 0;
			categoryMenu(id * -1)
		}
	}
}

function toggleCatContent(id) {
	if(document.getElementById('catcontent' + id).style.display == 'none') {
		Effect.Appear('catcontent' + id, { duration: 0.8 });
		window.setTimeout('category_opened = ' + id, 1000);
	} else {
		Effect.Fade('catcontent' + id, { duration: 0.8 });
	}
}

function sendContact() {
	var spam_a = document.getElementById('spam_a').value;
	var spam_b = document.getElementById('spam_b').value;
	var contactname = document.getElementById('contactname').value;
	var contactmail = document.getElementById('contactmail').value;
	var contacttext = document.getElementById('contacttext').value;
	var spam_c = prompt("Geben Sie zum Schutz vor Spam bitte das Ergebnis von " + spam_a + " + " + spam_b + " ein.", "");

	new Ajax.Request('ajax-contact.php', {
		method: 'post',
		parameters: { msg: contacttext,
					  name: contactname,
					  mail: contactmail,
					  spama: spam_a,
					  spamb: spam_b,
					  spamc: spam_c },
		onSuccess: function(transport){
			var response = transport.responseText || "";
			if (response == "" || isNaN(response)) {
				alert('Es ist ein Fehler bei der AJAX-Response aufgetreten:\n' + response);
			} else {
				if (response == 0) {
					alert("Herzlichen Dank.\nSie erhalten in den nächsten Tagen eine Antwort.");
					document.getElementById('contactname').value = '';
					document.getElementById('contactmail').value = '';
					document.getElementById('contacttext').value = '';
					toggleContact()
				} else if (response == -1) {
					alert("Es ist ein Fehler beim Mailversand aufgetreten");
				} else {
					var s = "";
					if (response & 1)	s += "Bitte geben Sie eine Nachricht ein.\n";
					if (response & 2)	s += "Bitte geben Sie Ihren Namen an.\n";
					if (response & 4)	s += "Bitte geben Sie Ihre E-Mail-Adresse an.\n";
					if (response & 8)	s += "Bitte geben Sie das richtige Ergebnis zum Spamschutz an.\n";
					alert(s);
				}
			}
		},
		onFailure: function(){ alert('Es ist ein Fehler beim AJAX-Request aufgetreten.'); }
	});	
}
