$(document).ready(function(){

	$('a[href="#contact"]').click(function() {
		showContactForm();
 return false;	});
	
	$('.closecontact').click(function() {
		hideContactForm();
	});


	function showContactForm() { 
		$('.contactbutton').animate({
			marginTop: -50
		},200, 'swing', function() {
			$('#contact').animate({
				top: 0	
			}, 500, 'swing', function() {
				//complete
			});
		});
		return false;
	}
	
	function hideContactForm() {
		$('#contact').animate({
			top: -702
		},350, 'swing', function() {
			$('.contactbutton').animate({
				marginTop: 0	
			}, 200, 'swing', function() {
				//complete
			});
		});
		return false;
	}
	
	
	$('#factsheetselector').change(function() {
		if ($(this).val() === "") {
			$('.downloadfactsheet').attr('href','#');
			$('.downloadfactsheet').css('opacity','0.5');
		} else {
			$('.downloadfactsheet').css('opacity', '1');
			$('.downloadfactsheet').attr('href','pdf/'+$(this).val()+'.pdf');
		}
	});
	
	$('.contactsubmit').click(function() {
		errorcount = 0;
		$('.error').removeClass('error');
		$('#errormessage').fadeOut('150');
		$('label:contains("*")').each(function() {
			//if a label has a * in it, and the next input is empty
		    if ($(this).next('input,select').val() == "") {
				//add .error to the label and input
				$(this).addClass('error');
				$(this).next('input,select').addClass('error');
				$('#errormessage').fadeIn('150');
				//+1 to the error count
				errorcount = errorcount + 1;
			} else {
			
			}
		});
		
		//check for errors
		if (errorcount === 0) {
			//submit the form (normal action) if no errors
		} else {
			//Otherwise: NO SUBMIT!
			return false;
		}
	});
	
	//check if required, errored fields are edited correctly
	$('input').blur(function(){
		if ($(this).hasClass('error') == false) {
			return;
		} else {
			if (!$(this).val()) {
				return;
			} else {
				$(this).removeClass('error');
				$('label[for="'+$(this).attr('id')+'"]').removeClass('error');
			}
		}
	});
	
	//page nav indicator
	thisPage = location.href.substring((location.href.lastIndexOf("/"))+1);
	if (thisPage.indexOf("#")>0){
		thisPage = thisPage.substring(0,thisPage.indexOf("#"));
	}
	
	if (thisPage.indexOf("?")>0){
		thisPage = thisPage.substring(0,thisPage.indexOf("?"));
	}
	
	$('#sidenav li').removeClass('current');

	$("#sidenav li a[href='"+thisPage+"']'").each(function() {
		$(this).parent().addClass('current');
	});
	
	$("#sidenav li ul li a[href='"+thisPage+"']'").each(function() {
		$(this).parent().parent().parent().addClass('current');
	});
	
    $("#sidenav li a[href='"+thisPage+"']'").each(function() {
          $(this).parent().addClass('current');
    });
	
	thisTopLevel = $('#sidenav li:first-child a').attr('href');
	$('#nav li a[href="'+thisTopLevel+'"]').parent().addClass('current');

	
	
});
