(function($) {		
	var t = $.tools.scrollable; 
	t.plugins = t.plugins || {};
	t.plugins.autoscroll = {
		version: '1.0.1',
		conf: {
			autoplay: true,
			interval: 3000,
			autopause: true,
			steps: 1,
			api: false
		}
	};	
	$.fn.autoscroll = function(conf) { 
		if (typeof conf == 'number') {
			conf = {interval: conf};	
		}
		var opts = $.extend({}, t.plugins.autoscroll.conf), ret;
		$.extend(opts, conf);   	
		this.each(function() {		
			var api = $(this).scrollable();			
			if (api) { ret = api; }
			var timer, hoverTimer, stopped = true;
			api.play = function() {
				if (timer) { return; }
				stopped = false;
				timer = setInterval(function() { 
					api.move(opts.steps);				
				}, opts.interval);
				api.move(opts.steps);
			};	
			api.pause = function() {
				timer = clearInterval(timer);	
			};
			api.stop = function() {
				api.pause();
				stopped = true;	
			};
			if (opts.autopause) {
				api.getRoot().add(api.getNaviButtons()).hover(function() {			
					api.pause();
					clearInterval(hoverTimer);
				}, function() {
					if (!stopped) {						
						hoverTimer = setTimeout(api.play, opts.interval);						
					}
				});
			}			
			if (opts.autoplay) {
				setTimeout(api.play, opts.interval);				
			}
		});
		return opts.api ? ret : this;
	}; 
})(jQuery);
