/*!
 * jquery.scrollable.autoscroll @VERSION
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/scrollable.html#autoscroll
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Launch  : July 2009
 * Date: @DATE
 * Revision: @REVISION 
 */
(function($) {		
		
	// jQuery plugin implementation
	$.fn.autoscroll = function(conf) { 
		
		var opts = {
			autoplay: true,
			interval: 1000,
			autopause: true,
			steps: 1
		};  
		
		if (typeof conf == 'number') {
			conf = {interval: conf};	
		}
		
		$.extend(opts, conf);		
		
		return this.each(function() {		
				
			var api = $(this).scrollable();	

			// interval stuff
			var timer = null, stopped = false;
	
			api.play = function() {
	
				// do not start additional timer if already exists
				if (timer) { return; }
				
				stopped = false;
				
				// construct new timer
				timer = setInterval(function() { 
					api.move(opts.steps);				
				}, opts.interval);
				
				api.move(opts.steps);
			};	

			api.pause = function() {
				timer = clearInterval(timer);	
			};
			
			// when stopped - mouseover won't restart 
			api.stop = function() {
				api.pause();
				stopped = true;	
			};
		
			/* when mouse enters, autoscroll stops */
			if (opts.autopause) {
				api.getRoot().add(api.getNaviButtons()).hover(function() {			
					api.pause();
					
				}, function() {
					if (!stopped) { api.play() };
				});
			}
			
			
			if (opts.autoplay) {
				setTimeout(function() {
					api.play(); 			
				}, opts.interval);				
			}
			
		});  
	}; 
	
})(jQuery);		

