$(document).ready(function() {
	$("#practiceDropdown").hover(function() {
		practices.cancelTimer();
		$("#practiceChoices").slideDown();
	}, function() {
		practices.startTimer();
	});
	
	$("#practiceChoices").hover(function() {
		practices.cancelTimer();
	}, function() {
		practices.startTimer();
	});
	
	$("input, textarea").each(function() {
		switch($(this).attr("name")) {
			case "name":
				$(this).data("defaultVal", "Enter name...").defaultValue($(this).data("defaultVal"));
				break;
			case "phone":
				$(this).data("defaultVal", "Enter phone...").defaultValue($(this).data("defaultVal"));
				break;
			case "email":
				$(this).data("defaultVal", "Enter email...").defaultValue($(this).data("defaultVal"));
				break;
			case "comments":
				$(this).data("defaultVal", "Enter a brief description of your case...").defaultValue($(this).data("defaultVal"));
				break;
		}
	});
	
	$("#freeConsultation img").css("cursor", "pointer").click(function() {
		$form=$("#consultationForm");
		if(parseInt($form.css("left")) > 7) {
			$form.stop().animate({left: 7});
		} else {
			$form.animate({left: 205});
		}
	});
	
	$("form").submit(function() {
		$resultBox=$(this).find(".result").text("");
		$.post(
			$(this).attr("action"),
			$(this).serialize(),
			function(data) {
				if(data.success) {
					$resultBox.css("color", "#3C3");
					$resultBox.text(data.message);
				} else {
					$resultBox.css("color", "#C33");
					$resultBox.text(data.error);
				}
			},
			"json"
		);
		//Restore default values for empty inputs
		$(this).find("input[type=text], textarea").each(function() {
			if($(this).val()=="") {
				$(this).val($(this).data("defaultVal")).addClass("inactive");
			}
		});
		return false;
	});
	
	if($("#homepageSlideshow")[0]) {
		homeSlideshow.init();
	}
	
	$('.result').ajaxError(function() {
		$(this)
			.css("color", "#C33")
			.text("Protocol Error #002");
	});
});

var practices={
	timer: 0,
	
	startTimer: function() {
		//console.log("Starting timer");
		this.timer=window.setTimeout("practices.hideChoices()", 500);
	},
	
	cancelTimer: function() {
		//console.log("Canceling timer");
		window.clearTimeout(this.timer);
	},
	
	hideChoices: function() {
		//console.log("Closing Menu");
		$("#practiceChoices").slideUp();
	}
};

var homeSlideshow={
	numPics: 3,
	curPic: 1,
	delay: 7000,
	init: function() {
		window.setTimeout("homeSlideshow.rotate()", this.delay);
		
		for(i=1 ; i <= this.numPics ; i++) {
			$img=$("<img />").attr("src", this.buildUrl(i)).css("display", "none").appendTo("body");
			$img.remove();
		}
	},
	
	rotate: function() {
		this.curPic++;
		if(this.curPic > this.numPics) this.curPic=1;
		$newImg=$("<img />").attr("src", this.buildUrl()).css("display", "none").appendTo("#homepageSlideshow").addClass("fadingIn").fadeIn(1000, function() {
			$(this).removeClass("fadingIn");
			window.setTimeout("homeSlideshow.rotate()", homeSlideshow.delay);
		});
		$("#homepageSlideshow img:eq(0)").fadeOut(1000, function() {
			$(this).remove();
		});
	},
	
	buildUrl: function(picNum) {
		if(typeof(picNum)=="undefined") picNum=this.curPic;
		return "images/homepage/"+picNum+".jpg";
	}
};
