// --- SWF OBJECT EMBED --- //

var flashvars = {};
flashvars.name1 = "Gracie_SD";

var params = {};
params.menu = "false";
params.bgcolor = "#ede8d3";
params.wmode = "opaque";

var attributes = {};
attributes.id = "gracie-sd";
attributes.name = "gracie-sd";

swfobject.embedSWF("/wp/wp-content/themes/gracie-sd/flash/gracie-sd.swf", "gracie-sd", "720", "192", "9.0.0", "/wp/wp-content/themes/gracie-sd/flash/expressInstall.swf", flashvars, params, attributes);


// --- DOCUMENT READY FUNCTIONS --- //
$(document).ready(function() {


// --- DROP DOWN MENU > SUPER FISH --- //

$("ul.sf-menu").superfish({ delay: 500, animation: {opacity:'show'}, speed: 'fast', dropShadows: false }); 
$("ul ul li:last-child").addClass("last-subNav");


// --- AUTOGROW --- //

$('textarea').autogrow();


// validate the comment form when it is submitted
$("#commentform").validate();


// --- AJAX FORM POST --- //
$("#contact-form").submit(function() { 
	var formURL = this.getAttribute('action');
	var formID = this.getAttribute('id');
	var str = $(this).serialize();

	$.ajax({  
		type: "POST",
		url: formURL,
		data: str,
		timeout: 50000,
		beforeSend: function(){
			$("#"+formID).children('.msg').text("").append('<p>' + "Request Started..." + '<br /><img src=\"/wp/wp-content/themes/gracie-sd/images/global/loading.gif\" /></p>');
			if ($("#"+formID).children('.msg').is(":hidden")) { 
				$("#"+formID).children('.msg').slideDown(300);
			}
		},
		error: function(text, textStatus){
			$("#"+formID).children('.msg').ajaxError(function(event, request, settings){
				$("#"+formID).children('.msg').text("").append('<p>'+"Request Failed." + '<br />' + "Script URL: " + formURL + '</p>');
			});
		},
		success: function(data, textStatus){
			$("#"+formID).children('.msg').text("");
			$("#"+formID).children('.msg').ajaxComplete(function(event, request, settings){
				$("#"+formID).children('.msg').text("").append( data );
			});
		}/*,
		complete: function(){
			$("#"+formID).children('.msg').animate({opacity: 100.0}, 3000).slideUp(300);
			$("#"+formID).children('.msg').text("");
		}*/
		
	});  
  
return false;
});

$("#free-class-form").submit(function() { 
	var formURL = this.getAttribute('action');
	var formID = this.getAttribute('id');
	var str = $(this).serialize();

	$.ajax({  
		type: "POST",
		url: formURL,
		data: str,
		timeout: 50000,
		beforeSend: function(){
			$("#"+formID).children('.msg').text("").append('<p>' + "Request Started..." + '<br /><img src=\"/wp/wp-content/themes/gracie-sd/images/global/loading.gif\" /></p>');
			if ($("#"+formID).children('.msg').is(":hidden")) { 
				$("#"+formID).children('.msg').slideDown(300);
			}
		},
		error: function(text, textStatus){
			$("#"+formID).children('.msg').ajaxError(function(event, request, settings){
				$("#"+formID).children('.msg').text("").append('<p>'+"Request Failed." + '<br />' + "Script URL: " + formURL + '</p>');
			});
		},
		success: function(data, textStatus){
			$("#"+formID).children('.msg').text("");
			$("#"+formID).children('.msg').ajaxComplete(function(event, request, settings){
				$("#"+formID).children('.msg').text("").append( data );
			});
		}/*,
		complete: function(){
			$("#"+formID).children('.msg').animate({opacity: 100.0}, 3000).slideUp(300);
			$("#"+formID).children('.msg').text("");
		}*/
		
	});  
  
return false;
});

$("#newsletter-form").submit(function() { 
	var formURL = this.getAttribute('action');
	var formID = this.getAttribute('id');
	var str = $(this).serialize();

	$.ajax({  
		type: "POST",
		url: formURL,
		data: str,
		timeout: 50000,
		beforeSend: function(){
			$("#"+formID).children('.msg').text("").append('<p>' + "Request Started..." + '<br /><img src=\"/wp/wp-content/themes/gracie-sd/images/global/loading.gif\" /></p>');
			if ($("#"+formID).children('.msg').is(":hidden")) { 
				$("#"+formID).children('.msg').slideDown(300);
			}
		},
		error: function(text, textStatus){
			$("#"+formID).children('.msg').ajaxError(function(event, request, settings){
				$("#"+formID).children('.msg').text("").append('<p>'+"Request Failed." + '<br />' + "Script URL: " + formURL + '</p>');
			});
		},
		success: function(data, textStatus){
			$("#"+formID).children('.msg').text("");
			$("#"+formID).children('.msg').ajaxComplete(function(event, request, settings){
				$("#"+formID).children('.msg').text("").append( data );
			});
		}/*,
		complete: function(){
			$("#"+formID).children('.msg').animate({opacity: 100.0}, 3000).slideUp(300);
			$("#"+formID).children('.msg').text("");
		}*/
		
	});  
  
return false;
});

// --- END DOCUMENT READY FUNCTIONS --- //
}); 

