/*
Bones Scripts File
Author: Eddie Machado

This file should contain any js scripts you want to add to the site.
Instead of calling it in the header or throwing it inside wp-head()
this file will be called automatically in the footer so as not to 
slow the page load.

*/

// Modernizr.load loading the right scripts only if you need them
Modernizr.load([
	{
    // Let's see if we need to load selectivizr
    test : Modernizr.borderradius,
    // Modernizr.load loads selectivizr for IE6-8
    nope : ['selectivizr-min.js']
	}
]);

/* imgsizer (flexible images for fluid sites) */
var imgSizer={Config:{imgCache:[],spacer:"/path/to/your/spacer.gif"},collate:function(aScope){var isOldIE=(document.all&&!window.opera&&!window.XDomainRequest)?1:0;if(isOldIE&&document.getElementsByTagName){var c=imgSizer;var imgCache=c.Config.imgCache;var images=(aScope&&aScope.length)?aScope:document.getElementsByTagName("img");for(var i=0;i<images.length;i++){images[i].origWidth=images[i].offsetWidth;images[i].origHeight=images[i].offsetHeight;imgCache.push(images[i]);c.ieAlpha(images[i]);images[i].style.width="100%";}
if(imgCache.length){c.resize(function(){for(var i=0;i<imgCache.length;i++){var ratio=(imgCache[i].offsetWidth/imgCache[i].origWidth);imgCache[i].style.height=(imgCache[i].origHeight*ratio)+"px";}});}}},ieAlpha:function(img){var c=imgSizer;if(img.oldSrc){img.src=img.oldSrc;}
var src=img.src;img.style.width=img.offsetWidth+"px";img.style.height=img.offsetHeight+"px";img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')"
img.oldSrc=src;img.src=c.Config.spacer;},resize:function(func){var oldonresize=window.onresize;if(typeof window.onresize!='function'){window.onresize=func;}else{window.onresize=function(){if(oldonresize){oldonresize();}
func();}}}}

// Validate Email - Function //////////////////////////////////////////////////////////
function isValidEmailAddress(emailAddress) {
						
	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(emailAddress);
	
} // end Validate email - function ////////////////////////////////////////////////////


// as the page loads, cal these scripts
$(document).ready(function() {

	
	
	// add all your scripts here
	/*****************************/
	$('.error').hide();
	
	/********* Anything Slider Hero Image *********************/
	$('#airplane-slideshow').cycle({		
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...	
		speed: 1500,
		timeout: 5000
	});
	
	
	/********* Form Elements **********************************/
	$("#contact-form #contact-submit").click(function() {
	
		$('.error').hide();
		// collect variables
		var firstName = $("input#firstName").val();
		var lastName = $("input#lastName").val();
		var email = $("input#email").val();
		var phone = $("input#phone").val();
		var source = $("select#source").val();
		var comments = $("textarea#comments").val();		
		
		//Validation
		if (firstName == "") {  
	      	$("#first-name-error").show();
	      	$("input#firstName").focus();  
	      	return false;  
    	}
    	
    	if (lastName == "") {  
	      	$("#last-name-error").show();
	      	$("input#lastName").focus();  
	      	return false;  
    	}
    	
    	if (email == "" || !isValidEmailAddress(email)) { 
	      	$("#email-error").show();
	      	$("input#email").focus();  
	      	return false;  
    	}
    	
    	if (phone == "") {  
	      	$("#phone-error").show();
	      	$("input#phone").focus();  
	      	return false;  
    	}
    	    	    	
		if (source == "Please Choose") {  
	      	$("#source-error").show();
	      	$("select#source").focus();  
	      	return false;  
    	}
    	
    	if (comments == "") {  
	      	$("#comments-error").show();
	      	$("textarea#comments").focus();  
	      	return false;  
    	}
		
		// Collect Data to send
		var dataString = 'firstName=' + firstName + '&lastName=' + lastName + '&email=' + email + '&phone=' + phone + '&source=' + source + '&comments=' + comments;
		
		//Send the form wit a little AJAX
    	$.ajax({
    		type: "POST",
    		url: "http://www.tigerairshows.com/wp-content/themes/tiger-airshows-bones/library/contact.inc.php",    		
    		data: dataString, 
    		success: function() {
    			$('#contact-form').fadeOut(1000, function() {
    				$('#contact-form').replaceWith("<div id='message'></div>");
    				$('#message').html("<h3 class='h3'>Information Sent!</h3>")
    				.append("<p>Your information has been sent to Tiger Airshows and you will be contacted as soon as possible by one of our pilots. If you would like, feel free to call 678-463-5944 at any time to talk to us directly.</p>")
    				.append("<h4 class='h4'>Thank you, " + firstName + "</h4>")
    				var $anchor = $('#post-34');
    				var top = -200;
    				$('html, body').animate({scrollTop: $anchor.position().top }, 'slow');
    				$('#contact-form-container').animate({height: '200'}, 'slow');
    			});
    		},
    		error: function() {
    			$('#contact-form').append("<h4 class='h4'>There was an error sending your form. Call us at 678-463-5944.</h4>")
    		}
    	
    	
    	});//end AJAX form
	
	return false;
	
	});//end contact form
 
}); /* end of as page load scripts */
