var teaserSwitcher = Class.create({
duration: 0.8,
timeDuration: 5000,
showTimed: false,
initialize: function(e, a) {
		this.box = e;
		this.active = typeof a == 'number' ? a : 0;
		this.pagechooserLink = [];
		this.elements = [];
		return this;
},
init: function(){
		var self = this;
		var li = this.box.select('li');
		this.maxElements = li.length;
		try{
			if(this.active < 0)
				throw "this.active < 0";
			if(this.active > this.maxElements-1)
				throw "this.active > " + (this.maxElements-1);
		}catch(e){
			this.active = 0;
		}
		for(var o = 0; o<this.maxElements; o++)
		{
			var liA = li[o].down('a');
			this.elements.push({li: li[o], href: liA.href});
			liA.hide();

			li[o].setStyle({position: 'absolute', top: 0, left: (this.box.getWidth()*o) - (this.active * this.box.getWidth()) + 'px'});
			
			var a = Builder.node('a', {href: '#'});
			if ( o == this.active )
			{
				a.addClassName('aktiv');
			}
			a.onclick = function(){self.show(this); self.showTimed = false; return false};
			a.i = o;
			this.pagechooserLink.push(a);
		}
		this.box.setStyle({height: li[this.active].getHeight() + 'px', overflow: 'hidden'});
		var navLinkTxt = window.location.href.indexOf('de') > -1 ? 'mehr erfahren' : 'Find out more';
		this.navLink = Builder.node('a', {'class': 'arrow', href: this.elements[this.active].href}, navLinkTxt);
		this.nav = Builder.node('div', {'class': 'nav'}, [
			Builder.node('div', {'class': 'pagechooser'}, [this.pagechooserLink]),
			this.navLink
		]);
		this.box.insert({after: this.nav});
		return this;
	},
timedShow: function(){
	var self = this;
	this.showTimed = true;
	var next = this.active + 1;
	try{
		if(next < 0)
			throw "next < 0";
		if(next > this.maxElements-1)
			throw "next > " + (this.maxElements-1);
	}catch(e){
		next = 0;
	}
	this.timer = window.setTimeout(function(){self.show({i: next});}, this.timeDuration);
	return this;
},
show: function(e){
		var self = this;
		var oldActive = this.active;
		this.active = e.i;
		this.pagechooserLink[oldActive].removeClassName('aktiv');
		this.pagechooserLink[this.active].addClassName('aktiv');
		var d = Math.round(this.duration*(oldActive - this.active).abs() * 10)/10;
		
		var myEffects = [];
		
		for(var i=0; i<this.maxElements; i++)
		{
			var li = this.elements[i].li;
			myEffects.push(new Effect.Move(li, {sync: true, x: (li.getWidth()*i) - (self.active * li.getWidth()) , y: 0, mode: 'absolute'}));
		}
		new Effect.Parallel(myEffects, { 
				duration: d,
				transition: Effect.Transitions.linear,
				afterFinish: function(){self.__afterFinishShow()}
			}
		);
		this.navLink.href = this.elements[this.active].href;
	},
__afterFinishShow: function(){
	if(this.showTimed == true){
		this.timedShow();
	}
}
});