// --- CAMPAIGN MONITOR AJAX FORM --- //
	$(function()
	{
		// -- NEWSLETTER -- //
		$("#newsletter-form-cm").submit(function() {	
			
			// First, disable the form from submitting
			$('form#newsletter-form').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#newsletter-form").attr("action");
			var formID = this.getAttribute('id');
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			id = "duhluk";
			emailId = id + "-" + id;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return false;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#newsletter-form").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "/wp/wp-content/themes/gracie-sd/scripts/proxy.php",
				type: "POST",
				data: final,
				success: function(data){
					//Check to make sure that the email was accepted
					if (data.search(/invalid/i) != -1) {
						alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
					}
					else
					{
						//$("#theForm").hide(); // If successfully submitted hides the form
						$("#"+formID).children(".confirmation").slideDown(300);  // Shows "Thanks for subscribing" div
						$("#"+formID+" ol").hide();
					}
				}
			});
			return false;
		});
		// -- MEMBERSHIP -- //
		$("#membership-form").submit(function() {	
			
			// First, disable the form from submitting
			$('form#membership-form').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#membership-form").attr("action");
			var formID = this.getAttribute('id');
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			id = "duhrll";
			emailId = id + "-" + id;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return false;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#membership-form").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "/wp/wp-content/themes/gracie-sd/scripts/proxy.php",
				type: "POST",
				data: final,
				success: function(data){
					//Check to make sure that the email was accepted
					if (data.search(/invalid/i) != -1) {
						alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
					}
					else
					{
						//$("#theForm").hide(); // If successfully submitted hides the form
						$("#"+formID).children(".confirmation").slideDown(300);  // Shows "Thanks for subscribing" div
						$("#"+formID+" ol").hide();
					}
				}
			});
			return false;
		});
		// -- INTRO CLASS -- //
		$("#intro-class-form").submit(function() {	
			
			// First, disable the form from submitting
			$('form#intro-class-form').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#intro-class-form").attr("action");
			var formID = this.getAttribute('id');
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			id = "hridji";
			emailId = id + "-" + id;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return false;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#intro-class-form").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "/wp/wp-content/themes/gracie-sd/scripts/proxy.php",
				type: "POST",
				data: final,
				success: function(data){
					//Check to make sure that the email was accepted
					if (data.search(/invalid/i) != -1) {
						alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
					}
					else
					{
						//$("#theForm").hide(); // If successfully submitted hides the form
						$("#"+formID).children(".confirmation").slideDown(300);  // Shows "Thanks for subscribing" div
						$("#"+formID+" ol").hide();
					}
				}
			});
			return false;
		});
	});
	
	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);
	}
