DF.featuredSlideshow = {
	speed: 6500,
	noOfSlides: 0,
	activeSlide: 0,
	
	$featuredProjects: {},
	$slides: {},

	/*
	 * Load the slides, and
	 * Attach events to allow skipping to specific slides.
	 */
	init: function() {
		this.$featuredProjects = $('.featured_projects');
		this.$slides = this.$featuredProjects.find('.featured_image');
		this.noOfSlides = this.$slides.length;
		this.$skipLinks = $('.featured_skip');
		this.$projectTitle = $('.project_title');
		
		this.$slides.click($.proxy(this, 'goToProject'));
		this.$skipLinks.click($.proxy(this, 'skip'));
		
		this.start();
	},
	
	/* 
	 * Start the slideshow
	 */
	start: function() {
		clearInterval( this.loop );
		this.loop = setInterval($.proxy(this, 'cycle'), this.speed);
	},
	
	/*
	 * Show the specified slide, and highlight its related link.
	 */
	show: function(slide) {
		this.activeSlide = slide;
	
		var $slide = this.$slides.filter(':eq('+slide+')');
			$slide.fadeIn(1750);
			
		var $link = this.$skipLinks.filter(':eq('+slide+')');
			$link.addClass('on');
			
		var title = $slide.prev('.featured_title');
		this.$projectTitle.text( title.text() );
	},
	
	/*
	 * Fade out the specified slide
	 */
	hide: function(slide) {
		var $slide = this.$slides.filter(':eq('+slide+')')
			$slide.fadeOut(2500);
			
		this.$skipLinks.removeClass('on');
	},

	/*
	 * Cycle through each slide.
	 * Go to the beginning once we reach the end,
	 * otherwise keep going...
	 */
	cycle: function() {
		this.hide( this.activeSlide );
		
		if ((this.activeSlide + 1) >= this.noOfSlides) {
			this.activeSlide = 0;
		} else {
			this.activeSlide++;
		}
		
		this.show( this.activeSlide );
	},
	
	/*
	 * Click handler for a skip link.
	 * Re-start the slideshow from a specific slide.
	 */
	skip: function(e) {
		var $slide = $(e.currentTarget);
		var slide = $slide.index();
		
		this.hide( this.activeSlide );
		this.show( slide );
		this.start();
		
		return false;
	},
	
	/*
	 * The featured image was clicked.
	 * Find its related URL and go to it.
	 */
	goToProject: function(e) {
		window.location = this.$skipLinks.filter(':eq('+this.activeSlide+')').attr('href');
	}
};

