function formShade(el,notify) {
	$('div.alert',el).remove();
	$(el).fadeOut(400, function() {
		if (notify !== undefined && notify !== '') {
			$(el).before('<div class="notify">'+notify+'</div>');
		}
	});
}
function formReveal(el,str,kf) {
	$('div.notify').remove();
	$(el)
		.fadeOut(200,function() {
			if (kf===true) {
				$(this).prepend(str);
			} else {	
				$(this).html(str);
			}
		})
		.fadeIn(400);
}

$.expr[':'].external = function(o) {
	return !o.href.match(/^mailto\:/) && (o.hostname != location.hostname);
};

$(function() {



	// FF3.0
	if ($.browser.mozilla && $.browser.version.substr(0,5)=='1.9.0') {
		$('#nav-primary').css({'fontSize':'20px'});
		$('#feature aside.actions h2.callout').css({'fontSize':'1.25em'});
	}



	// External Links open in New Windows
	$('a:external').attr('target','_blank');
	
	// open pdf files in new window
	$("a[href$=pdf]").each(function() {
	  $(this).attr('target', '_blank');
	});



	// To Make Buttons Submit Forms (IE <= 7 Only)
	if ($.browser.msie && $.browser.version <= 7 && $('form button').length) {
		$('form button').click(function() {
			$(this).parents('form').submit();
		});
	}



	// Front Page Feature Rotator
	if ($('#feature ul li').length > 1) {
		$('#feature ul.slides').cycle({
			cleartypeNoBg:true,
			pager:'#feature ul.ctrl',
			pagerAnchorBuilder:function(i,e) {
				return '<li><a href="#">'+i+'</a></li>';
				}
		});
	}
	
	
	
	// Photo Gallery Fancybox
	if ($('dl.gallery-item a').length) {
		$('div.gallery').each(function() {
			var i = $(this).attr('id');
			$('dl.gallery-item a',this).each(function() {
				$(this).attr('rel',i);
			});
		});
		$('dl.gallery-item a').fancybox();
	}



	// Contact Form Validation
	if ($('form#contact').length) {
		var contactVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				comments:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						fname:$('input#'+$(f).attr('id')+'-fname',f).val(),
						lname:$('input#'+$(f).attr('id')+'-lname',f).val(),
						addr:$('input#'+$(f).attr('id')+'-addr',f).val(),
						city:$('input#'+$(f).attr('id')+'-city',f).val(),
						state:$('input#'+$(f).attr('id')+'-state',f).val(),
						zip:$('input#'+$(f).attr('id')+'-zip',f).val(),
						email:$('input#'+$(f).attr('id')+'-email',f).val(),
						phone:$('input#'+$(f).attr('id')+'-phone',f).val(),
						comments:$('textarea#'+$(f).attr('id')+'-comments',f).val(),
						sponsor:($('input#'+$(f).attr('id')+'-sponsor').length)? 'Sponsorship Inquiry':''
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h1>Thanks for your comments.</h1><p>'+json.message+'</p>';
						} else {
							m = '<h1>There was a problem sending your message.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#contact').validate(contactVal);
		$('form#contact input#contact-phone').mask("999 999-9999",{placeholder:" "});
		$('form#contact input#contact-zip').mask("99999",{placeholder:" "});
	}



	// Member Form Validation
	if ($('form#member').length) {
		var memberVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				'member-type':'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						fname:$('input#'+$(f).attr('id')+'-fname',f).val(),
						lname:$('input#'+$(f).attr('id')+'-lname',f).val(),
						addr:$('input#'+$(f).attr('id')+'-addr',f).val(),
						city:$('input#'+$(f).attr('id')+'-city',f).val(),
						state:$('input#'+$(f).attr('id')+'-state',f).val(),
						zip:$('input#'+$(f).attr('id')+'-zip',f).val(),
						email:$('input#'+$(f).attr('id')+'-email',f).val(),
						phone:$('input#'+$(f).attr('id')+'-phone',f).val(),
						comments:$('textarea#'+$(f).attr('id')+'-comments',f).val(),
						membertype:$('input.'+$(f).attr('id')+'-member-type:checked',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h1>Thanks for your comments.</h1><p>'+json.message+'</p>';
						} else {
							m = '<h1>There was a problem sending your message.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#member').validate(memberVal);
		$('form#member input#member-phone').mask("999 999-9999",{placeholder:" "});
		$('form#member input#member-zip').mask("99999",{placeholder:" "});
	}



	// Volunteer Form Validation
	if ($('form#volunteer').length) {
		var volunteerVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				formShade(f);
				var vtype = [];
				$('input.volunteer-volunteer-type:checked').each(function() {
					vtype.push($(this).val());
				});
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						fname:$('input#'+$(f).attr('id')+'-fname',f).val(),
						lname:$('input#'+$(f).attr('id')+'-lname',f).val(),
						addr:$('input#'+$(f).attr('id')+'-addr',f).val(),
						city:$('input#'+$(f).attr('id')+'-city',f).val(),
						state:$('input#'+$(f).attr('id')+'-state',f).val(),
						zip:$('input#'+$(f).attr('id')+'-zip',f).val(),
						email:$('input#'+$(f).attr('id')+'-email',f).val(),
						phone:$('input#'+$(f).attr('id')+'-phone',f).val(),
						comments:$('textarea#'+$(f).attr('id')+'-comments',f).val(),
						volunteertype:vtype.join(', ')
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h1>Thanks for your comments.</h1><p>'+json.message+'</p>';
						} else {
							m = '<h1>There was a problem sending your message.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#volunteer').validate(volunteerVal);
		$('form#volunteer input#volunteer-phone').mask("999 999-9999",{placeholder:" "});
		$('form#volunteer input#volunteer-zip').mask("99999",{placeholder:" "});
	}



	// Email Subscription Form Validation
	if ($('form#em-sub').length) {
		var emSubVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				$(f).before('<div class="alert"></div>');
				formShade(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						jssub:'1',
						fname:$('input#'+$(f).attr('id')+'-fname',f).val(),
						lname:$('input#'+$(f).attr('id')+'-lname',f).val(),
						email:$('input#'+$(f).attr('id')+'-email',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<h1>Thanks for your interest!</h1><p>'+json.message+'</p>';
						} else {
							m = '<h1>There was a problem subscribing.</h1><p>'+json.message+'</p>';
						}
						$('div.alert').html(m).show(200);
					},
					error:function(x,t,e) {
						$('div.alert').html('<h1>There was a problem submitting the form.</h1>').show(200);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#em-sub').validate(emSubVal);
	}



});
