/*********************************************
	 * Modified autoscroll.js (jquerytools) RBN
	 *********************************************/

	var t = $.tools.scrollable;
	t.fiautoscroll = {
		conf: {
			autoplay: true,
			interval: 3000,
			autopause: true
		}
	};
	// jQuery plugin implementation
	$.fn.fiautoscroll = function(conf) {
	
		if (typeof conf == 'number') {
			conf = {interval: conf};
		}
		
		var opts = $.extend({}, t.fiautoscroll.conf, conf), ret;
		
		this.each(function() {
		
			var api = $(this).data("scrollable");
			if (api) { ret = api; }
			
			// interval stuff
			var timer, stopped = true;				
							
			api.play = function() {
				// do not start additional timer if already exists
				if (timer) { return; }
				
				stopped = false;
					
				//////////////////////////////////
				
				var imageCounter = 1;
			
				// construct new timer
				timer = setInterval(function() {
					
						var domIndex = api.getIndex();						
						var domItems = api.getItems();
						var domItemsSize = api.getItems().size();
						
						var domImages = $(domItems).eq(domIndex).find("img");
						var domImagesSize = $(domItems).eq(domIndex).find("img").size();
						
						if(imageCounter<domImagesSize)
						{
							$(domImages).eq(imageCounter).click();
							imageCounter++;
						}
						else
						{							
							imageCounter = 0;
																
							var newDomIndex = (domIndex < domItemsSize-1) ? domIndex+1 :0 ;
							domImages = $(domItems).eq(newDomIndex).find("img");
							$(domImages).eq(imageCounter).click();
							
							imageCounter++;				
							api.next();
						}
					
					}, opts.interval);
				
				//////////////////////////////////
			};
			
			api.pause = function() {
				timer = clearInterval(timer);
			};
			
			// when stopped - mouseover won't restart
			api.stop = function() {
				api.pause();
				stopped = true;
			};
			
			/* when mouse enters, fiautoscroll stops */
			if (opts.autopause) {
				api.getRoot().add(api.getNaviButtons()).hover(api.pause, api.play);
			}
			
			if (opts.autoplay) {
				api.play();
			}
		
		});
		
		return opts.api ? ret : this;
	
	}; 
