(function($) {

	var settings = {
		'boxWidth' : 250,
		'brake' : 40,
		'sauiTimer' : null
	};

	var methods = {
		init : function( options ) {

	       return this.each(function(){
	    	   
	    	// If options exist, lets merge them
			// with our default settings
			if (options) {
				$.extend(settings, options);
			}
	         
	         var $this = $(this),
	             data = $this.data('sauiScrollable'),
	             meta = $.metadata.get(this),
	             tooltip = $('<div />', {
	               text : $this.attr('title')
	             });
	         
	         // If the plugin hasn't been initialized yet
	         if ( ! data ) {
	           $(this).data('sauiScrollable', {
	               target : $this,
	               meta : meta,
	               tooltip : tooltip
	           });

	         }

	         $.get(meta.url, function(products) {
	        	 var items = [];

	        	  $.each(products, function(key, val) {
	        	    items.push('<li><a href="'+ val.url +'" title="'+ val.name +'"><img src="'+ val.image +'" alt="'+ val.name +'" height="100" width="100" /><br />'+ val.name +'<br />'+ val.price + '</a></li>');
	        	  });
	        	  
	        	  $this.html('<ul id="productsBarItems">'+ items.join('') +'</ul>').wrap('<div id="productsBarContainer"></div>').addClass('saui-scrollable');
		          $('div#productsBarContainer').
		          		append('<a href="#" id="avScrollLeft" class="avScrollButton"></a><a href="#" id="avScrollRight" class="avScrollButton"></a>').
		          		prepend('<h2>'+ meta.name +'</h2>');
		          
		         // $this.bind('resize.tooltip', methods.reposition);
		          

		      	$this.css({overflow: 'hidden'});
		      	var wrapperWidth = $this.width();
		      	
		      	var numberOfItems = $this.find('li').length;
		      	var itemWidth = $this.find('li:first-child').outerWidth(true);	//width including margins, padding, border, etc. //$("ul#productsBarItems").children(":first-child").outerWidth();
		      	var maxScrollLeft = (itemWidth*numberOfItems)-wrapperWidth+20;
		      	$("ul#productsBarItems").css("width", (itemWidth*numberOfItems)+"px");
		      	
		      	
		      	$this.bind('mouseover.sauiScrollable', function(e) {
		    		if(($this.scrollLeft()>=0) && ($this.scrollLeft()<=maxScrollLeft)) {
		    			methods.move(e, $this, wrapperWidth);
		    		}
		    	});
		      	
		      	$this.bind('mousemove.sauiScrollable', function(e) {
		    		if(($this.scrollLeft()>=0) && ($this.scrollLeft()<=maxScrollLeft)) {
		    			methods.move(e, $this, wrapperWidth);
		    		}
		    	});
		      	
		      	$this.bind('mouseout.sauiScrollable', function(e) {
		      		clearInterval(settings.sauiTimer);
		    	});

		    	$('a#avScrollLeft').click(function() {
		    		methods.moveleft(wrapperWidth);
		    		return false;
		    	});

		    	$('a#avScrollRight').click(function() {
		    		methods.moveright(wrapperWidth);
		    		return false;
		    	});
		      	

	        });

	         
	       });
	     },
	     destroy : function( ) {

	       return this.each(function(){

	         var $this = $(this),
	             data = $this.data('sauiScrollable');

	         // Namespacing FTW
	         $this.unbind('.sauiScrollable');
	         data.sauiScrollable.remove();
	         $this.removeData('sauiScrollable');

	       });

	    },
	    move : function(e, $this, wrapperWidth) {
	    	clearInterval(settings.sauiTimer);
            data = $this.data('sauiScrollable');
	    	
	    	var relPos= e.pageX-$this.offset().left;
	    	if (relPos>(wrapperWidth-settings.boxWidth)) {
	    		var acceleration = (((relPos-(settings.boxWidth+100))/settings.brake)^2);
	    		settings.sauiTimer = setInterval(function() {methods.moveright(acceleration)}, 13); 
	    	
	    	} else if(relPos<settings.boxWidth) {
	    		var acceleration = (((wrapperWidth-relPos-(settings.boxWidth+100))/settings.brake)^2);
	    		settings.sauiTimer = setInterval(function() {methods.moveleft(acceleration)}, 13); 
	    	}
	    },
		moveleft : function(acceleration) {
			var div = $('div#productsBar');
	    	div.scrollLeft(div.scrollLeft()-acceleration);
		},
		moveright : function(acceleration) {
			var div = $('div#productsBar');
	    	div.scrollLeft(div.scrollLeft()+acceleration);
		}
	};

	$.fn.sauiScrollable = function(method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(
					arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on sauiScrollable');
		}

	};

})(jQuery);
