/************************************************************************
 *	OnLoad-Aufrufe														*
 *																		*
 *	Funktionen, die bei Laden des Dokuments aufgerufen werden sollen,	*
 *	mit   initFunc.push(<Funktionsname>);   hinzufügen!					*
 ************************************************************************/
var initFunc = [];

window.onload = function() {
	while (initFunc.length) {
		initFunc.pop()();
	}
	};

/********************
 *	Menü-Animation	*
 ********************/
/*
 *	Kapselt alle nötigen Informationen zu einem Untermenü
 */
function Bundle(aElem, ulElem, fullH) {
	this.a = aElem;
	this.ul = ulElem;
	this.fullHeight = fullH;
	this.currHeight = 0;
	
	/*
	 *	Ändert die Höhe des Untermenüs um $by.
	 *	Es gilt immer 0 <= Höhe <= $fullHeight.
	 *	Gibt FALSE zurück, wenn eine der Grenzen erreicht ist.
	 */
	this.change = function(by) {
		var moveOn = !!by;
		var limit = (by < 0) ? 0 : this.fullHeight;
		
		this.currHeight += by;
		
		if ((!!limit) == (this.currHeight > limit)) {
			this.currHeight = limit;
			moveOn = false;
		}
		
		this.ul.style.height = this.currHeight + 'px';
		
		return moveOn;
	}
}

/*
 *	Singleton zur Initialisierung der Menü-Animation
 */
var navOverlay = new function() {
	var motion = false;
	var step = 10;	// Schrittweite pro 25-stel Sekunde in Pixel
	var sublist = [];
	var active = -1;
	
	/*
	 *	Verändert die Höhe aller nötigen Untermenüs um $step
	 */
	function move() {
		var moved = false;
		
		for (var i = 0; i < sublist.length; i++) {
			var curr = sublist[i];
			var by = 0;
			
			if (i == active && curr.currHeight < curr.fullHeight) {
				by = step;
			}
			else if (i != active && curr.currHeight > 0) {
				by = -step;
			}
			
			moved |= curr.change(by);
		}
		
		if (!moved) {
			window.clearInterval(motion);
			motion = false;
		}
	}
	
	/*
	 *	Startet die Animation
	 */
	function change() {
		active = (this.lang != active) ? this.lang : -1;
		motion = window.setInterval(move, 40);
		return false;
	}
	
	/*
	 *	Initialisiert die Menü-Animation
	 */
	this.init = function() {	
		var nav = document.getElementById('navigation').firstChild;
		
		while (nav) {
			if (nav.nodeName == 'LI') {
				var a = nav.firstChild;
				
				while (a.nodeName != 'A') { a = a.nextSibling; }
				
				var list = a.nextSibling;
				
				while (list && list.nodeName != 'UL') { list = list.nextSibling; }
				
				if (list) {
					if (nav.className.indexOf('active') < 0) {
						list.style.display = 'block';
						list.style.overflow = 'hidden';
						a.lang = sublist.length;
						sublist.push(new Bundle(a, list, list.clientHeight));
						list.style.height = '0';				
					}
					else {
						a.lang = 'de';
					}
					a.onclick = change;
				}
			}
			nav = nav.nextSibling;
		}
	};
	
	initFunc.push(this.init);
}();

/***********************
 *  Galerie-Animation  *
 ***********************/

var gallOverlay = function() {
	var galleryImg = [];
	var active = 0;
	var gall, img, cont;
	var step = 8;
	var imgWidth = 100;	// nicht dynamisch, wg. IE8
	var diff, pos, opac;
	var motion = false;
	
	function fadeOut() {
		opac = Math.max(0, opac - step);
		
		gall.style.backgroundPosition = (pos + diff * (100 - opac) / 100) + 'px 0';
	
		if (opac) {
			img.style.opacity = opac / 100;
			img.style.filter = 'alpha(opacity=' + opac + ')';
		}
		else {
			window.clearInterval(motion);
			motion = false;
	
			img.src = galleryImg[ active ].src;
			img.alt = galleryImg[ active ].alt;
			img.title = galleryImg[ active ].alt;
			img.style.opacity = 1;
			img.style.filter = 'alpha(opacity=100)';
		}
	}
	
	function showImg() {
		if (this.lang != active) {		
			pos = active * imgWidth;
			active = this.lang;
			diff = active * imgWidth - pos;
			cont.style.backgroundImage = 'url("' + galleryImg[ active ].src + '")';
			opac = 100;
			motion = window.setInterval(fadeOut, 40);
		
			return false;
		}
	}
	
	this.init = function() {
		var a;
	
		cont = document.getElementById('image'); if (!cont) { return; }
		img = cont.firstChild;
	
		while (img && img.nodeName != 'IMG') { img = img.nextSibling; }
		if (!img) { return; }
	
		gall = img.nextSibling;
	
		while (gall && gall.nodeName != 'DIV') { gall = gall.nextSibling; }
		if (!gall) { return; }

//		gall.style.display = 'block';
		a = gall.firstChild;
	
		while (a) {
			if (a.nodeName == 'IMG') {
				var i = new Image();

				a.lang = galleryImg.length;
				a.onclick = showImg;

				i.src = a.src.split('\?')[ 0 ];
				i.alt = a.alt;
				galleryImg.push(i);
	
//				if (!imgWidth) {
//					imgWidth = a.clientWidth;
//				}
			}
			a = a.nextSibling;
		}
		
		if (galleryImg.length > 1) { gall.style.display = 'block'; }
	};
	
	initFunc.push(this.init);
}();













