$(document).ready(function() {
	// load page
	$('#load').fadeOut(2000);

	// navigation
	var queryhash = window.location.hash
	switch (queryhash) {
		case "#forestry":
			document.title = "Summit Business Group - Forestry";
			initialShowForestry();
			break;
		case "#environmental":
			document.title = "Summit Business Group - Environmental Remediation";
			initialShowEnvironmental();
			break;
		case "#excavation":
			document.title = "Summit Business Group - Excavation";
			initialShowExcavation();
			break;
		case "#demolition":
			document.title = "Summit Business Group - Demolition";
			initialShowDemolition();
			break;
		case "#contact":
			document.title = "Summit Business Group - Inquiries & Professional Proposals";
			initialShowContact();
			break;
		case "#home":
			document.title = "Summit Business Group - SBG - Forestry, Environmental Remediation, Excavation, Demolition";
			initialShowHome();
			break;
		default:
			initialShowHome();
			break;
	}

	// form handler
	var options = {
		beforeSubmit:  testData, // pre-submit callback
		success: showResponse,  // post-submit callback
		clearForm: true        // clear all form fields after successful submit
	};
	$('#lead').submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	});
	function isEmail(email) {
		var re = /^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i;
		if (re.exec(email)) {
			return true;
		} else {
			return false;
		}
	}
	function testData(formData, jqForm, options) {
		var e = $('input[name=email]').fieldValue();
		var p = $('input[name=phone]').fieldValue();
		var n = $('input[name=name]').fieldValue();
		var m = $('textarea[name=msg]').fieldValue();
		if (!n[0]) {
			alert('It appears that you\'ve failed to include your name. Please provide your name so we know with whom we will be speaking with.');
			return false;
		}
		if (!e[0] || !p[0]) {
			alert('It appears that you\'ve failed to include a way for us to reach you. Please provide your phone number and email address so we can reply to your message.');
			return false;
		}
		if (e[0]) {
			if (!isEmail(e[0])) {
				alert('We\'re very sorry, but it appears your email address is invalid. If you believe this statement to be inaccurate, please call us at one of the numbers listed below:\n\nOffice: (717) 794-2446\nCellular: (301) 728-5887\n\nThank you for your consideration and understanding.');
				return false;
			}
		}
		if (!m[0]) {
			alert('It appears that you\'ve failed to include your message. Please let us know a little bit about how we can help you complete your project.');
			return false;
		}
	}
	function showResponse(responseText, statusText, xhr, $form)  {
		if (statusText == 'success') {
			alert(responseText);
			switch (responseText) {
				case "sent":
					alert('Thank you for taking the time to contact us. We\'ll be in touch with you very shortly. If your request is urgent, please call us at one of the numbers listed below:\n\nOffice: (717) 794-2446\nCellular: (301) 728-5887');
					break;
				case "error":
					alert('We\'re very sorry, but an error occurred when trying to submit your message. Please call us at one of the numbers listed below:\n\nOffice: (717) 794-2446\nCellular: (301) 728-5887\n\nThank you for your consideration and understanding.');
					break;
				default:
					alert('We\'re very sorry, but an error occurred when trying to submit your message. Please call us at one of the numbers listed below:\n\nOffice: (717) 794-2446\nCellular: (301) 728-5887\n\nThank you for your consideration and understanding.');
					break;
			}
		} else {
			alert('We\'re very sorry, but an error occurred when trying to submit your message. Please call us at one of the numbers listed below:\n\nOffice: (717) 794-2446\nCellular: (301) 728-5887\n\nThank you for your consideration and understanding.');
		}
	}

	// initialize
	$("h2").hide();
	$("#nav-forestry a").click(showForestry);
	$("#nav-environmental a").click(showEnvironmental);
	$("#nav-excavation a").click(showExcavation);
	$("#nav-demolition a").click(showDemolition);
	$("#nav-home a").click(showHome);
	$("#nav-contact a").click(showContact);
	$("a[rel='sbg-forestry']").colorbox({transition:"elastic", width:"85%", height:"85%"});
	$("a[rel='sbg-environmental']").colorbox({transition:"elastic", width:"85%", height:"85%"});
	$("a[rel='sbg-excavation']").colorbox({transition:"elastic", width:"85%", height:"85%"});
	$("a[rel='sbg-demolition']").colorbox({transition:"elastic", width:"85%", height:"85%"});
});

