(function(){

	$.scrollable = function(options){

		var	settings = {
				'parent'		: '.scrollable',
				'item'			: '.scrollable-item',
				'prev'			: '.scrollable-prev',
				'next'			: '.scrollable-next',
				'timing'		: 100
			},
		
			set_buttons = function(obj){
				var $this		= $(obj),
					num_items	= $this.attr('data-scrollable-items-visible');

				($this.find(settings.item).not(':visible').length > 0) ?
					$this.find(settings.prev).show():
					$this.find(settings.prev).hide();

				(num_items && ($this.find(settings.item).filter(':visible').length <= num_items)) ?
					$this.find(settings.next).hide():
					$this.find(settings.next).show();
			};

		$.extend(settings, options);

		$(settings.parent).each(function(){
			set_buttons(this);
		});

		$(document)
			.delegate(settings.prev, 'mousedown', function(){

				var $this	= $(this),
					$parent	= $this.closest(settings.parent),
					$first	= $parent.find(settings.item).not(':visible').filter(':last');

				$first.stop(true, true).show(settings.timing, function(){
					set_buttons($parent);
				});
			})
			.delegate(settings.next, 'mousedown', function(){

				var $this	= $(this),
					$parent	= $this.closest(settings.parent),
					$first	= $parent.find(settings.item).filter(':visible').filter(':first');

				$first.stop(true, true).hide(settings.timing, function(){
					set_buttons($parent);
				});
			});
	};

})(jQuery);
