﻿var App = [];
(function($) {
	App.els = {
		jumpnav: $('a[rel="subsection"]'),
		articles: $('article'),
		bios: $('.bio'),
		biosnav: $('#page-bios nav a'),
		cases: $('.case-study'),
		casesnav: $('#page-case-studies nav a'),
		contactform: $('#contactform')
	};
	App.vars = {
		contactheight: 0
	};
	function scrollPage(elid) {
		scrollfind = App.els.articles.filter('[data-hash="' + elid.replace('#','') + '"]');
		elid = '#' + scrollfind.data('hash');
    window.location.hash = elid;
		$('html,body').animate({
			scrollTop: scrollfind.offset().top - 58
		}, 200);
	}
	$('#submit-form').click(function(e) {
		e.preventDefault();
		App.els.contactform.find('input[type="text"]').each(function() {
			boolshowerror = false;
			if ($(this).val() == '') {
				$(this).addClass('field-error');
				boolshowerror = true;
			} else {
				$(this).removeClass('field-error');
			}
		});
		if (boolshowerror) {
			$('.form-error').fadeIn(100);
		} else {
			$('.form-error').hide();
			$.ajax({
				type: 'POST',
				url: '/inc/mail-send.asp',
				data: App.els.contactform.serialize(),
				success: function(msg) {
					App.els.contactform.html(msg);
				}
			});
		}
	});
	App.els.jumpnav.click(function(e) {
		//e.preventDefault();
		scrollPage($(this).attr('href'));
	});
	App.els.articles.filter(':last').addClass('last-item');
	App.els.articles.find('*:last-child').addClass('last-item');
	App.els.cases.filter(':not(:first)').hide();
	App.els.casesnav.click(function(e) {
		e.preventDefault();
		App.els.casesnav.removeClass('active');
		$(this).addClass('active');
		App.els.cases.hide();
		App.els.cases.filter($(this).attr('href')).fadeIn(200);
	});
	App.els.biosnav.click(function(e) {
		e.preventDefault();
		App.els.biosnav.removeClass('active');
		$(this).addClass('active');
		App.els.bios.hide();
		App.els.bios.filter($(this).attr('href')).fadeIn(200);
	});
	$('#pre-2006').hide();
	$('a[href="#pre-2006"]').click(function(e) {
		e.preventDefault();
		$('#pre-2006').slideToggle(100);
	});
	$(window).load(function() {
		if (location.hash) {
			scrollPage(location.hash);
		}
		App.vars.contactheight = App.els.articles.filter('[data-hash="contact-us"]').height();
	});
	/*$(document).ready(function() {
		if (location.hash) {
			scrollPage(location.hash);
		}
		App.vars.contactheight = App.els.articles.filter('[data-hash="contact-us"]').height();
	});*/
	// resize to fix contact sizing
	$(window).resize(function() {
		if ($(this).height() > App.vars.contactheight) {
			App.els.articles.filter('#contact-us').css({
				height: $(this).height()
			});
		} else {
			App.els.articles.filter('#contact-us').css({
				height: App.vars.contactheight
			});
		}
	});
})(jQuery);
