Prolific.app('Slideshow', function (Slideshow) {

	var settings = {
			intervalLength: 5000,
			fadeLength: 500
		};

	$(function () {
		
		var $slider = $('#slider_block'),
			$slides = $slider.find('.slides .slide'),
			$nav = $slider.find('.nav'),
			$nav_items = $nav.find('a'),
			
			nmr_slides = $slides.length,
			
			init = function () {
			
				//Select first slide
				$slides.removeClass('selected').eq(0).addClass('selected');
				
				//Select first nav item
				$nav_items.removeClass('selected').eq(0).addClass('selected');
				
				//Bind clicks to nav items
				$nav_items.click(function () {
					Slideshow.controller.select($(this).parent().prevAll().length);
					Slideshow.timer.pause(5000);
					return false;
				});
				
				//Get slide timer going
				Slideshow.timer.go();
			};
			
		Slideshow.feature('controller', function (controller) {
			var index = 0,
			
				correctSlide = function (slide) {
					if (slide >= nmr_slides) {
						slide = slide % nmr_slides;
					} else if (slide < 0) {
						slide = nmr_slides - (slide % nmr_slides);
					}
					return slide;
				},
				fade = function (slide, opacity) {
					var opacity = opacity? opacity: 0;
					slide = correctSlide(slide >= 0? slide: index);
					$slides.eq(slide).css({
						display: 'block',
						zIndex: opacity * 100
					}).stop().fadeTo(settings.fadeLength, opacity, function () {
						if (opacity == 0) {
							$(this).css('display', 'none');
						} else if (opacity == 1) {
							$(this).css('opacity', null);
						}				
					});
				},
				fadeIn = function (slide) {
					fade(slide, 1);
				},
				fadeOut = function (slide) {
					fade(slide, 0);
				},
				fadeOutCurrent = function () {
					fadeOut(index);
				},
				
				//Privileged
				select = function (slide) {
					if (index === slide) {
						return;
					}
					
					slide = correctSlide(slide);
					fadeOutCurrent();
					fadeIn(slide);
					
					$nav_items.eq(index).removeClass('selected');
					$nav_items.eq(slide).addClass('selected');
					
					index = slide;
					
					return this;
				},
				next = function () {
					select(index + 1);
					return this;
				},
				prev = function () {
					select(index - 1);
					return this;
				};
				
			Prolific.augment.call(controller, {
				select: select,
				next: next,
				prev: prev
			});
		});
		
		Slideshow.feature('timer', function (timer) {
			var interval;
			
			timer.go = function () {
				timer.stop();
				
				interval = window.setInterval(function () {
					Slideshow.controller.next();
				}, settings.intervalLength);
				
				running = true;
				return this;
			};
			
			timer.pause = function (length) {
				var length = length || settings.intervalLength;
				
				//Stop for the time being; clears interval
				timer.stop();
				
				window.setTimeout(function () {
					timer.go();
				}, length);
				
				return this;
			};
			
			timer.stop = function () {
				window.clearInterval(interval);
				running = false;
				return this;
			};
		});
			
		
		//Start everything up	
		init();
	});
});
