/*
 * Class Scroller
 */
 
var VerticalScroller = function(items, itemsInView, scrollAmount, container, scrollSpeed) {
	var self = this;
	var scrollIndex = 0;
	var minScrollIndex = 0;
	var scrollConstant = scrollAmount;
	var numItems = items;
	var numItemsInView = itemsInView;
	var speed = isNaN(scrollSpeed)?1000:scrollSpeed;

	this.init = function() {
		if (numItems > numItemsInView) {
			$('.scroll-down', container).removeClass('disabled');
			$('.scroll-down .arrow', container).removeClass('disabled');
			$('.scroll-down', container).click(function() {
				self.scrollDown();
				return false;
			});
			$('.scroll-up', container).click(function() { 
				self.scrollUp(); 
				return false;
			});
		}
		minScrollIndex = 0 - (numItems - numItemsInView);
	}
	
	this.scrollUp = function () {
		var newScrollIndex = scrollIndex + numItemsInView;
		if (newScrollIndex < 0) {
			scrollIndex = newScrollIndex;
			var anim = true;
		} else if (newScrollIndex >= 0) {
			scrollIndex = 0;
			var anim = true;
		}
		if (anim) {
			var newTop = scrollIndex * scrollConstant;		
			$('.scroll-content', container).stop(true).animate({top: newTop}, speed);
			$('.scroll-down', container).removeClass('disabled');
			$('.scroll-down .arrow', container).removeClass('disabled');
			if (newTop == 0) {
				$('.scroll-up', container).addClass('disabled');
				$('.scroll-up .arrow', container).addClass('disabled');
			}
		}
		return false;
	}
	
	this.scrollDown = function() {
		var newScrollIndex = scrollIndex - numItemsInView;
		if (newScrollIndex >= minScrollIndex) {
			scrollIndex = newScrollIndex;
			var anim = true;
		} else if (scrollIndex != minScrollIndex) {
			scrollIndex = minScrollIndex;
			var anim = true;
		}
		if (anim) {		
			var newTop = scrollIndex * scrollConstant;
			$('.scroll-content', container).stop(true).animate({top: newTop}, speed);
			$('.scroll-up', container).removeClass('disabled');
			$('.scroll-up .arrow', container).removeClass('disabled');
			if (newTop == ((numItems - numItemsInView) * - scrollConstant)) {
				$('.scroll-down', container).addClass('disabled');
				$('.scroll-down .arrow', container).addClass('disabled');
			}
		}
		return false;
	}	
	this.init();
}
