$(function(){

	/**
	 * Features on home page
	 */
	var index = 0;
	var lastIndex = 0;
	var timer = null;
	var duration = 5000;
	var animationDuration = 750;
	var slideCount = $('#features .thumbs li').length;
	
	// Default thumbs to partially opaque
	$('#features .thumbs img').fadeTo(0, 0.66).eq(index).fadeTo(0, 1);
	$('#features .thumbs li').eq(index).addClass('active');
	
	// Start auto rotation
	play();
	
	// Bind thumbs
	$('#features .thumbs li').map(function(i){
		$(this).bind('click', function(){
			index = i;
			pause();
			showItem(i);
		});
		$(this).bind('mouseenter', function(){
			$(this).find('img').fadeTo(500, 1);
		});
		$(this).bind('mouseleave', function(){
			if ($(this).hasClass('active')) return;
			$(this).find('img').fadeTo(500, 0.66);
		});		
	});
	
	function play() {
		timer = setInterval(function(){
			index++;
			if (index == slideCount) index = 0;
			showItem(index);
		}, duration);
	}
	
	function pause() {
		clearInterval(timer);
	}
	
	function showItem(index) {
		if (index == lastIndex) return;
		$('#features .contents .content').eq(lastIndex).fadeOut(animationDuration);
		$('#features .contents .content').eq(index).fadeIn(animationDuration);
		$('#features .thumbs li').eq(lastIndex).removeClass('active');
		$('#features .thumbs li').eq(index).addClass('active');
		$('#features .thumbs img').eq(lastIndex).fadeTo(animationDuration, 0.66);
		$('#features .thumbs img').eq(index).fadeTo(animationDuration, 1);
		$('#features .features .feature').eq(lastIndex).fadeOut(animationDuration);
		$('#features .features .feature').eq(index).fadeIn(animationDuration);
		lastIndex = index;
	}
	
});
