var numPhotos = 0;
var imageTimer = -1;
var currentShowingPhoto = 0;

$(window).resize(function() {
	resize($('.title img.fitted').first(), 250);
	resize($('#photos img.active')), 400;
});

$(document).ready(function() {
		
	resize($('.title img.fitted').first(), 250);
	resize($('#photos img.active')), 400;
	
	numPhotos = $('#photos img.fitted').length;

	if (numPhotos > 1)
	{
		startTimer();
	}
	
	
	$('#enquirebutton').click(function(){
		$('#enquire').fadeIn();
		$('#intro').fadeOut();
	});

	$('#closeform').click(function(){
		$('#intro').fadeIn();
		$('#enquire').fadeOut();
	});	
	
});

function nextImage(imageNum)
{	
	if (imageNum == currentShowingPhoto)
		return false;
		
	var currentAnimations = 0;
	$('#photos img.fitted').each(function () {
		currentAnimations += $(this).queue("fx").length;
	});
	if (currentAnimations > 2)
		return false;
		
	clearInterval(imageTimer);
	
	if (imageNum >= numPhotos) imageNum = 0;
	$('#photos img.active').fadeOut(1500);
	$('#photos img.active').removeClass('active');
	$('#photos li.active').removeClass('active');
	
	$('#photos img.fitted').eq(imageNum).fadeIn(1500);
	$('#photos img.fitted').eq(imageNum).addClass('active');
	$('#photos li').eq(imageNum).addClass('active');
	
	resize($('#photos img.active'), 400);
	currentShowingPhoto = imageNum;
	startTimer();
	return false;
}

function startTimer()
{
	imageTimer = setInterval(function() {
		var theNextImage = currentShowingPhoto+1;
		nextImage(theNextImage);
	}, 8000);
}

function resize(element, targetHeight)
{
	height = $(element).height();
	height = parseInt(height);
	height -= targetHeight;
	height /= 2;
	theHeight = '-' + height + 'px';
	//$('.title .fitted').css('height', theHeight);
	$(element).css('top', theHeight);
}

