/*****
 * Slides through all Items on the left side
 */
var TeaserSlideshow = {
	slides: [],	// the sources to the images
	activeSlide: null,
	duration: 800,
	delay: 8000,
	activeEffects: [],

	init: function() {
		this.activeSlide = null;
		this.stopActiveEffects();
		this.slides = $$('.cal-teaser-item');

		// only a single image available, not slideshow
		this.showNow(0);
		if (this.slides.length > 1) {
			if (window.ie6) try {document.execCommand("BackgroundImageCache", false, true);} catch(e){};
			this.next.delay(this.delay, this);
		}
	},
	
	showNow: function(i) {
		if (this.activeSlide) {
			$(this.activeSlide).setStyles({ opacity: 0 });
		}
		$(this.slides[i]).setStyles({
			opacity: 1,
			display: 'block'
		});
		this.activeSlide = this.slides[i];
	},

	next: function(i) {
		if (typeof i == 'undefined') {
			this.stopActiveEffects();
			var i = this.slides.indexOf(this.activeSlide)+1;
		}
			// reset
		if (this.slides.length == i || !i) {
			i = 0;
		}

		var nextSlide = this.slides[i];

			// fade out active slide
		if (this.activeSlide) {
			this.activeEffects.fadeOut = new Fx.Tween(this.activeSlide, {
				duration: this.duration,
				link: 'chain',
				transition: Fx.Transitions.Circ.easeOut
			});
			this.activeEffects.fadeOut.start('opacity', 0);
		}
			// fade in new slide
		if (nextSlide) {
			nextSlide.setStyles({
				display: 'block',
				opacity: 0
			});
			this.activeEffects.fadeIn = new Fx.Tween(nextSlide, {
				duration: this.duration,
				link: 'chain',
				transition: Fx.Transitions.Circ.easeOut
			});
			this.activeEffects.fadeIn.addEvent('onComplete', function() {
				this.activeSlide = nextSlide;
					// start next transition
				this.next.delay(this.delay, this);
			}.bind(this));
			this.activeEffects.fadeIn.start('opacity', 1);
		}
	},

	stopActiveEffects: function() {
		this.activeEffects.each(function(fx) {
			fx.cancel();
		});
	}
};

window.addEvent('domready', TeaserSlideshow.init.bind(TeaserSlideshow));


var DynamicFamilyAttendants = {
	selector: 'familienangehoerige',
	
	init: function() {
		$(this.selector).addEvent('change', function() {
			var val = $(this.selector).get('value');
			val = parseInt(val);
			var next = $(this.selector).getParent().getNext();
			for (var i = 0; i < 9; i++) {
				if (i >= val) {
					next.addClass('cal-anmeldung-field-hidden');
				} else {
					next.removeClass('cal-anmeldung-field-hidden');
				}
				next = next.getNext();
			}
		}.bind(this));
	}

};

window.addEvent('domready', DynamicFamilyAttendants.init.bind(DynamicFamilyAttendants));
