window.addEvent("load", function(e) {
	slides();
});


function slides() {
	var thumbs = $("galerie").getElementsBySelector("ul li a");
	
	if (thumbs.length > 1) {

		var bild = $("bild");
		
		var i = 0;
		
		var images = new Array();
		thumbs.each(function(thumb) {
			var index = images.length;
			
			/* preload & bild-referenz behalten für automatische slides */
			var image = new Image();
			image.src = thumb.href;
			images[index] = image;

			/* event handler für manuellen bildwechsel */
			thumb.addEvent("click", function(e) {

				/* link nicht weiter verfolgen */
				stop_event(e);

				i = index-1;
				
				bildwechsel();
			}
			);
		});

		var timeout;
		var fade;
		var appear;

		bildwechsel = function() {
			/* ggf. alte Timer ausschalten */
			try {
				window.clearTimeout(timeout);
				fade.clearTimer();

				appear.clearTimer();
			}
			catch (e) {
			}

			i = (i+1) % (images.length);
			fade = new fx.Opacity(bild, {duration: 500, onComplete: function() {
	
				bild.src = images[i].src;
				fade.clearTimer();

				appear = new fx.Opacity(bild, {duration: 500, onComplete: function() {
						timeout = window.setTimeout(bildwechsel, 3500);
					}});
					appear.custom(0.4, 1.0);
				}});
					
			fade.custom(1.0, 0.4);
		}
		
		timeout = window.setTimeout(bildwechsel, 2500);
	}
}



function stop_event (el) {
	   if (el.preventDefault) {
 	      el.preventDefault();
 	      el.stopPropagation();
 	    } else {
 	      el.returnValue = false;
 	      el.cancelBubble = true;
 	    }
}