function initialShowHome() {
	$("#content").hide();
	$("#sbg").removeClass();
	$("#sbg").addClass("home");
	$(".node").hide();
	$("#home").show();
	setTimeout("$('#content').slideDown('slow');", 1500);
}

function initialShowForestry() {
	$("#content").hide();
	$("#sbg").removeClass();
	$("#sbg").addClass("forestry");
	$(".node").hide();
	$("#forestry").show();
	setTimeout("$('#content').slideDown('slow');", 1500);
}

function initialShowEnvironmental() {
	$("#content").hide();
	$("#sbg").removeClass();
	$("#sbg").addClass("environmental");
	$(".node").hide();
	$("#environmental").show();
	setTimeout("$('#content').slideDown('slow');", 1500);
}

function initialShowExcavation() {
	$("#content").hide();
	$("#sbg").removeClass();
	$("#sbg").addClass("excavation");
	$(".node").hide();
	$("#excavation").show();
	setTimeout("$('#content').slideDown('slow');", 1500);
}

function initialShowDemolition() {
	$("#content").hide();
	$("#sbg").removeClass();
	$("#sbg").addClass("demolition");
	$(".node").hide();
	$("#demolition").show();
	setTimeout("$('#content').slideDown('slow');", 1500);
}

function initialShowContact() {
	$("#content").hide();
	$("#sbg").removeClass();
	$("#sbg").addClass("contact");
	$(".node").hide();
	$("#contact").show();
	setTimeout("$('#content').slideDown('slow');", 1500);
}

function showForestry() {
	if ($("#sbg").hasClass("forestry")){ }
	else {
		document.title = "Summit Business Group - Forestry";
		$("#content").slideUp(500);
		$(".node").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#forestry').show();", 500);
		$("#content").slideDown(500);
		$("#sbg").removeClass();
		$("#sbg").addClass("forestry");
	}
}

function showEnvironmental() {
	if ($("#sbg").hasClass("environmental")){ }
	else {
		document.title = "Summit Business Group - Evironmental Remediation";
		$("#content").slideUp(500);
		$(".node").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#environmental').show();", 500);
		$("#content").slideDown(500);
		$("#sbg").removeClass();
		$("#sbg").addClass("environmental");
	}
}

function showExcavation() {
	if ($("#sbg").hasClass("excavation")){ }
	else {
		document.title = "Summit Business Group - Excavation";
		$("#content").slideUp(500);
		$(".node").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#excavation').show();", 500);
		$("#content").slideDown(500);
		$("#sbg").removeClass();
		$("#sbg").addClass("excavation");
	}
}

function showDemolition() {
	if ($("#sbg").hasClass("demolition")){ }
	else {
		document.title = "Summit Business Group - Demolition";
		$("#content").slideUp(500);
		$(".node").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#demolition').show();", 500);
		$("#content").slideDown(500);
		$("#sbg").removeClass();
		$("#sbg").addClass("demolition");
	}
}

function showHome() {
	document.title = "Summit Business Group - SBG - Forestry, Environmental Remediation, Excavation, Demolition";
	if ($("#sbg").hasClass("home")){ }
	else {
		$("#content").slideUp(500);
		$(".node").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#home').show();", 500);
		$("#content").slideDown(500);
		$("#sbg").removeClass();
		$("#sbg").addClass("home");
	}
}

function showContact() {
	if ($("#sbg").hasClass("contact")){ }
	else {
		document.title = "Summit Business Group - Contact Us";
		$("#content").slideUp(500);
		$(".node").fadeOut(500);
		setTimeout("$('.node').hide();", 500);
		setTimeout("$('#contact').show();", 500);
		$("#content").slideDown(500);
		$("#sbg").removeClass();
		$("#sbg").addClass("contact");
	}
}