$(document).ready(function(){
	// Make the resources page accordions
	if ($("BODY.resources").length > 0) {
		$("DIV.answerBox").hide();
		$("DIV.resourceList a").click(function (event) {
			var myItem = $(this).parent().next();
			if (myItem.is(":visible")){
				myItem.slideUp();
			}else{
				$("DIV.answerBox").not(myItem).slideUp();
				myItem.slideDown();
			}
			event.preventDefault();
		});
	};
	
	// Add code for the attorneys page
	if ($("BODY.attorneys").length > 0) {
		$("BODY.attorneys DIV.subcolspMid,DIV.subcolspRight").hide();
		var myHash = window.location.hash.toLowerCase();
		var myPages = ["jschultz","wdavis","gfearing","rschultz"];
		var myPage = myPages[Math.floor(myPages.length*Math.random())];
		$("DIV.attorneysList a").each(function () {
			var myItem = $(this).attr("href").toLowerCase();
			if (myHash == myItem) {
				myPage = $(this).attr("href").replace("#","");
			};
		});
		$("DIV.attorneysList." + myPage + " a").addClass("nofade");
		$("DIV.attorneysList." + myPage + " ").addClass("selected");
		
		$("BODY.attorneys DIV.subcolspMid."+myPage).fadeIn("slow");
		$("DIV.subcolspRight."+myPage).fadeIn("slow");
		$("DIV.attorneysList a").click(function () {
			
			var myItem = $(this).attr("href").replace("#",".");
			if (myItem.length > 3){
				$("DIV.attorneysList").removeClass("selected");
				$(this).parent().addClass("selected");
				$("DIV.subcolspMid,DIV.subcolspRight").not(myItem).hide();
				$("DIV.subcolspMid" + myItem).fadeIn("slow");
				$("DIV.subcolspRight" + myItem).fadeIn("slow");
				$("DIV.attorneysList a").removeClass("nofade");
				$(this).addClass("nofade");			
				$("DIV.attorneysList img.attorney").not("DIV.attorneysList a.nofade img").fadeTo(300,.5);
			};
		});
		
		// Add code for the hover
		$("DIV.attorneysList img.attorney").not("DIV.attorneysList a.nofade img").css("opacity",.5);
		$("DIV.attorneysList").hover(function () {
			$(this).find("img.attorney").not("DIV.attorneysList a.nofade img").stop().fadeTo(300,1);
		}, function () {
			$(this).find("img.attorney").not("DIV.attorneysList a.nofade img").stop().fadeTo(300,.5);
		});
	}
	
	// Add code for the home page attorney list and form validation
	if ($("BODY.home").length > 0) {
		$("DIV.attorneysListHome img.attorney").css("opacity",1);
		$("DIV.attorneysListHome").hover(function () {
			$(this).find("img.attorney").stop().fadeTo(300,.6);
		}, function () {
			$(this).find("img.attorney").stop().fadeTo(300,1);
		});
		
		// Validate the Contact form.
		$('#contactForm').submit(function() {
			var isValid = true;
			var myErrorMsg = $(this).attr("inputerror") + "<br>"
			$("#contactForm .required").each(function () {
				if ($(this).hasClass("email")) {
					if (!validateEmail($(this).val())) {
						myErrorMsg += $(this).attr("inputerror") + "<br>"
						isValid = false;
						$(this).addClass("error");
					} else {
						$(this).removeClass("error");
					};
				} else {
					if (!$(this).val().length > 0) {
						myErrorMsg += $(this).attr("inputerror") + "<br>"
						isValid = false;
						$(this).addClass("error");
					} else {
						$(this).removeClass("error");
					};
				}
			});
			if (!isValid) {
				$("#formMessage").html(myErrorMsg).slideDown();
			} else {
				$("#formMessage").html(myErrorMsg).slideUp();
			};
			if (isValid) {
				// scripts/sendmail.php
				$("#formMessage, #contactForm").fadeOut('fast', function(){
					$("#contactResponse").fadeIn();
				});
				$.post("scripts/sendmail.php", $("#contactForm").serialize(), 
					function(data){
						$("#contactResponse").html(data);
						
					}
				);
				
			};
			return false;
		});		
	};
});

function validateEmail(email) 
{ 
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
	return email.match(re) 
}

