
// scrolling

function scrollAnimation() {
	var viewportHeight;

	// Store HTML references and offset outside of scroll because these are expensive calculations
	var joinUs = $("#joinus");
	var joinUsOffsetTop = joinUs.offset().top;
	var joinUsOffsetBottom = joinUs.offset().top + joinUs.height() + 150;

	var launch = $("#launch-day");
	var launchOffsetTop = launch.offset().top;
	var launchOffsetBottom = launch.offset().top + launch.height() + 30;

	//viewportHeight = $(window).height();
	viewportHeight = 500;

	function setActive(el) {
		$(el).addClass("active");
	};

	function setPassive(el) {
		$(el).removeClass("active");
	};

	// detecting the scroll location
	$(window).scroll(function(e) {
		var scrollTop = $(document).scrollTop() + viewportHeight - 200;		
		if (scrollTop >= launchOffsetTop && scrollTop <= launchOffsetBottom) {
			setActive($("ul#nav li#nav-launch a"));
		} else if (scrollTop >= joinUsOffsetTop && scrollTop <= joinUsOffsetBottom) {
			setActive($("ul#nav li#nav-joinus a"));
		} else {
			setPassive($("ul#nav li a"));						
		}		
	});

	jQuery.easing.def = "easeInOutQuad";

	// animating the scroll
	$("a.arrow-down, a.arrow-up, #nav-joinus a, #nav-launch a, #logo a").click(function(e) {
		e.preventDefault();
		var target = $(this).attr("href");

		var el = $(target);
		var offsetTop = el.offset().top;

		$('html, body').animate({
			scrollTop: offsetTop
		}, 1000);
	});
};


function setFormState() {
	var URL = window.location.href;
	
	var form = $("#joinus-form");
	var message = $("#signup-thanks");
	
	function checkAnchorHash() {
		var joinusRegex = /#signup-true/;
		console.log(joinusRegex)
		var joinusPosition = URL.search(joinusRegex);
		
		if (joinusPosition != -1) {
			setPageState("true")
			$("#nav-joinus a").trigger("click");
		}	
	}
	
	function checkFormState() {
		var isFormActive = true;

		// Read a page's GET URL variables and return them as an associative array.
		function getUrlVars()
		{
		    var vars = [], hash;
		    var hashes = window.location.href.slice(URL.indexOf('?') + 1).split('&');
		    for(var i = 0; i < hashes.length; i++)
		    {
		        hash = hashes[i].split('=');
		        vars.push(hash[0]);
		        vars[hash[0]] = hash[1];
		    }
		    return vars;
		}

		isFormActive = getUrlVars()["signup"];
		
		setPageState(isFormActive);
	}
	
	function setPageState(isFormActive) {
		if (isFormActive == "true") {
			$("#joinus .content").remove();
			form.remove();
			message.show();
		} else {
			form.show();
			message.remove();
		}
	}

//	checkFormState();
	checkAnchorHash();
};


// loaded when DOM is ready:

$(function() {
	scrollAnimation();
	setFormState();
});