function initGallery() {
	
	var divs = document.getElementsByTagName('DIV');
	
	for (var i = 0; i < divs.length; i++) {
		if ((divs[i].className == 'pfeillinks' ||  divs[i].className == 'pfeilrechts') && galleryGetGalleryForArc(divs[i])) {
			galleryShowArc(divs[i]);
			
			if (divs[i].className == 'pfeillinks') {
				divs[i].onclick = function () {
					galleryMove(galleryGetGalleryForArc(this), -1);
				}
			} else {
				divs[i].onclick = function () {
					galleryMove(galleryGetGalleryForArc(this), +1);
				}
			}
			
		}
	}
	
}

function galleryCountVisible(gallery) {
	var countTotal = 0;
	var countHidden = 0;
	var node = findFirstSub(gallery, 'A', null);
	do {
		countTotal++;
		if (matchClassName('hidden', node.className)) {
			countHidden++;
		}
		node = findNextSub(node, 'A');
	} while (node);
	return countTotal - countHidden;
}

function galleryCountHidden(gallery) {
	var countTotal = 0;
	var countHidden = 0;
	var node = findFirstSub(gallery, 'A', null);
	do {
		countTotal++;
		if (matchClassName('hidden', node.className)) {
			countHidden++;
		}
		node = findNextSub(node, 'A');
	} while (node);
	return countHidden;
}

function galleryMove(gallery, dir) {
	var countHidden = galleryCountHidden(gallery);
	var countVisible = galleryCountVisible(gallery);
	var countTotal = countVisible + countHidden;
	if(dir > 0) {
		// hide left
		if (countTotal - countHidden  > 4) {
			var node = findFirstSub(gallery, 'A');
			while (node && matchClassName('hidden',node.className)) {
				node = findNextSub(node, 'A');
			} 
			if (node) {
				addClass(node,'hidden');
			}
		}
	}
	if (dir < 0) {
		// show left
		if (countHidden > 0) {
			var node = findFirstSub(gallery, 'A');
			var lastNode = null;
			do {
				lastNode = node;
				node = findNextSub(node, 'A');
			} while (node && matchClassName('hidden',node.className));
			if (lastNode) {
				removeClass(lastNode,'hidden');
			}
		}
	}
	
}

function galleryShowArc(arc) {
			arc.style.display = 'block';
			arc.style.cursor = 'pointer';
			var app = navigator.userAgent.toLowerCase();
			if (app.indexOf("msie") == -1) {
			//	galleryGetGalleryForArc(arc).style.marginLeft = '8px';
			}
}

function galleryGetGalleryForArc(arc) {
	if (arc.parentNode && findFirstSub(arc.parentNode, 'DIV', 'galerie-content')) {
		return findFirstSub(arc.parentNode, 'DIV', 'galerie-content');
	}
	return null;
}

addInitFunction(initGallery);