var currentPhotoIndex = 0;
var photoAnimationRunning = false;
var homePhotoInterval;
var homePhotoClickTimeout;

function showHomePhoto(i)
{
	clearInterval(homePhotoInterval);
	clearTimeout(homePhotoClickTimeout);
	_showHomePhoto(i);

	homePhotoClickTimeout = setTimeout(startHomePhotoInterval, 10000);
}


function _showHomePhoto(i)
{
	if (i != currentPhotoIndex && !photoAnimationRunning)
	{
		currentPhotoIndex = i;
		photoAnimationRunning = true;

		// PHOTOS
		var photos = $("#photo img.photo");
		// old photo
		photos.filter(".selected").
			css('z-index', 2).
			removeClass('selected');
		// new photo
		photos.eq(i).
			hide().
			css('z-index', 3).
			addClass('selected').
			fadeIn(200, function(){
				$(this).css('z-index', 2);
				photos.filter("img:not(.selected)").css('z-index', 1);
			});

		// NAV ITEMS
		var navItems = $("#photo-navigation li");
		// old nav item
		navItems.filter(".selected").removeClass('selected');
		// new nav item
		navItems.eq(i).addClass('selected');
	
		// TITLE
		var titles = $("#photo-title a");
		// old title
		titles.filter(".selected").
			fadeOut(200, function(){
				$(this).removeClass('selected');
			});
		// new title
		titles.eq(i).
			fadeIn(200, function(){
				$(this).addClass('selected');
				photoAnimationRunning = false;
			});
	}
}

function showNextPhoto()
{
	next = (currentPhotoIndex == 4) ? 0 : currentPhotoIndex + 1;
	_showHomePhoto(next);
}

function startHomePhotoInterval()
{
	homePhotoInterval = setInterval(showNextPhoto, 5000);
}

$(document).ready(startHomePhotoInterval);
