function scrollToItem(prevButton, nextButton, direction, scroller, context) {
	
	if (direction == 'next') {
		prevButton.show();
		context.scrollForward();
		if (context.canScrollForward()) {
			nextButton.show();
		} else {
			nextButton.hide();
		}
	}
	
	if (direction == 'prev') {
		nextButton.show();
		context.scrollBackward();
		if (context.canScrollBackward()) {
			prevButton.show();
		} else {
			prevButton.hide();
		}
	}
	
	tippelement = '#items li:eq(' + ( context.getCurrItem() - 1 ) + ')';
	scroller.stop().scrollTo( tippelement, 600 );
	
	return false;
}

var scrollerFunction = function(index,elem) {
	var prevButton = $("#button-prev", elem);
	var nextButton = $("#button-next", elem);
	var scroller   = $("#scroller"   , elem);
	var liCount    = $("li"          , scroller).size();
	
	prevButton.hide();
	if ( liCount <= 4 ) {
		nextButton.hide();
	} else {
		nextButton.show();
	}
	
	var context = {
		currItem : 1,
		maxItem : $("li" , scroller).size(),
		getCurrItem : function() {
			return this.currItem;
		},
		canScrollForward : function() {
			return (this.currItem <= this.maxItem - 4);
		},
		canScrollBackward : function() {
			return (this.currItem > 4);
		},
		scrollForward : function() {
			this.currItem += 4;
		},
		scrollBackward : function() {
			this.currItem -= 4;
		}
	}
	
	prevButton.mouseup( function () { scrollToItem(prevButton, nextButton, "prev", scroller, context ); } );
	nextButton.mouseup( function () { scrollToItem(prevButton, nextButton, "next", scroller, context ); } ); 
}

$(".persons").each(scrollerFunction);
$(".immo-tipps").each(scrollerFunction);
$(".team").each(scrollerFunction);

