
	(function($){
	
		JohnsonsBabyUX.scrollers = {

			//_________________________________________________________________
				
			instances: [],
			
			//_________________________________________________________________
			
			defaults: {
			},
			
			//_________________________________________________________________
			
			instance: function(id) {
				
				var instance = null;
				var instances = this.instances;
				for(var i = 0; i < instances.length; i++) {
					instance = instances[i];
					if(instance.id == id) {
						break;
					}
				}
				
				
				return instance;
			},
			
			//_________________________________________________________________
			
			factory: function(element, options) {
				// Persist this scope.
				var scrollers = this;
				
				var scroller = function(element, options) {
					// Persist this scope.
					var scroller = this;
					
					this.options = $.extend({},
						JohnsonsBabyUX.scrollers.defaults,
						options);
					
					var value = 0;
					
					this.container = element.css({
						
					});
					
					this.thumb = $(".thumb", this.container).css({
						position: 'relative',
						left: '0px'
					});
					
					this.dragging = false;
					
					//_____________________________________________________________
					
					this.container.mousedown(function(event) {
						if(scroller.dragging == false) {
							scroller.dragging = true;
							if($.isFunction(scroller.options.onStart)) {
								scroller.options.onStart.apply(scroller, [scroller]);
							}
						}
						return false;
					});
					
					//_____________________________________________________________
					
					$(document).mouseup(function() {
						if(scroller.dragging) {
							scroller.dragging = false;
							if($.isFunction(scroller.options.onStop)) {
								scroller.options.onStop.apply(scroller, [scroller]);
							}
						}
						return false;
					});
					
					//_____________________________________________________________
					
					$(document).mousemove(function(event) {
						if(scroller.dragging) {
							var offset = scroller.container.offset();
							var X = event.pageX - offset.left;
							move(X - (scroller.thumb.width() / 2));
							if($.isFunction(scroller.options.onDrag)) {
								scroller.options.onDrag.apply(scroller, [scroller]);
							}
						}
					});
					
					//_____________________________________________________________
					
					this.value = function() {
						var maximum = scroller.container
						.width() - scroller.thumb.width();
						if(arguments.length == 0) {
							var left = parseInt(scroller.thumb.css('left'));
							return Math.floor((left / maximum) * 100);
						} else {
							if(scroller.dragging == false) {
								var value = arguments[0];
								var left = maximum * (value / 100);
								move(left);
							}
						}
					}
					
					var move = function(position) {
						var offset = scroller.container.offset();
						
						if(position < 0) {
							position = 0;
						} else {
							var maximum = scroller.container
							.width() - scroller.thumb.width();
							if(position > maximum ) {
								position = maximum;
							}
						}
						
						scroller.thumb.css({
							left: position + 'px'
						});
					}
					
					//_____________________________________________________________
					
					// Return the instance.
					return this;
					
					//_____________________________________________________________
				}
				
				// Create a new scroller instance.
				var instance = new scroller(element, options);
				
				// Add the instance to the instances array.
				this.instances.push(instance);
				
				// Return the new instance.
				return instance;
			}
		}
		
		//_____________________________________________________________________
		
		$.fn.JohnsonsBabyUXScroller = function(options) {
			return this.each(function() {
				// Create a new carousel instance.
				JohnsonsBabyUX.scrollers.factory(
					$(this), options);
			});
		}
		
	})(jQuery);
