$(document).ready(function() {


	/* Smooth scrolling */
	var to, hs;
	$('a[href*=#]').click(function() {

		if (this.hash == "#gototop") {
			to = "0px";
			hs = ""
		}
		else {
			to = $(this.hash).offset().top;
			hs = this.hash
		}
		if (to) {
			$(window + ":not(:animated)").animate({ scrollTop: to }, { duration: 600, queue: false });
			window.location.hash = '/' + hs.replace("#", "")
		}
		return false;
	});
	var loadHash = window.location.hash.replace("/", "");
	if (loadHash != '' && loadHash != '#' && $(loadHash).length > 0) $(window + ":not(:animated)").animate({ scrollTop: $(loadHash).offset().top }, { duration: 600, queue: false });


	/* Menu */
	var my = parseFloat($("#menu").css("top"));
	var pt, cr, offset;
	$(window).scroll(function () {

		pt = 35;
		cr = parseFloat($(document).scrollTop());
		
		if (cr > my) offset = cr - pt;
		else offset = my;

		$("#menu").animate({ top: offset + "px"}, { duration: 600, queue: false })
	});

	$("#menu-open").hover(function() {
		$("#menu-holder").animate({ height: "110px" }, { duration: 100, queue: false });
		$("#menu ul").fadeIn(400)
	},
	function() {
		$("#menu ul").fadeOut(200, function() {
			$("#menu-holder").animate({ height: "0px" }, { duration: 100, queue: false })
		})
	});


	/* Services List */
	$("#services_list").prepend('<span id="services_select"></span>');
	
	$("#services_list dt a").unbind();
	$("#services_list dt a").click(function() {

		if ($(this).parent("dt").hasClass("selected") == false) {

			$("#services_list dt").removeClass("selected");
			$(this).parent("dt").addClass("selected");

			var target = this.hash;
			var selected = this;

			$("#services_list .service-selected" + ":not(:animated)").fadeOut(400, function() {
			
				$("#services_select").animate({ top: (parseFloat($(selected).position().top) + 5) + "px" }, 200);
			
				$(this).removeClass("service-selected");
				$(target).fadeIn(400, function() {
					$(this).addClass("service-selected")
				});
			});		
		}
		return false
	});


	/* Contact Form Validation and Send */
	var em;
	var rs = true;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	var createMessage = function(m) {
		if ($("#ferror").length > 0) $("#ferror").html(m);
		else $("#contact-submit").append('<p class="bob" id="ferror">' + m + '</p>');
	}
	
	$("#contact-form").submit(function() {

		var vname = $("#fname").val();
		var vemail = $("#femail").val();
		var vphone = $("#fphone").val();
		var vcomments = $("#fcomments").val();

		if (vname.length < 4) {
			em = "Please let us know your name";
			rs = false
		}
		else if (vemail.length < 5) {
			em = "Please provide a contact email";
			rs = false
		}
		else if (!emailReg.test(vemail)) {
			em = "Please provide a valid email";
			rs = false
		}
		else if (vphone.length != '' && vphone.length < 6) {
			em = "Please provide a valid phone";
			rs = false
		}
		else if (vcomments.length < 4) {
			em = "Please leave your comments";
			rs = false
		}

		if (rs == true) {
			$.post("/contact.php?a=1", { femail: vemail, fname: vname, fphone: vphone, fcomments: vcomments }, function(data){
				createMessage(data);
				if (data == "Message sent successfully") $("#fsubmit").attr("disabled", "true")
   			});		
		}
		else createMessage(em);
		
		return false;
	});
	

	/* Open .external links in new window */
	$("a.external").attr("target", "_blank");

});
