$(function(){
	
	var body = $(document.body),
		url = window.location,
		hashes = ['#lightfilm', '#lightfilm-applications', '#about-flex', '#contact'];
	
	var toggleHome = function(animate){
		animate = animate !== false ? true : false;
		if (animate) {
			$('#home').animate({
				height:0,
				opacity:0
			}, 200);
			$('nav').animate({
				bottom:25
			}, 500);
			$('header .wrap').animate({
				height:210
			}, 500, function(){
				body.addClass('closed');
			});
		} else {
			$('#home').hide();
			$('nav').css('bottom', 25);
			$('header .wrap').height(210);
			body.addClass('closed');
		}
	};
	
	$('#content .page').each(function(){
		var page = $(this);
		page.height(page.outerHeight());
	});
	
	$('#content').height(0);
	
	$('nav a, #home a, #secondaryNav a').click(function(e){
		if (jQuery.inArray($(this).attr('href'), hashes) === -1) return;
		e.preventDefault();
		var animate = $(this).attr('href') === '#contact' ? false : true;
		if (!body.hasClass('closed')) toggleHome(animate);
		var id = $(this).attr('href'),
			content = $('#content'),
			page = $(id),
			contact = false;
		if (id === '#contact') {
			id = '#about-flex';
			page = $(id);
			contact = true;
		}
		$('nav a').parent().removeClass('selected');
		$('nav a[href="' + id + '"]').parent().addClass('selected');
		content.height(page.height());
		content.scrollTo(page, 500, {
			onAfter: function(){
				if (contact) {
					$.scrollTo('#contact', 500);
				} else {
					$.scrollTo(0, 200);
				}
			}
		});
	});
	
	$('#how-it-works > ul > li > a').click(function(e){
		e.preventDefault();
		if ($(this).parent().hasClass('selected')) return;
		var id = $(this).attr('href');
		if (!id || id === '' || $(id).length === 0) return;
		$(this).parent().siblings().removeClass('selected');
		$(this).parent().addClass('selected');
		if (id === '#front-lighting') {
			$('#back-lighting').fadeOut(500);
			$('#features-benefits > ul li.invisible').show();
			$('#features-benefits > ul li.front-lighting').show();
			$('#features-benefits > ul li.back-lighting').hide();
		} else {
			$('#front-lighting').fadeOut(500);
			$('#features-benefits > ul li.invisible').hide();
			$('#features-benefits > ul li.front-lighting').hide();
			$('#features-benefits > ul li.back-lighting').show();
		}
		// adjust content height
		var page = $(this).closest('.page');
		page.height('auto');
		page.height(page.outerHeight());
		$('#content').height(page.height());
		
		$(id).fadeIn(500);
	});
	
    $('#subscribeForm').submit(function(e) {
        e.preventDefault();
        var self = this;
        $.getJSON(this.action + "?callback=?", $(this).serialize(), function(data) {
			if (data.Status === 400) {
				$('> p', self).text('Please enter a valid e-mail address and try again.');
			} else { // 200
				$('> ul', self).fadeOut(500, function(){
					$('<p class="thanks">Thanks! We&rsquo;ll keep you updated on our newest discoveries.</p>').appendTo(self);
				});
			}
		});
    });
    
    if (url.hash !== '' && jQuery.inArray(url.hash, hashes) !== -1) {
    	if (!body.hasClass('closed')) toggleHome(false);
		var id = url.hash,
			content = $('#content'),
			page = $(id),
			contact = false;
		if (id === '#contact') {
			id = '#about-flex';
			page = $(id);
			contact = true;
		}
		$('nav a').parent().removeClass('selected');
		$('nav a[href="' + id + '"]').parent().addClass('selected');
		content.height(page.height());
		content.scrollTo(page, 500, {
			onAfter: function(){
				if (contact) {
					$.scrollTo('#contact', 500);
				}
			}
		});
    }
	
});
