// Use a bad browser, get a bad hack
if ( /msie 6/i.test(navigator.userAgent) )
    document.write('<style type="text/css">.signup { padding-left: 85px !important; }</style>');

$(document).ready(function() {
	$.fn.idle = function(time)
	{
		var o = $(this);
		o.queue(function()
		{
			setTimeout(function()
			{
				o.dequeue();
			}, time);
		});
		return this;
	};
	
	$(".toast").idle(3000).slideDown('slow');
	
	jQuery.validator.addMethod("zipcode", function(zip) {
		// matches US ZIP code
		// allow either five digits or nine digits with an optional '-' between
		zip = zip.replace(/^\s+/, "");
		zip = zip.replace(/\s+$/, "");
		
		if(zip.length == 0) {
			return true;
		}
	
		if(zip.match(/^\d{5}([- ]?\d{4})?$/)) {
			return true;
		}
		
		return false;
	}, 
	
	"Please specify a valid US ZIP code");

	// validate signup form on keyup and submit
	var validator = $("#signup_form").validate({
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			zip: {
				required: true,
				zipcode: true
			}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
				error.appendTo( element.next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		},
		submitHandler: function() {
        	var form = $("#signup_form");
        	form[0].submit();

        	var toast = $(".toast");
    		toast.slideUp('slow');
    		toast.css("padding", "0");
    		toast.css("border", "0px");
            
            var thanks = $("#thanks");
            thanks.slideDown('slow');
    		thanks.idle(12000).fadeTo('slow', 0);
    	}
	});

});

