$(function() {

	$(window).load(updateBackgroundContainer).resize(updateBackgroundContainer);
	$('#backgroundimage img').imagesLoaded(updateBackgroundContainer);
	
	initBackgroundSlideShow();
	initFooter();
	
});

var backgroundSlideShowWait = 5000;
var backgroundSlideShowDuration = 1750;
var currentBackgroundPhoto = 0;
var homePhotosSlideShowTimeout;

var revealFooterDuration = 750;
var devealFooterDuration = 250;
var footerStartBottom;

function updateBackgroundContainer() {
	var siteHeight = $('#maincontainer').height();
	var windowHeight = $(window).height();
	var windowWidth = Math.max($(window).width(), 1040);
	
	$('#backgroundcontainer').show();
	
	$('#backgroundcontainer').height(windowHeight);
	$('#backgroundcontainer').width(windowWidth);
	
	$('#backgroundcontainer img').removeAttr('width').attr('height', windowHeight);
	if ($('#backgroundcontainer img').width() < windowWidth) $('#backgroundcontainer img').removeAttr('height').attr('width', windowWidth);
}

function initBackgroundSlideShow() {
	$('#backgroundcontainer img').eq(currentBackgroundPhoto).addClass('active').fadeIn(0);
	
	continueBackgroundSlideShow();
}

function continueBackgroundSlideShow() {
	var nextPhoto = currentBackgroundPhoto + 1;
	var numPhotos = $('#backgroundcontainer img').length;
	
	if (nextPhoto > (numPhotos - 1)) nextPhoto = 0;
	
	homePhotosSlideShowTimeout = setTimeout(function() {
		revealBackgroundPhoto(nextPhoto);
	}, backgroundSlideShowWait);
}

function revealBackgroundPhoto(nextPhoto) {
	$('#backgroundcontainer img.active').css('z-index', '1');
	
	$('#backgroundcontainer img').eq(nextPhoto).css('z-index', '5').fadeIn(
		backgroundSlideShowDuration,
		'easeOutQuint',
		function() {
			$('#backgroundcontainer img.active').fadeOut(0).removeClass('active');
			$(this).addClass('active');
			currentBackgroundPhoto = nextPhoto;
			continueBackgroundSlideShow();
	});
}

function initFooter() {
	footerStartBottom = $('#footercontainer').css('bottom');
	
	$('#footer').mouseenter(revealFooter);
	$('#footercontainer').mouseleave(devealFooter);
}

function revealFooter() {
	$('#footercontainer').stop().animate(
		{
			bottom : '0'
		},
		revealFooterDuration,
		'easeOutQuint'
	);
}

function devealFooter() {
	$('#footercontainer').stop().animate(
		{
			bottom : footerStartBottom
		},
		devealFooterDuration,
		'easeOutQuint'
	);
}
