(function($){

	$.fn.fpScroller = function() {

		return this.each(function() {
		
			var wrapper = $(this).find('.scroller-wrapper');
			var scroller = wrapper.find('.scroller');
			var boxes = scroller.find('.box');
			var arrowLeft = $(this).find('.arrow-left');
			var arrowRight = $(this).find('.arrow-right');
			var timer = 0;
			
			var stepSize = boxes.eq(0).outerWidth(true);
			var wrapperSize = wrapper.outerWidth();
			var scrollerSize = stepSize * boxes.length;
			
			var max = wrapperSize - scrollerSize;
			var min = 0;
			var pos = 0;
			
			scroller.width(scrollerSize);
			
			//startTimer();
			
			if (scrollerSize > wrapperSize)
			{
				//il pannello è più largo della visuale disponibile, quindi scrollo...
				arrowLeft.click(function() {
					moveScroller(1);
					startTimer();
					return false;
				});
				
				arrowRight.click(function() {
					moveScroller(-1);
					startTimer();
					return false;
				});
			}
			
			function moveScroller(dir)
			{
				pos = pos + dir * stepSize;
				
				if (pos > min)
				{
					//mi sto muovendo a sinistra e ho finito i box, quindi salto alla fine
					pos = wrapperSize - scrollerSize;
				}
				
				if (pos < max)
				{
					//mi sto muovendo a destra e ho finito i box, quindi salto all'inizio
					pos = min;
				}
				scroller.animate({left:pos}, 'slow');
			}
			
			function startTimer()
			{
				clearInterval(timer);
				timer = setInterval(function() {
					moveScroller(-1);
				}, 4000);
			}
			
			
		});
	
	}
	
	$.fn.fpDropdown = function() {
	
		return this.each(function() {
		
			$(this).find('li ul').hide();
			
			var lis = $(this).find('li:has(>ul)');

			lis.hover(function() {
				$(this).find('>ul').show();
				$(this).find('>a').addClass('open');
			}, function() {
				$(this).find('>ul').hide();
				$(this).find('>a').removeClass('open');
			});
		
		});
	
	}
	
	$.fn.fpSlideshow = function() {
	
		return this.each(function() {
		
			var imgs = $(this).find('img');
			var current = imgs.eq(0);
			
			imgs.hide();	
			current.show();
			
			setInterval(function() {
			
				next = current.next();
				
				if (next.length == 0)
				{
					next = imgs.eq(0);
				}
				
				current.fadeOut(1000);
				next.fadeIn(1000, function() {
					current = next;				
				});
			
			}, 3500);
		
		});
	
	}

})(jQuery)
