/**
 * Horizontaler Scroller mit Auswahlmöglichkeit.
 * Benoetigt: Prototype, Scriptaculous 
 */

var ScrollerMitAuswahl = Class.create({

	initialize: function(container, args) {
		this.container = $(container);
		if (!this.container) 
			throw('The specified DOM element does not exist: '+ container);
		this.elemente  = this.container.childElements();

		this.options =	args
			  ||	{ elementeProSeite: 1,
				  afterScroll: function() {}
				};
		this.aktuell     = 0;
		this.aktiverSlot = 0;
	},

	aktivesElement: function() {
		var pos = Math.min(
				this.aktuell+this.aktiverSlot,
				this.elemente.length-1
		);
		return this.elemente[pos];
	},


	aktiviereSlot: function(pos) {
		this.aktiverSlot = pos % this.options.elementeProSeite;
	},
	
	startScrollLinks:  function() {},
	startScrollRechts: function() {},
	stopScrolling:     function() {},

	scroll : function(dx, dy) {
		new Effect.Move(this.container, {
			x: dx,
			y: dy,
			transition:  Effect.Transitions.sinoidal,
			duration:    0.75,
			queue:       'end',
			afterFinish: this.options.afterScroll
		});
	},
	
	oben: function() {
		if (! this.kannLinksScrollen())
			return;
		var vorheriges = Math.max(
			this.aktuell - this.options.elementeProSeite,
			0);
		var dx	= this.elemente[this.aktuell].cumulativeOffset().top
			- this.elemente[vorheriges  ].cumulativeOffset().top;
		this.aktuell = vorheriges;
		this.scroll( 0 ,dx);
	},

	unten: function() {
		if (! this.kannRechtsScrollen())
			return;
		var naechstes = Math.min(
			this.aktuell + this.options.elementeProSeite,
			this.elemente.length-1
		);
		if (naechstes >= this.elemente.length)
			return;
		var dx	= this.elemente[this.aktuell].cumulativeOffset().top
			- this.elemente[naechstes   ].cumulativeOffset().top;
		this.aktuell = naechstes;
		this.scroll( 0, dx );
	},

	links: function() {
		if (! this.kannLinksScrollen())
			return;
		var vorheriges = Math.max(
			this.aktuell - this.options.elementeProSeite,
			0);
		var dx	= this.elemente[this.aktuell].cumulativeOffset().left
			- this.elemente[vorheriges  ].cumulativeOffset().left;
		this.aktuell = vorheriges;
		this.scroll( dx, 0 );
	},

	rechts: function() {
		if (! this.kannRechtsScrollen())
			return;
		var naechstes = Math.min(
			this.aktuell + this.options.elementeProSeite,
			this.elemente.length-1
		);
		if (naechstes >= this.elemente.length)
			return;
		var dx	= this.elemente[this.aktuell].cumulativeOffset().left
			- this.elemente[naechstes   ].cumulativeOffset().left;
		this.aktuell = naechstes;
		this.scroll( dx, 0 );
	},

	kannRechtsScrollen: function() {
		return (this.aktuell + this.options.elementeProSeite) < this.elemente.length;
	},

	kannLinksScrollen: function() {
		return this.aktuell >= this.options.elementeProSeite;
	}
});

