	(function($){
		$.fn.thumbSlider = function(options){
			var options = $.extend({}, $.fn.thumbSlider.defaults, options);
			
			return this.each(function(){
				new thumbSlider($(this), options);
			});
		};
		
		$.fn.thumbSlider.defaults = {
			numViewItems: 		5,
			duration:			900,
			easing:				'swing'
		};
		
		// Private functions.
		var thumbSlider = function (element, options)
		{
			var ul = element.find('ul')
			.css({
				'width': '10000em',
				'position': 'relative'
			}).wrap('<div></div>');
			
			var lis = ul.find('li').css({
				'float': 'left'
			});
			
			//set width variables
			var item = element.find('li:first');
			var itemWidth = item.outerWidth() + parseInt(item.css('margin-left')) + parseInt(item.css('margin-right'));
			var viewWidth = itemWidth * options.numViewItems;
			var lastViewWidth = parseInt(((lis.length / options.numViewItems) - parseInt((lis.length / options.numViewItems))) * viewWidth);
			if(lastViewWidth == 0)
				lastViewWidth = viewWidth;
			
			var overflow = ul.parent().css({
				'width': viewWidth,
				'overflow': 'hidden',
				'position': 'relative',
				'float': 'left'
			});
			//set position variables
			var position = 0;
			var maxPos = parseInt(lis.length / options.numViewItems);
			var remainder = lis.length % options.numViewItems;
			if(remainder == 0)
				maxPos--;
			//set animation variables
			var isAnimating = false;
			var animationOver = function(){
				isAnimating = false;
			}
			//create move object
			var move = {
				getNext: function(){
					var toLeft = 0;
					if((position + 1) > maxPos)
						position = 0;
					else if (remainder && position == (maxPos - 1)){
						position++;
						toLeft = '-=' + lastViewWidth;
					}
					else{
						position++;
						toLeft = '-=' + viewWidth;
					}
					return toLeft;	
				},
				getBack: function(){
					var left  = -parseFloat(ul[0].offsetLeft);
					//alert('vw: ' + viewWidth + '\nlvw: ' + lastViewWidth + '\nmp: ' + maxPos);
					var toLeft = '-=' + (((viewWidth * maxPos) + lastViewWidth) -  viewWidth);
					if(position == 0)
						position = maxPos;
					else if (remainder && position == 1 && left == lastViewWidth){
						position--;
						toLeft = '+=' + lastViewWidth;
					}
					else{
						position--;
						toLeft = '+=' + viewWidth;
					}
					return toLeft;	
				}
			};
			
			
			var back = $('<a href="javascript:;" class="arrow">back</a>').click(function(){
					if(isAnimating) return false;
					isAnimating = true;
					ul.animate({left: move.getBack()}, {duration: options.duration, easing: options.easing, complete: animationOver});
			});
			var next = $('<a href="javascript:;" class="arrow">next</a>').click(function(){
					if(isAnimating) return false;
					isAnimating = true;
					ul.animate({left: move.getNext()}, {duration: options.duration, easing: options.easing, complete: animationOver});
			});
			
			overflow.before(back);
			overflow.after(next);
			
			element.find('a.arrow').css({
				'float': 'left'
			});
		};
	})(jQuery);
