/**
 *
 * Simple scroller
 * Jamie Norton 2009
 *
 ***/
(function($) {
	
	$.fn.thumbscroll = function(options) {
		return this.each(function() {
			new $ts(this, options);
		});
	};
	
	var defaults = {}
		
	$.thumbscroll = function(element, options) {
		
		this.container		= null;
		this.clip			= null;
		this.list			= null;
		this.numItems		= null;
		this.itemOuterWidth	= null;
		this.totalWidth		= null;
		this.numberShowing	= null;
		this.time			= null;
		this.rightButton	= null;
		this.leftButton		= null;
		
		this.options    = $.extend({}, defaults, options || {});
		
		if (element.nodeName == 'UL' || element.nodeName == 'OL') {
			
			this.list = $(element);
			this.container = this.list.parent();
			
			// hardcode the width for the mo
			if($('li', this.list).length <= 5) { // don't bother
				return;
			}
			
			this.setup();
			
		}
		
		var self = this;
		
		this.rightButton = $('<div>').attr({
			id: 'scroll-right'
		})
		.addClass('scroller')
		.hover(
			function(){
				self.scrollRight();
			},
			function(){
				self.stop();
			}
		);
		
		this.leftButton = $('<div>').attr({
			id: 'scroll-left'
		})
		.addClass('scroller')
		.hover(
			function(){
				self.scrollLeft();
			},
			function(){
				self.stop();
			}
		);
		
		this.container.append(this.rightButton);
		this.container.append(this.leftButton);
		
		$('li:first', this.list).addClass('current');
		
		this.nextButton = $('<a>').attr({
			href: $('li:eq(1) a', this.list).attr('href'),
			id: 'thumb-next'
		})
		.addClass('next-prev')
		.text('Next')
		.click(function(){
			$("#large-image").attr('src', $(this).attr('href'));
			
			current = $('li.current', element).next('li:first');
			if(!current.length) {
				current = $('li:first', element);
			}
			$('li', element).removeClass('current');
			current.addClass('current');
			
			next = current.next('li:first');
			if(!next.length) {
				next = $('li:first', element);
			}
			$(this).attr('href', $('a', next).attr('href') );
			
			return false;
		});
		
		this.prevButton = $('<a>').attr({
			href: $('li:last a', this.list).attr('href'),
			id: 'thumb-prev'
		})
		.addClass('next-prev')
		.text('Previous')
		.click(function(){
			$("#large-image").attr('src', $(this).attr('href'));
			
			current = $('li.current', element).prev();//'li:first'
			if(!current.length) {
				current = $('li:last', element);
			}
			$('li', element).removeClass('current');
			current.addClass('current');
			
			prev = current.prev('li:first');
			if(!prev.length) {
				prev = $('li:last', element);
			}
			$(this).attr('href', $('a', prev).attr('href') );
			
			return false;
		});
		
		this.container.append(this.prevButton);
		this.container.append(this.nextButton);
	
	};
	
	
	// Create shortcut for internal use
	var $ts = $.thumbscroll;
	
	$ts.fn = $ts.prototype = {
		thumbscroll: '0.0.1'
	};

	$ts.fn.extend = $ts.extend = $.extend;
	
    $ts.fn.extend({
	
		setup: function(){
			
			this.numItems = this.list.children('li').length;
			
			if(this.numItems > 0) {
				
				this.itemOuterWidth = this.list.children('li:first').outerWidth(true);
				
				this.totalWidth = this.numItems * this.itemOuterWidth;
				
				// 150 msec each item
				this.time = this.numItems * 150;
				
				// Set width
				this.list.css({width: this.totalWidth+'px'});
				
				// Total number showing
				this.numberShowing = parseInt(this.container.outerWidth(true) / this.itemOuterWidth);
				
				// Set container width
				this.container.css({width: (this.numberShowing*this.itemOuterWidth)+'px'});
			}
			
		},
	
		scrollRight: function(){
			
			left = -(this.totalWidth - (this.numberShowing * this.itemOuterWidth));
			
			this.list.animate({left: left+"px"}, this.time, "linear");
			
		},
		
		scrollLeft: function(){
			
			this.list.animate({left: "0px"}, this.time, "linear");
			
		},
		
		stop: function() {
			
			this.list.stop();
			
		}
	
	});
	
})(jQuery);