/**
 * @author jallen
 */
;(function($){
	$.fn.extend({
		svScroll: function(options) {
			var defaults = {
				'windowClass' : 'window'
				,'trackClass': 'track'
				,'itemClass': 'item'
				,'prvClass': 'prv'
				,'nxtClass': 'nxt'
				,'orientation': 'left' // 'left' or 'top' indicate relevant anchor
				,'speed': '400'
				,'items': 1 // number of items shown in the window
			};
			
			var options = $.extend(defaults, options);
			

			var _scrollPrev = function(el,options) {
				var o = options;
				// only go if active
				if (!$(el).hasClass('active')) return false;

				// unbind while doing the work
				$(el).unbind('click').removeClass('active');
				$(el).siblings('.' + o.nxtClass).unbind('click').removeClass('active');
				var win = $(el).siblings('.' + o.windowClass);
				var winSize = o.orientation == 'top'
						? win.innerHeight()
						: win.innerWidth();
				var itemCount = win.find('.' + o.itemClass).length;
				var itemSize = o.orientation == 'top'
						? win.find('.' + o.itemClass).outerHeight(true)
						: win.find('.' + o.itemClass).outerWidth(true);
				var maxPos = -(itemCount * itemSize) + itemSize * Math.floor(winSize / itemSize);
				var curPos = o.orientation == 'top'
						? win.find('.' + o.trackClass).position().top
						: win.find('.' + o.trackClass).position().left;
				var newPos = (curPos + (itemSize * o.items)) > 0
						? 0
						: (curPos + (itemSize * o.items));
				var animation = o.orientation == 'top'
						? {top: newPos+"px"}
						: {left: newPos+"px"};
				win.find('.' + o.trackClass).animate(animation,o.speed);
				
				// rebind if it should be bound
				$(el).bind('click',function(){
					return _scrollPrev(this,o);
				}).siblings('.' + o.nxtClass).bind('click',function(){
					return _scrollNext(this,o);
				});
				if (newPos < 0) $(el).addClass('active');
				if (newPos > maxPos) $(el).siblings('.' + o.nxtClass).addClass('active');
				return false;
			};

			var _scrollNext = function(el,options) {
				var o = options;
				// only go if active
				if (!$(el).hasClass('active')) return false;

				// unbind while doing the work
				$(el).unbind('click').removeClass('active');
				$(el).siblings('.' + o.prvClass).unbind('click').removeClass('active');
				var win = $(el).siblings('.' + o.windowClass);
				var winSize = o.orientation == 'top'
						? win.innerHeight()
						: win.innerWidth();
				var itemCount = win.find('.' + o.itemClass).length;
				var itemSize = o.orientation == 'top'
						? win.find('.' + o.itemClass).outerHeight(true)
						: win.find('.' + o.itemClass).outerWidth(true);
				var maxPos = -(itemCount * itemSize) + itemSize * Math.floor(winSize / itemSize);
				var curPos = o.orientation == 'top'
						? win.find('.' + o.trackClass).position().top
						: win.find('.' + o.trackClass).position().left;
				var newPos = (curPos - (itemSize * o.items)) < maxPos
						? maxPos
						: (curPos - (itemSize * o.items));
				var animation = o.orientation == 'top'
						? {top: newPos+"px"}
						: {left: newPos+"px"};
				win.find('.' + o.trackClass).animate(animation,o.speed);
				
				// rebind if it should be bound
				$(el).bind('click',function(){
					return _scrollNext(this,o);
				}).siblings('.' + o.prvClass).bind('click',function(){
					return _scrollPrev(this,o);
				});
				if (newPos > maxPos) $(el).addClass('active');
				if (newPos < 0) $(el).siblings('.' + o.prvClass).addClass('active');
				return false;
			};

			return this.each(function() {
				var o = options;
				
				// figure out initial state information
				var winSize = o.orientation == 'top'
						? $(this).find('.'+o.windowClass).innerHeight()
						: $(this).find('.'+o.windowClass).innerWidth();
				var itemCount = $(this).find('.' + o.itemClass).length;
				var itemSize = o.orientation == 'top'
						? $(this).find('.' + o.itemClass).outerHeight(true)
						: $(this).find('.' + o.itemClass).outerWidth(true);
				var maxPos = -(itemCount * itemSize) + itemSize * Math.floor(winSize / itemSize);
				var curPos = o.orientation == 'top'
						? $(this).find('.' + o.trackClass).position().top
						: $(this).find('.' + o.trackClass).position().left;

				// previous button
				var prvBttn = $(this).find('a.' + o.prvClass).show().bind('click',function() {
						return _scrollPrev(this,o);
					});
				if (curPos < 0) {
					prvBttn.addClass('active');
				}
				
				// next button
				var nxtBttn = $(this).find('a.' + o.nxtClass).show().bind('click',function() {
						return _scrollNext(this,o);
					});
				if (curPos > maxPos) {
					nxtBttn.addClass('active');
				}
				return this;
			});
			
		}
	});
})(jQuery);
