(function ($) {
	var lead_url = "/registration/register-lead/",
		survey_url = "/registration/save-survey/",
		emailRegex = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)/i;

	function handleLead(result) {
		if (result && result.status === 200) {
			$('#wait [type=submit]').removeClass('loading').addClass('done').val('done');
			$('#info [name=email]').val($('#wait [name=email]').val());
			$('#littlemore').show(1000).scrollTo(1000);
		} else {
			if (result && result.error_description.indexOf('User with this email already exists') >= 0) {
				$("#wait fieldset").append('<span class="note srvmsg">You\'re already in.</span>');
			} else {
				$("#wait fieldset").append('<span class="note srvmsg">Oops - try later.</span>');
			}
			$('#wait [type=submit]').removeClass('loading').addClass('ok');
		}
	}
	function handleSurvey(result) {
		if (result && result.status === 200) {
			$('#littlemore').hide(1000);
			$('#thanks').show(1000).scrollTo(1000);
		} else {
			$("#info fieldset").append('<span class="note srvmsg">Oops - try later.</span>');
			$('#info [type=submit]').removeClass('loading');
		}
	}
	window.a = handleLead;
	window.b = handleSurvey;
	$.fn.extend({
		scrollTo: function (dur) {
			$('html,body').animate({scrollTop: this.eq(0).offset().top}, dur);
			return this;
		}
	});

	function addToWaitingList(ev) {
		var obj = {
			'email': $.trim($("#email").val()),
			'page': document.location.href,
			'referrer': document.referrer || ""
		};
		$(".srvmsg").remove();

		// Only send if we have email address
		if (obj.email && checkValidity(obj.email)) {
			$('#wait [type=submit]').removeClass('ok').addClass('loading');
			$.post(lead_url, obj, handleLead, "json");
		} else {
			$('#email').focus();
		}
		ev.preventDefault();
	}
	
	function submitSurvey(ev) {
		var obj = $(this).serialize();
		$(".srvmsg").remove();

		$('#info [type=submit]').addClass('loading');
		$.post(survey_url, obj, handleSurvey, "json");

		ev.preventDefault();
	}

	function checkValidity(v) {
		v = typeof v === 'string' && v || $('#email').val();
		if (v.match(emailRegex)) {
			$('#wait [type=submit]').addClass('ok');
			return true;
		} else {
			$('#wait [type=submit]').removeClass('ok');
			return false;
		}
	}
	
	$('#wait')
		.submit(addToWaitingList)
		.find('[type=submit]').focus(checkValidity).end()
		.find('#email')
			.keyup(function (ev) {
				checkValidity(this.value);
			})
			.focus(function (ev) {
				var e = $(this).parent().find('[type=submit]');
				if (e.hasClass('done')) {
					$('#littlemore').hide(300);
					e.removeClass('done').val('now');
					$(this).val('');
				}
				this.select();
			})
			.each(function () {
				checkValidity(this.value);
			})
			.end();
	$('#info')
		.submit(submitSurvey);
	$("#register").show(500);
	$('#littlemore .lotmore').hide();
	$('#lotmore').click(function (ev) {
		$(this).parent().hide();
		$('#littlemore .lotmore').show(500, function () {
			$(this).css('display', '');
		});
		ev.preventDefault();
	});
})(window.jQuery);

