var ddDynamicListScroller = new Class({
	
	initialize: function(scrollerId, linksId, maxItems) {

		
		this.scroller = $(scrollerId);
		this.links = $(linksId);
		this.maxItems = maxItems;
		this.currentIndex = 0;

		/* fetch elements */
		this.elements = $$('#' + scrollerId + ' div.comment');
		
		if (this.elements.length > this.maxItems) {
			containerHeight = 0;
			for (c=0; c<this.maxItems; c++) {
				containerHeight += this.elements[c].getSize().y+12;
			}
			this.scroller.setStyle('height', containerHeight+'px');
			this.prevContainer = new Element('div', {
				'class': 'buttonMagenta buttonMagentaUp'
				});
			this.prevLink = new Element('a', {
			    'href': 'javascript:void(0);',
			    'html': 'neuere Kommentare'
			    });
			    
			this.nextContainer = new Element('div', {
				'class': 'buttonMagenta buttonMagentaDown',
			    'styles': {
			    	'float': 'right'
			    	}				
				}); 
			this.nextLink = new Element('a', {
			    'href': 'javascript:void(0);',
			    'html': 'ältere Kommentare'
			    });	
			
			this.nextLink.inject(this.nextContainer);
			this.nextContainer.inject(this.links);	
			this.nextLink.addEvent('click', this.scrollDown.bind(this));		    
			this.prevLink.inject(this.prevContainer);
			this.prevContainer.inject(this.links);
			this.prevLink.addEvent('click', this.scrollUp.bind(this));
			
		}
	
		
	},
	
	scrollUp: function() {
		if (this.currentIndex) {
			this.currentIndex--;
			var scrollFx = new Fx.Scroll(this.scroller).toElement(this.elements[this.currentIndex]);
		}		
	},
	
	scrollDown: function() {
		if (this.currentIndex+this.maxItems<this.elements.length) {
			this.currentIndex++;
			var scrollFx = new Fx.Scroll(this.scroller).toElement(this.elements[this.currentIndex]);
		}
	}
	
	
});

window.addEvent('domready', initDynamicListScroller);
function initDynamicListScroller() {
	commentsList = new ddDynamicListScroller('commentsScrollContainer', 'commentsScrollLinks', 5);
	
}
