jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};

function scroll_to(element, time){
	$('html, body').animate({
	scrollTop: $(element).offset().top - 150
	}, time);
	}
	
$().ready(function() {
	
	//ajax form submit
	
	
	$("a.email-button").click(function(){
		scroll_to( 'form#email', 2000);
	});
	
	var options = { 
        target:        '#form-success',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
 		resetForm: false
    }; 
 


	// pre-submit callback 
	function showRequest(formData, jqForm, options) { 

	    // formData is an array; here we use $.param to convert it to a string to display it 
	    // but the form plugin does this for you automatically when it submits the data 
	    var queryString = $.param(formData); 

		$("div#form-error").hide();
		$("div#form-loading").show().html('<img src="http://'+document.location.host+'/images/loading.gif" height="23">');
			myTimer = $.timer(2000,function(){
				$("div#form-loading").fadeOut("fast"); // hide the loading gif after 2 seconds
			});
			
	    return true; 
	} 

	// post-submit callback 
	function showResponse(responseText, statusText)  { 
		myTimer = $.timer(2000,function(){
			$("div#form-success").fadeOut("fast").fadeIn("fast").fadeTo(3000, 1.0); // reveal the success after 2 seconds (e.g. when the loading gif has gone)
		});
	}

	
	// validate the comment form when it is submitted
	
	$("form.onclick-reset input:text, form.onclick-reset textarea").each(function(){
	var default_val = $(this).val();
		$(this).focus(function(){
			if ($(this).val() == default_val) $(this).val("");
		});
		$(this).blur(function(){
			if ($(this).val() == "") $(this).val(default_val);
		});
	});

	jQuery.validator.addMethod("nameRequired", function(value, element) {
		return value != element.defaultValue;
	}, "");

	jQuery.validator.addMethod("messageRequired", function(value, element) {
		return value != element.defaultValue;
	}, "");

	jQuery.validator.messages.required = ""; 
	//$("#message-form").validate();
	$("form#email, form.validate").validate({
				rules: {
					name: "nameRequired",
					message: "messageRequired",
					email: {
						required: true,
						email: true
					}
				},
				invalidHandler: function(e, validator) {
					var errors = validator.numberOfInvalids();
					if (errors) {
						var message = errors == 1
						? 'You missed 1 field. It has been highlighted above'
						: 'You missed ' + errors + ' fields. They have been highlighted above';
						$("div#form-error").html('<p>'+message+'</p>');
						$("div#form-message").hide();
						$("div#form-error").fadeIn("fast");
					}
				},
				onkeyup: false,
				submitHandler: function(form) {
					$("div#form-error").fadeOut("fast");
					$(form).ajaxSubmit(options); 
					
					
					//alert("submit! use link below to go to the other step");
				},
				messages: {
					name: "",
					message: "",
					email: ""
					},
				debug: true
			});
	
});
