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

function showHomePhoto(i) {
	clearInterval(homePhotoInterval);
	clearTimeout(homePhotoClickTimeout);
	_showHomePhoto(i);
	homePhotoClickTimeout = setTimeout(startHomePhotoInterval, 4000);
}

function _showHomePhoto(i) {
	if (i != currentPhotoIndex && !photoAnimationRunning) {
		currentPhotoIndex = i;
		photoAnimationRunning = true;
		var photos = $("#photo img.photo");
		$('.slider_bg3').css('display','none');
		$('.slider_bg3').eq(i).fadeIn(200);
		photos.filter(".selected").css('z-index', 2).removeClass('selected');
		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);
			photoAnimationRunning = false;
		});
		var navItems = $(".button_bg a");
		navItems.filter(".selected").removeClass('selected');
		navItems.eq(i).addClass('selected');
	}
}

function showNextPhoto() {
	var num=$("#photo img.photo").length;
	next = (currentPhotoIndex == (num-1)) ? 0 : currentPhotoIndex + 1;
	_showHomePhoto(next);
}

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

$(document).ready(startHomePhotoInterval);
