var clickCounter = 0;
var timerId = 0;

jQuery(document).ready(function() {
	initHovers();
	initClicks();
	setTimeout('initSlideshow();', 5000);

	// fade in first project	
	jQuery('#splash #project-1').fadeIn('slow');
	jQuery('#splash #project-1').addClass('selected');
	jQuery('#splash .featured ul li:first a').addClass('selected');
});

function initClicks()
{	
	jQuery('#splash .featured ul li a').click(projectSelectorClickHandler);	
}

function initHovers()
{
	// featured thumbs
	jQuery('#splash .featured ul li a').each(function() {
		jQuery(this).hover(function() {
			jQuery(this).addClass("active");
				}, function() {
			jQuery(this).removeClass("active");
			});
	});
}

function initSlideshow()
{
	timerId = setInterval('autoSlideNextProject()', 7500);
}

function autoSlideNextProject()
{
	$currentProject = jQuery('#splash .featured ul li a.selected');
	numProjects = parseInt(jQuery('#splash .featured ul li').length);
	if($currentProject.length)
	{
		currentProjId = parseInt($currentProject.attr('id').replace(/featured-/, ''));
		nextProjId = ((++currentProjId - 1) % numProjects) + 1;
		unbindClicks(true);
		swapProject(nextProjId);
	}
}

function stopSlideshow()
{
	clearInterval(timerId);
}

function unbindClicks(initCC)
{
	jQuery('#splash .featured ul li a').each(function() { jQuery(this).unbind('click') });
}

function resetProjects()
{
	jQuery('#splash .featured ul li a').removeClass('active').removeClass('selected');
}

function projectSelectorClickHandler(e)
{
	stopSlideshow();
	projId = currentProjId = parseInt(jQuery(this).attr('id').replace(/featured-/, ''));
	unbindClicks(true);
	swapProject(projId);
	e.preventDefault();
}

function fadeSelectedProject()
{
	jQuery('#splashWrapper').fadeOut(100);
	jQuery('#splash .selected').hide();
	jQuery('#splash .selected').removeClass('selected');
}

function showSelectedProject(id)
{
	jQuery('#splash #project-' + id).addClass('selected');
	jQuery('#splash .selected').show();
	jQuery('#splashWrapper').fadeIn(150);
}

function swapProject(id)
{
	jQuery('#splash .featured ul li a#featured-' + id).unbind();
	var newMargin = '-1px';
	var bottomDefault = '-4px';
	var bottomHidden = '-80px';
		
	switch(id)
	{
		case 1:
			newMargin = -1;
		break;
		case 2:
			newMargin = 114;
		break;
		case 3:
			newMargin = 229;
		break;
		case 4:
			newMargin = 344;
		break
		case 5:
			newMargin = 459;
		break;
		case 6:
			newMargin = 574;
		break;
	}
	
	
	initHovers();
	
	// reset the thumb
	resetProjects();
	
	fadeSelectedProject();
	
	jQuery('#splash .featured ul li a#featured-'+id).addClass("selected");
	

	currentMargin = parseInt(jQuery('#splash .featured #highlighter').css('left'));
	bouncePadding = 15;
	// set bounce padding
	// moving left, so negative
	if(currentMargin > newMargin)
		bouncePadding *= -1;
	newMarginPlus = newMargin + bouncePadding;
	
	// add some delay based on the distance we're sliding	
	slideDelay = 200 + Math.abs(newMargin - currentMargin) / 4;

	// slide the highlighter left or right plus 15px
	jQuery('#splash .featured #highlighter').animate({
		left: newMarginPlus + 'px'
	},
	slideDelay,
	function() {
		showSelectedProject(id);
		
		// slide back 10% for bounce
		jQuery('#splash .featured #highlighter').animate({
			left: newMargin + 'px'
		},
		100,
		function() {
			initClicks();	
		});
	});	
}
