$(document).ready(function(){
	
	var numberOfImages = $('#top-promo li').length; 
	var currentImage = 0;
	var previousImage = numberOfImages;
	
	if(numberOfImages > 1){
		// Init
		$('#top-promo').css('visibility', 'visible');
		$('#top-promo li').fadeOut(1);
		$('#top-promo li:eq(0)').fadeIn(1).css('z-index', '1').css('opacity', 1);
		$('#top-promo li:eq(1)').css('z-index', '-1').show();
		
		setInterval(function(){		
			
			nextImage = currentImage + 1;
			if(nextImage >= numberOfImages){
				nextImage = 0;
			}
			
			$('#top-promo li:eq(' + currentImage + ')').show().css('z-index', '-1');
			$('#top-promo li:eq(' + nextImage + ')').css('z-index', '1').fadeIn();				
			
			setTimeout(function(){
				$('#top-promo li:eq(' + previousImage + ')').fadeOut();
			}, 500);
			
			previousImage = currentImage;
			currentImage ++;
			
			if(currentImage >= numberOfImages){
				currentImage = 0;
			}
			
		}, 5000);				
		
	}
	
	$('form').submit(function() {
		var inputs = $('div.input input.required', $(this));
		var error = false;
		var errorMsg = 'Sorry, you need to complete the following fields:\n';
		inputs.each(function(){
			if($(this).val() == '') {
				error = true;
				errorMsg += '\n - ' + $('label', $(this).parent()).html();
			}
		});
		errorMsg += '\n\nPlease try again.';
		if(error) {
			alert(errorMsg);		
			return false;	
		} else {
			return true;
		}
	});
	
});