// (c) 2001 - 2007, EIKONA AG, it.x informationssysteme gmbh, Alle Rechte vorbehalten.

if(!NewsScroll) {

	var NewsScroll = Class.create({
		
		initialize: function(eContainer, options) {
			
			this.options = Object.extend({
				'width': 615,
				'height': 100,
				'startAt': 0,
				'step': 206,
				'elementsPerScroll': 1,
				'childClassName': 'zeile',
				'onStart': Prototype.emptyFunction,
				'onScroll': Prototype.emptyFunction,
				'afterScroll': Prototype.emptyFunction,
				'duration': 0.15
				}, options || {});
			
			this.container = $(eContainer) || new Element('div');
			this.content = this.container.childElements()[0];
			this.children = $$('#'.concat(this.container.identify(), ' .', this.options.childClassName));
			
			this.scrollHandleLeft = new Element('div', { 'class': 'scroll-handle-left' });
			this.scrollHandleRight = new Element('div', { 'class': 'scroll-handle-right' });
			
			this.scrollHandleLeft.observe('click', this.scrollLeft.bindAsEventListener(this));
			this.scrollHandleRight.observe('click', this.scrollRight.bindAsEventListener(this));
			
			this.container.insert({ 'before': this.scrollHandleLeft });
			this.container.insert({ 'before': this.scrollHandleRight });
			
			this.options.onStart();
			
			this.actualElementIndex = this.options.startAt % this.children.length;
			this.scroll();
		},
		
		
		update: function(){
			if (this.needScrollHandleLeft())
				this.scrollHandleLeft.show();
			
			else
				this.scrollHandleLeft.hide();
			
			if (this.needScrollHandleRight())
				this.scrollHandleRight.show();
			
			else
				this.scrollHandleRight.hide();
		},
		
		
		scroll: function() {
			if (this.effect)
				this.effect.cancel();
			
			var left = - this.actualElementIndex * this.options.step;
			
			this.effect = new Effect.Morph(this.content, {
				'duration': this.options.duration,
				'style': 'left: '.concat(left, 'px'),
				'afterFinish': function(){ this.update(); }.bind(this)
				});
		},
		
		
		scrollLeft: function(event) {
			Event.stop(event);
			
			this.actualElementIndex = Math.max(0, this.actualElementIndex - this.options.elementsPerScroll);
			this.scroll();
		},
		
		
		scrollRight: function(event) {
			Event.stop(event);
			
			this.actualElementIndex = Math.min(this.children.length, this.actualElementIndex + this.options.elementsPerScroll);
			this.scroll();
		},
		
		
		needScrollHandleLeft: function() {
			return this.actualElementIndex > 0;
		},
		
		
		needScrollHandleRight: function() {
			return this.children.length > this.actualElementIndex + this.options.elementsPerScroll + this.options.width / this.options.step;
		}
	});
}
