jQuery(document).ready(function($){

	if($('#countdown').length){
		var campStarts = new Date();
		campStarts = new Date(2012, 6-1, 23, 0, 0, 0); // June 23, 2012
		$("#countdown").countdown({until: campStarts, compact: true, format: 'd', layout: '<div class="num left num{d100}"><img src="images/countdown/{d100}.png"></div><div class="num mid num{d10}"><img src="images/countdown/{d10}.png"></div><div class="num right num{d1}"><img src="images/countdown/{d1}.png"></div>'});
	}
	
	//Request Info form
	$('#requestInfoTrigger, #requestInfoTriggerSign, #requestInfo a.close').click(function(){
  	$('#requestInfo').slideToggle();
  });

	
	// Request Info form
	if (('#requestInfo #submit').length){
	$('#requestInfo #submit').click(function(){
		//$(this).addAttr('disabled').addClass('disabled');
		var hasError = false;
	
		// Validate required fields and set variables
		var inputname = $('input#fname').val();
		if (inputname == ""){
			$('input#fname').focus().after('<label class="error">Please enter your name</label>').addClass('error');
			hasError = true;
		}
		var emailReg = 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);
		var inputemail = $('input#email').val();
		if (inputemail == ""){
			$('input#email').focus().after('<label class="error">Please enter your email</label>').addClass('error');
			hasError = true;
		} else if(!emailReg.test(inputemail)){
			$('input#email').focus().after('<span class="label">Email address appears invalid</label>').addClass('error');
			hasError = true;
		}
		var inputphone = $('input#phone').val();
		if (inputphone == ""){
			$('input#phone').focus().after('<label class="error">Please enter your phone</label>').addClass('error');
			hasError = true;
		}
		var inputlname = $('input#lname').val();
		var inputcompany = $('input#company').val();
		var inputmessage = $('textarea#message').val();
		
		//Spam trap values
		var token1val = $('input#token1').val();
		var token2val = $('input#token2').val();
		var myurlval = $('input#myurl').val();
		//Send value
		var sendval = $('input#send').val();
		
		// Process the form via AJAX
		if(hasError == false){
			$('#requestInfo form').append('<img src="images/loading.gif" alt="" id="loading" class="loading" />');
					
			var dataString = 'fname='+ inputname + '&lname=' + inputlname + '&email=' + inputemail + '&phone=' + inputphone + '&company=' + inputcompany + '&message=' + inputmessage + '&send=' + sendval;
  		
  		$.ajax({
  		  type: 'POST',
  		  url: 'request-info.php',
  		  dataType: 'json', 
  		  data: dataString,
  		  success: function(data) {
  	   	 $('#requestInfo form').append('<span class="notify"></span>');
  	  	  if(data.error === true){
						$('.notify').addClass('error').text(data.msg).hide().fadeIn(1500);
					} else {
						$('#requestInfo #loading').fadeOut().remove();
						$('.notify').addClass('success').text('Thank you for your inquiry. We will be in touch soon!').hide().fadeIn(1500);
						$('label.error').remove();
						$('.field input, .field textarea').removeClass('error').val('');
					}
  	    	$('#requestInfo #loading').fadeOut();
  	  	}, 
  	  	error:function (data, XMLHttpRequest, textStatus, errorThrown){
  	  		$('#requestInfo #submit').append('<span class="notify"></span>');
  	  	  if(data.error === true){
						$('.notify').addClass('error').text(data.msg).hide().fadeIn(1500);
					} else {
						$('.notify').addClass('success').text('Success!').hide().fadeIn(1500);
					}
  	  	  $('#requestInfo #loading').fadeOut();
  	  		$('#facilityRentalForm #submit').removeAttr('disabled').removeClass('disabled');
      	}
  		});
  	
  	} else {
  		$('#facilityRentalForm #submit').removeAttr('disabled').removeClass('disabled');
  	}
  	return false;	
	});
	}

});
