// Javascript that needs to run when the page is ready goes here.
$(document).ready(function() {
	
	// Li Classes
	// Adds a class to first and last list items globally so we can target them with CSS, because IE7 sucks. Also add a class of even
	// to well, the even rows of a list so we can give separate margins, styles or floats.
	$("li:first-child").addClass("first");
	$("li:last-child").addClass("last");
	
	// Smooth Anchor
	// Add a class of scroll to any anchor link and this will smoothly scroll to the location. 
	$(".scroll").click(function(event){
		event.preventDefault();
		$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
	});
	
	$("navigation a").attr('title',''); // removes the title tags from the navigation
	
	
	
	// Home Slider
	fadeCount = $('#homegallery').children().size(); //number of slides
	nextFade = 2;// anchors for the next slide
	
	fadeDuration = 800; // the duration of the transition
	fadeDelay = 6000; // the delay between each transition
	fadeInstance = 9999; // the instance of the slide animation que
	
	if(fadeCount>=2){ // only animates if there is more than one item in the slider
		$('#fade1').addClass('active').css('z-index',fadeInstance); // adds the active class to the first frame
		$('#fade1-nav').addClass('here');
		setInterval(playFade, fadeDelay); // plays the slider, with a timer
	}
	
	function playFade() { // function for slide validation
		isPaused = $('#homegallery').hasClass('paused'); // if the animations are paused
		isSkipped = $('#homegallery').hasClass('skipOnce'); // if the next transition is due to be skipped
		if(!isPaused && !isSkipped) { // checks reasoning for effect
			animateFade(); // animates the transition to the next slide
		} else {
			$('#homegallery').removeClass('skipOnce');
		}
	}
	
	function animateFade() { // function for slide transition
		$('#homegallery .active').removeClass('active');
		$('#subnav-home-grad .here').removeClass('here');
		$('#fade' + nextFade).addClass('active');
		$('#fade' + nextFade + '-nav').addClass('here');
		$('#homegallery .active').hide().css('z-index',fadeInstance).fadeIn(fadeDuration);
		if(nextFade==fadeCount) { // if the current slide is the last
			nextFade=1; // the next slide is the first
		} else {
			nextFade++; // increments the next value
		}
		fadeInstance++; // ups the value, used 
	}
	
	$('#subnav-home-grad a').click(function(event) { // click function for the gallery navigation
	  	event.preventDefault();
		nextFade = $(this).parent().attr('rel'); // get the slide to switch to
		$('#homegallery').addClass('skipOnce'); // skips the next round of animation
		animateFade(); // animates the transition to the next slide
	});
	
	$('#homegallery').bind({
		mouseenter: function() {
    	$("#homegallery").addClass('paused'); // pause transitions
  	},  mouseleave: function() {
    	$("#homegallery").removeClass('paused'); // unpauses transitions
  	}});
  	
  	
  	
  	// Contact Form (Pull Suburb)
	$(function(){
		$('#select_region').change(function() {
			var cat_id = $(this).val();
			var url = '/contact/fetch_suburbs/' + cat_id;
	  		// alert('calling url: ' + url);
	  		$('#suburb_holder').load(url);
		});
	});

	
	// Email Validator
	function validEmail(emailAddy) {  
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddy);
	}
	
	// Validation Function
	validateField = function(fieldID) {
		thisVal = $(fieldID).val();
		$(fieldID).removeClass('error');
		if(!thisVal) { // if the field is blank
			$(fieldID).addClass('error');
			messageValid = false;
		}
		if($(fieldID).hasClass('email')) { // if the field is for email
			if(!validEmail(thisVal)){ // checks against regex
				$(fieldID).addClass('error');
				messageValid = false;
			}
		}
	}
	
	submitAttempted = false; // true after first submission attempt
	messageValid = false; // if the form has been validated
	
	// Contact Form (Validation on submission)
	$('#submit-btn').click(function(event) {
		messageValid = true;
		event.preventDefault();
		$('#freeform .required').each(function() { // loops through the fields
			field = '#' + $(this).attr('id');
			validateField(field);
		});
		if(messageValid) {
			$('#freeform').submit(); // sends the message
		} else {
			showErrorMessage();
		}
		
//		if (freeform.agree.checked == false){
//			alert('You need to agree to our terms and conditions before entering.');
//			return false;
//		} else {
//			return true;
//		}
		
	});
	
	// Contact Form (Validation on change)
	$('#freeform .required').change(function() {
		if(submitAttempted) {
			field = '#' + $(this).attr('id');
  			validateField(field);
  		}
	});
	
	// Error message
	showErrorMessage = function() {
		if(!submitAttempted) {
			$('#freeform').append('<p id="validator-message" style="display:none">* Please correctly fill in all required fields</p>');
			$('#validator-message').slideDown(300);
			submitAttempted = true;
		}
	}
	
	// iPad Subscribe Form
	acceptTerms = function(subForm) {
		if (subForm.agree.checked == false){
			alert('You need to agree to our terms and conditions before entering.');
			return false;
		} else {
			return true;
		}
	}
	
});
	

// Functions and plugins can go below here.











