/* **************************************
 * Home.js
 * jQuery functions for the home page only
 ***************************************/


/* 
 * jCarousel
 */
// Use the initCallback callback to assign functionality to the controls
function mycarousel_initCallback(carousel) {
	
	// Pagination control
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll($.jcarousel.intval($(this).text()));
		carousel.startAuto(0);// Disable autoscrolling
        return false;
    });
	
	// Next button
    $('#carouselNext').bind('click', function() {
        carousel.next();
		carousel.startAuto(0);// Disable autoscrolling
        return false;
    });
	// Previous button
    $('#carouselPrev').bind('click', function() {
        carousel.prev();
		carousel.startAuto(0);// Disable autoscrolling
        return false;
    });
	// Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

/*
 * Contact-form.js
 * Contact form validation via http://trevordavis.net/blog/wordpress-jquery-contact-form-without-a-plugin/
 */
function contactForm(){
	$('form#contactForm').submit(function() {
		$('form#contactForm .error').remove();
		var hasError = false;
		$('.requiredField').each(function() {
			if(jQuery.trim($(this).val()) == '') {
				var labelText = $(this).prev('label').text();
				$(this).parent().append('<span class="error">You forgot to enter your '+labelText+'.</span>');
				hasError = true;
			} else if($(this).hasClass('email')) {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!emailReg.test(jQuery.trim($(this).val()))) {
					var labelText = $(this).prev('label').text();
					$(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');
					hasError = true;
				}
			}
		});
		if(!hasError) {
			$('form#contactForm li.buttons button').fadeOut('normal', function() {
				$(this).parent().append('<img src="wp-content/themes/richardWestenra2/images/loading.gif" alt="Loading&hellip;" height="31" width="31" />');
			});
			var formInput = $(this).serialize();
			$.post($(this).attr('action'),formInput, function(data){
				$('form#contactForm').slideUp("fast", function() {				   
					$(this).before('<p class="thanks"><strong>Thanks!</strong> Your email was successfully sent. I&rsquo;ll be in touch shortly.</p>');
				});
			});
		}
		
		return false;
		
	});
}

