$(function(){
	
	var showcase = new Showcase(0,0);
	
});

/**
 * @param bool hasNav				Set false to disable manual navigation functionality (only transitions automatically)
 * @param bool hasDescription		Set false to disable descriptions
 */
function Showcase( hasNav, hasDescription ){

	this.hasNav = hasNav;
	this.hasDescription = false;				
	
	// Options
	this.animationTime = 2000;			// Duration of fading animation
	this.autoTransitionTime = 8000; 	// Time between automatic transitions
	
	// Selectors
	this.imageSelector = '.showcase-image';
	this.showcaseSelector = '#showcase';
	this.prevSelector = '#showcase-prev';
	this.nextSelector = '#showcase-next';
	this.descriptionSelector = '#showcase-description';
	
	this.showcase = $(this.showcaseSelector);
	
	if( this.hasNav ){
		this.prev = $(this.prevSelector,this.showcase);
		this.next = $(this.nextSelector,this.showcase);
	}
	
	if( this.hasDescription )
		this.description = $(this.descriptionSelector,this.showcase);
	
	// Grab all the  and put into this.images array
	this.images = [];
	var self = this;		
	$(this.imageSelector,this.showcase).each(function(i){
		self.images[i] = $(this);
	});
	
	if( !this.showcase.length || !this.images.length )
		return false;
	
	// Initialises Showcase, selects first showcase image to display at random and begins transitions
	this.init = function(){
		var random = Math.floor( Math.random() * self.images.length );
		this.load( random );
	};
	
	// If navigation is enabled bind navigation
	if( this.hasNav ){
	
		// Bind previous button
		this.prev.click(function(e){
		
			self.change.prev();
			
			e.preventDefault;
			return false;
		});
		
		// Bind next button
		this.next.click(function(e){		
			self.change.next();
				
			e.prevenDefault;
			return false;
		});
	}
	
	// Load function, fades old image out and new image in
	this.load = function( n ){
		
		// Clear auto transition time out
		if( typeof this.timer != 'undefined' )
			clearTimeout( this.timer );
		
		// If image request doesn't exist, throw exception
		if ( typeof self.images[n] == 'undefined' ){
			this.automate();
			return false;
		}
		
		if( typeof this.current != 'undefined' ) {
			this.loading = true;

			this.images[n].fadeIn(this.animationTime);
			this.images[this.current].fadeOut((this.animationTime / 2),function(){
				if( self.hasDescription )
					self.description.html(self.images[ n ].attr('alt'));
					
				self.loading = false;
			});
		}
		// If there is no existing images it means the showcase has been initialised and we can select our first image
		else
		{
			this.images[ n ].css('zIndex',15);
			if( this.hasDescription )
				this.description.html( this.images[n].attr('alt') );
		}
		
		this.current = n;
		
		this.automate();
		
	};

	// Calculates which image to load next
	this.change = {
		next : function(){
			if( typeof self.images[ self.current + 1 ] == 'undefined' )
				self.loadQueue( 0 );
				
			else
				self.loadQueue( self.current + 1 );
			
		},
		prev : function(){
			if( typeof self.images[ self.current - 1 ] == 'undefined' )
				self.loadQueue( self.images.length - 1 );
			else
				self.loadQueue( self.current - 1 );
		}
	};
	
	// Queues slide loading
	this.loadQueue = function( n ){
		$(self).queue( this.load( n ) );
	}
	
	this.automate = function(){
		this.timer = setTimeout( function(){
			self.change.next();
		}, (self.autoTransitionTime + self.animationTime) );
	};
	
	this.init();
}
