$(document).ready(function() {
	
	$('#SpringAuthorizationForm').submit(function(e) {
		

	
		var dateCount =0;
		var serviceCount =0;
		$("#SpringAuthorizationForm .openingDate:checked").each(function() {
			dateCount++;
		});
		if(dateCount < 1) {
			alert("You must choose at least one date for your pool opening.");
			return false;
		}
		
		$("#SpringAuthorizationForm .service:checked").each(function() {
			serviceCount++;
		});
		if(serviceCount < 1) {
			alert("You must select at least one opening service.");
			return false;
		}
		
		else {
			return true;
		}
			
	});
	
	$('#WeeklyServiceAgreement').submit(function(e) {
		var serviceCount =0;
		$("#WeeklyServiceAgreement .serviceOption:checked").each(function() {
			serviceCount++;
		});
		if($("[name=accept_agreement]:checked").length != 1) {
			alert("You must accept the terms of the agreement before continuing.");
			return false;
		}
		if(serviceCount < 1) {
			alert("You must select a service option");
			return false;
		}
			
	});
	


	
	$('#Gallery .thumbs p a').click(function(e){
		e.preventDefault();
		var imageSrc = $(this).attr('href');
		$('#MainGalleryImage').attr('src',imageSrc);
		imageSrc = imageSrc.replace(/gallery\/medium\//,'gallery/large/');
		$('#MainGalleryLargeImageLink').attr('href',imageSrc);
	});
	
	var gallerySelection = $('#GalleryListWrapper').find('p.current:first');
	if(gallerySelection.length > 0){
		if(!gallerySelection.parent().hasClass('currentRow')){
			var el = $('#GalleryListWrapper');
			el.find('.currentRow').removeClass('currentRow');
			gallerySelection.parent().addClass('currentRow');
			var currentRow = el.find('.currentRow:first').removeClass('currentRow').attr('id');
			if(currentRow != undefined){
				var index = currentRow.slice(-1);
				var nextRowID = '#' + currentRow.slice(0,-1) + index;
				var nextRow = el.find(nextRowID);
				if(nextRow.length > 0){
					nextRow.addClass('currentRow');
					var top = 82 * (index-1);
					$('#GalleryListWrapper').animate({scrollTop:top},'slow');
				}
			}
		}
	}
	
	$('.galleryListScrollDown').click(function(){
		var el = $('#GalleryListWrapper');
		var currentRow = el.find('.currentRow:first').attr('id');
		if(currentRow == undefined){
			currentRow = el.find(' > div:first').attr('id');
		}
		if(currentRow != undefined){
			var index = parseInt(currentRow.slice(-1));
			index = 4 + index;
			var numRows = (el.find(' > div')).length;
			if(index > numRows) {index = numRows;}
			
			var nextRowID = '#' + currentRow.slice(0,-1) + index;
			var nextRow = el.find(nextRowID);
			
			if(nextRow.length > 0){
				el.find('.currentRow').removeClass('currentRow');
				nextRow.addClass('currentRow');
				var top = 82 * (index-1);
				el.animate({scrollTop:top+'px'},'slow');
			}
		}
	});
	
	$('.galleryListScrollUp').click(function(){
		var el = $('#GalleryListWrapper');
		var currentRow = el.find('.currentRow:last').attr('id');
		if(currentRow == undefined){
			currentRow = el.find(' > div:last').attr('id');
		}
		if(currentRow != undefined){
			var index = parseInt(currentRow.slice(-1));
			var numRows = (el.find(' > div')).length;
			if(index == numRows) {index = numRows - 3;}
			index = -4 + index;
			if(index < 0) {index = 1;}
			
			var prevRowID = '#' + currentRow.slice(0,-1) + index;
			var prevRow = el.find(prevRowID);
			
			if(prevRow.length > 0){
				el.find('.currentRow').removeClass('currentRow');
				prevRow.addClass('currentRow');
				var top = 82 * (index-1);
				el.animate({scrollTop:top+'px'},'slow');
			}
		}
	});
	
	// $(".lightbox").lightbox(); put at end of jquery.lightbox.js file instead
	
	$("form").submit(function(e){
		var form = e.target;
		var errorMsg = '';
		for(i = 0; i < form.elements.length; i++){
			var field = $(form.elements[i]);
			if(field.hasClass('required') && form.elements[i].value.length < 1){
				var title = field.attr('title');
				errorMsg += title + " field is required.\n";
			}
		}
		if(errorMsg != ''){
			e.preventDefault();
			alert(errorMsg);
			return false;
		}
		return true;
	});
	
	
});