$(document).ready(function(){
	//Home Slideshow
	$('#masthead ul').cycle({ 
		fx:     'fade', 
		speed:  'slow',
		timeout: 6000,
		pager:  '#ss-nav'
	});
	
	//Header Opacity
	$('.content-image h2').css('opacity', 0.7);
	
	// IE Sucks	
	$('a.thumb.last, .item.last').css('margin-right', '0px');
	
	//Donate Form Any Amount
	$('#donate-any-amount #donate-submit').hover(
	      function () {
	        $(this).addClass('hover');
	      }, 
	      function () {
	        $(this).removeClass('hover');
	      }
	    );
	// Donate Monthly
	$('#donate-any-amount #monthly').click(function(){
		if($(this).is(':checked')){
			alert('Your credit card will be billed for your specified amount monthly.');
			$(this).parent().append('<input type="hidden" name="sub_frequency" id="recurring-payment" value="1m" />');
		}
		else{
			$(this).parent().find('#recurring-payment').remove();
		}
	})
	
	// Gallery Slideshow Using Fancybox
	$('a.group').fancybox();
	
	// Inline Slideshow.inline-slideshow
	$('#inline-slideshow').cycle({ 
		fx:     'fade', 
		speed:  'slow',
		before:  onBefore, 
	    after:   onAfter
	});
	
	//Callback before the switch...fade out the caption.
	function onBefore() { 
    $('#inline-slideshow-wrap p').fadeOut("slow"); 
	}
	
	//Callback after the image switches...fade in the caption.
	function onAfter(curr, next, opts) {
    // var index = $(this).parent().children().index(this);
	//Fadein the caption when the slide switches.
	$('#inline-slideshow-wrap p').text(this.alt).fadeIn("slow");
	}
	
	//form validation
	$("#contact-form").RSV({
		errorTextIntro: "Whoops...looks like you forgot something.",
		errorTargetElementId: "form-error",
		displayType: "display-html",
		errorFieldClass: "error",
		rules: [
		"required,name,Please enter your name.",
		"required,email_addy,Please enter your email address.",
		"valid_email,email_addy,Please enter a valid email address.",
		"required,subject,Please enter a subject.",
		"required,message,Please enter a message."
		]
	});
	
	//Enews Signup
	$("#enews-submit").click(function() {
		//Prevent the click
		if ($(this).hasClass('success, processing') == false){
			// First, disable the form from submitting
			$('#enews-signup').submit(function(){
				return false;
			});

			// Grab form action
			formAction = $("#enews-signup").attr("action");

			// Hack together id for email field
			emailId = formAction.replace("http://sendmail.s-12.com/t/r/s/", "");
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;

			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				$('#enews-submit').addClass('error');
				$('#jtluij-jtluij').addClass('error');
				setTimeout(function(){ $("#enews-submit").removeClass('error') }, 3000);
				return;
			}

			$('#enews-submit').addClass('processing');

			// Serialize form values to be submitted with POST
			var str = $("#enews-signup").serialize();

			// Add form action to end of serialized data
			final = str + "&action=" + formAction;

			// Submit the form via ajax
			$.ajax({
				url: "/process/proxy_curl.php",
				type: "POST",
				data: final,
				success: function(html){
					setTimeout(function(){ $('#enews-submit').addClass('success'); $('fieldset.remove').fadeOut('slow') }, 1000);
				}
			});
		}
	});
	
	//Blur Function
		//Show hide input copy
		$('#enews-signup input').each(function() {
			$(this).val($(this).prev('label').text());
		});
		
		$('#enews-signup input').focus(function() {
			if ($(this).val() == $(this).prev('label').text()) {
				$(this).val('');
			}
		}).blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).prev('label').text());
			}

		});
});

	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}