var showcase = 
{
	el: null,
	index: 0,
	nextIndex: -1,
	count: 0,	
	timeout: null,
	busy: false,
	
	init: function() 
	{
		this.$el = $('#slider');
		if (this.$el.length == 0)
			return;
		this.addPagination();
		this.navigate(0);
	},
	
	addPagination: function()
	{
		this.count = $('.item', this.el).length;
		var html = '';
		for (var i = 0; i < this.count; i++)
			html += '<a>' + (i + 1) + '</a>';
		$('.pagination', this.el).html(html);
		$('.pagination a', this.el).click(function(e) {
			showcase.navigate($(e.target).index());
		});
	},
	
	navigate: function(index)
	{
		$('.pagination a', this.el).removeClass('active');
		$('.pagination a:nth-child(' + (index + 1) + ')', this.el).addClass('active');

		if (this.busy)
		{
			this.nextIndex = index;
			return;
		}
		
		var time = 350;
		
		clearTimeout(this.timeout);

		
		if (index != this.index)
		{			
			this.busy = true;
			
			var oldItem = $('.item:nth-child(' + (this.index + 1) + ')', this.el);						
			
			this.index = index;
			
			$('.item', this.el).css({'z-index': '1'});

			var item = $('.item:nth-child(' + (index + 1) + ')', this.el);
			var newText = $('p', item).html();
			
			$('.overlay-wrap', this.el).slideUp(time);

			setTimeout( function() {
				item.css({'z-index': '2'}).fadeOut(0).fadeIn(time * 2 + time * .66);							
			}, time * 0.66);
			
			setTimeout(function() {
				$('.overlay', this.el).html(newText);
				if (newText.length > 0)
					$('.overlay-wrap', this.el).slideDown(time);				
			}, time * 3);
			
			setTimeout(function() { 
				oldItem.hide();
				showcase.busy = false; 
				if (showcase.nextIndex != -1)
				{
					showcase.navigate(showcase.nextIndex);
					showcase.nextIndex = -1;
				}
			}, time * 4);
			
		}
		
		this.timeout = setTimeout( function() { showcase.next(); }, 6000);		
	},
	
	next: function() 
	{
		this.navigate((this.index + 1) % this.count);
	}
	
};
