var iteration = 1;
var totalimages = 3;

function rotateAdverts() {
	if (iteration == totalimages) {
		iteration = 1;
	}
	else {
		iteration++;
	}
	jQuery('#rightColumn .advert:visible').fadeOut(600);
	jQuery('#advert'+iteration).fadeIn(600);
}

jQuery(document).ready(function() {
	adverts = setInterval("rotateAdverts();", 10000);
	jQuery('#rightColumn .advert').hover(function() {
		clearInterval(adverts);
	},
	function() {
		adverts = setInterval("rotateAdverts();", 10000);
	});
	jQuery('#searchform').validate({
		rules: {
			s: {
				required: true,
				minlength: 3
			}
		},
		messages: {
			s: {
			  required: "Please enter at least 3 characters",
			  minlength: "Please enter at least 3 characters"
			}
		}
	});
	jQuery('#joinMailingList form').validate({
		rules: {
			EMail: {
				required: true,
				email: true
			}
		},
		messages: {
			EMail: {
			  required:"Please complete this field",
			  email: "Please complete this field"
			}
		},
		submitHandler: function(form) {
			var height = jQuery('#joinMailingList').height();
			jQuery('#joinMailingList').css('height', height);
			jQuery('#joinMailingList form').remove();
			jQuery('#subscribeTitle').addClass('green').html('Please wait...');
			jQuery(form).ajaxSubmit({
				success: function(data) {
					jQuery('#subscribeTitle').html('Thank you').after('<p class="green">You have been added to our mailing list.</p>');
				}
			});
		}
	});
	jQuery('#commentform').validate({
		errorPlacement: function(error, element) {
            error.appendTo( element.parents("p") );
        },
		rules: {
			author: "required",
			email: {
				required: true,
				email: true
			},
			comment: "required"
		},
		messages: {
			author: {
			  required: "Please complete this field"
			},
			email: {
			  required: "Please complete this field",
			  email: "Please complete this field"
			},
			comment: {
			  required: "Please complete this field"
			}
		}
	});
	jQuery('#wpcf7-f1-p48-o1 form').validate({
		rules: {
			yourname: "required",
			youremail: {
				required: true,
				email: true
			},
			yourmessage: "required"
		},
		messages: {
			yourname: {
			  required: "Please complete this field"
			},
			youremail: {
			  required: "Please complete this field",
			  email: "Please complete this field"
			},
			yourmessage: {
			  required: "Please complete this field"
			}
		},
		submitHandler: function(form) {
			var height = jQuery('#submitContactForm').parent().height();
			jQuery('#submitContactForm').parent().addClass('green').css('height', height).html('Please wait...');
			jQuery(form).ajaxSubmit({
				success: function(data) {
					form.reset();
					jQuery('#wpcf7-f1-p48-o1 p.green').html('Thank you. We will respond as soon as possible.');
				}
			});
		}
	});
	jQuery('#wpcf7-f2-p42-o1 form').validate({
		rules: {
			yourName: "required",
			yourEmail: {
				required: true,
				email: true
			},
			yourTelephone: "required",
			yourAddress: "required",
			yourDOB: "required",
			yourAge: "required",
			yourName: "required",
			moreMerrier: "required",
			suffragettes: "required",
			bitterSweetSixteen: "required",
			trainShootAttendance: "required",
			groupInterviewAttendance: "required",
			howHeard: "required"
		},
		messages: {
			yourName: "Please complete this field",
			yourEmail: {
				required: "Please complete this field",
				email: "Please complete this field"
			},
			yourTelephone: "Please complete this field",
			yourAddress: "Please complete this field",
			yourDOB: "Please complete this field",
			yourAge: "Please complete this field",
			moreMerrier: "Please complete this field",
			suffragettes: "Please complete this field",
			bitterSweetSixteen: "Please complete this field",
			trainShootAttendance: "Please complete this field",
			groupInterviewAttendance: "Please complete this field",
			howHeard: "Please complete this field"
		},
		submitHandler: function(form) {
			var height = jQuery('#submitTrainingForm').parent().height();
			jQuery('#submitTrainingForm').parent().addClass('green').css('height', height).html('Please wait...');
			jQuery(form).ajaxSubmit({
				success: function(data) {
					form.reset();
					jQuery('#wpcf7-f2-p42-o1 p.green').html('Thank you. We will respond as soon as possible.');
				}
			});
		}
	});
	jQuery('#wpcf7-f3-p39-o1 form').validate({
		rules: {
			yourname: "required",
			youremail: {
				required: true,
				email: true
			},
			canattend: "required"
		},
		messages: {
			yourname: {
			  required: "Please complete this field"
			},
			youremail: {
			  required: "Please complete this field",
			  email: "Please complete this field"
			},
			canattend: {
			  required: "Please complete this field"
			}
		},
		submitHandler: function(form) {
			var height = jQuery('#submitVolunteerForm').parent().height();
			jQuery('#submitVolunteerForm').parent().addClass('green').css('height', height).html('Please wait...');
			jQuery(form).ajaxSubmit({
				success: function(data) {
					form.reset();
					jQuery('#wpcf7-f3-p39-o1 p.green').html('Thank you. We will respond as soon as possible.');
				}
			});
		}
	});
	jQuery('#showCommentForm a').click(function() {
		if (jQuery('#commentFormWrapper').is(':visible')) {
			jQuery('#commentFormWrapper').slideUp();		
		}
		else {
			jQuery('#commentFormWrapper').slideDown();
		}
		return false;
	});
	jQuery('#showHideTags').toggle(function() {
		jQuery(this).siblings().show();
		jQuery(this).html('hide all');
		return false;
	},
	function() {
		jQuery(this).siblings().hide();
		jQuery(this).html('see all');
		return false;
	});
	jQuery('#moreLatestVideos,#moreHotTopics').click(function() {
		var id = jQuery(this).attr('href');
		jQuery.scrollTo(id, 400);
		return false;
	});
});