/**
 * @author jallen
 */
;(function($){
	$.fn.extend({
		svScroll: function(options) {
			var defaults = {
				'trackClass': 'track'
				,'itemClass': 'item'
				,'prvClass': 'prv'
				,'nxtClass': 'nxt'
				,'orientation': 'left' // 'left' or 'top' indicate relevant anchor
				,'speed': '400'
			};
			
			var options = $.extend(defaults, options);
			
			return this.each(function() {
				var o = options;
				
				// previous button
				$(this).find('a.' + o.prvClass).click(function() {
					var trackSize = o.orientation == 'top'
							? $(this).siblings().find('.' + o.trackClass).height()
							: $(this).siblings().find('.' + o.trackClass).width();
					var itemSize = o.orientation == 'top'
							? $(this).siblings().find('.' + o.itemClass).height()
							: $(this).siblings().find('.' + o.itemClass).width();
					var maxPos = -(trackSize - itemSize);
					var curPos = o.orientation == 'top'
							? $(this).siblings().find('.' + o.trackClass).position().top
							: $(this).siblings().find('.' + o.trackClass).position().left;
					var newPos = curPos + itemSize;
					var animation = o.orientation == 'top'
							? {top: newPos+"px"}
							: {left: newPos+"px"};
					$(this).siblings().find('.' + o.trackClass).animate(animation,o.speed);
					if (newPos >= 0) $(this).hide();
					if (newPos > maxPos) $(this).siblings('.' + o.nxtClass).show();
					return false;
				});
				
				// next button
				$(this).find('a.' + o.nxtClass).click(function() {
					var trackSize = o.orientation == 'top'
							? $(this).siblings().find('.' + o.trackClass).height()
							: $(this).siblings().find('.' + o.trackClass).width();
					var itemSize = o.orientation == 'top'
							? $(this).siblings().find('.' + o.itemClass).height()
							: $(this).siblings().find('.' + o.itemClass).width();
					var maxPos = -(trackSize - itemSize);
					var curPos = o.orientation == 'top'
							? $(this).siblings().find('.' + o.trackClass).position().top
							: $(this).siblings().find('.' + o.trackClass).position().left;
					var newPos = curPos - itemSize;
					var animation = o.orientation == 'top'
							? {top: newPos+"px"}
							: {left: newPos+"px"};
					$(this).siblings().find('.' + o.trackClass).animate(animation,o.speed);
					if (newPos < 0) $(this).siblings('.' + o.prvClass).show();
					if (newPos <= maxPos) $(this).hide();
					return false;
				});
				return this;
			});
			
		}
	});
})(jQuery);
