$(document).ready(function(){ 
        $(document).pngFix();
		
		$(".s1:even").css("margin-right", "25px");
		
		// Reset our signup form 
		
		$("#errMsg").hide();
		$("#errMsg").html("");
		
		$('#cssdropdown li.headlink').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); 
		});
		
		
		// handle call back forms onfocus and blur 
		
		$("#callback input").focus(function () {
         	$(this).parent(".fancy-input-box").css('background-image', 'url(images/fancy-input-box-glow.png)');
        	$("#callback").pngFix();
    	});
		
		$("#callback input").blur(function () {
         	$(this).parent(".fancy-input-box").css('background-image', 'url(images/fancy-input-box.png)');
        	$("#callback").pngFix();
    	});
				 
    // bind to the form's submit event 
    $('#callback').submit(function() {
		$('#successMessage').html("Sending message, please wait...");	
		$('#successMessage').show();						 

		var opts = { beforeSubmit: validate, 
					 success: messageSent,
					 url: "sendmessage.php",
					 clearForm: false,
					 resetForm: false
					}									 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(opts); 
 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    	});
	
		$(".tweet").tweet({
            username: "dpaultaylor",
            join_text: "auto",
            avatar_size: null,
            count: 3,
            auto_join_text_default: "",
            auto_join_text_ed: "",
            auto_join_text_ing: "",
            auto_join_text_reply: "We replied to",
            auto_join_text_url: "We were checking out",
            loading_text: "loading tweets..."
        });
	
	}); // End of testing if dom is ready

/// Function Definitions Follow
		
		function validate(formData, jqForm, options) { 	 
			
			if (!isValidEmailAddress($("#callback input[name=footer-email]").val())) {
				alert("email invalid");
				return false;
			} else {return true; }
		}
		
		function messageSent() {
			$('#successMessage').html('Message sent. We\'ll be in touch'); 
		}
		
		function handleSignup() {
	
		if (isValidEmailAddress($("#signupname").val())) {
			callemailAPI();
		} else
		{
			$("#errMsg").html("Sorry, this email is not valid");
			$("#errMsg").css("color","red");
			$("#errMsg").show();
			return false;
		}
	
						
		} // end handle signup

		function callemailAPI()
		{
				jQuery.ajax({
					type: "POST",
					data: $("#signupname").serialize(),
					url: "Scripts/checkexists.php", // the location of the above mentioned php script
					cache: false,
					dataType: "xml",
					success: function(xml) {
						var _data = "";
						$("response", xml).each(function(){
							_data = jQuery("data", this).text();
							if (_data != "") {
								$("#errMsg").html("Sorry, this email is already registered");
								$("#errMsg").css("color","red");
								$("#errMsg").show();						
							} else {
								adde();
							}
						});
						} // end of success function
					});		
			}



			function adde() {
						jQuery.ajax({
							type: "POST",
							data: $("#signup").serialize(),
							url: "Scripts/adde.php", // the location of the above mentioned php script
							cache: false,
							dataType: "xml",
							success: function(xml) { 
								$("#errMsg").html("Cool, thanks for signing up!"); 
								$("#errMsg").css("color","#9bbfe5");
								$("#errMsg").show();
							} // end of success function
						});							
			
			}

			// Check if we have a valid email address
			
			
			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);
				}



