$(document).ready(function() {
	init();
	$("nav ul ul li").hover(function() {
		$(this).find("ul").fadeIn();
	},
	function() {
		$(this).find("ul").fadeOut();
	});
});

function init() {
	moreInfoBtn_prep();
	nav_prep();
	scroll();
}

function moreInfoBtn_prep() {
	var allBtns = $(".aside > div");
	$.each(allBtns,function(i,e) {
		var section = $(e).children(".section"),
			mtop = parseFloat(section.css("margin-top")),
			config = {
				over: function() {
					moreInfoBtn_anim(section,0)
				},
				out: function() {
					moreInfoBtn_anim(section,mtop)
				},
				timeout: 1
			};
		$(e)
			.hoverIntent(config);
	});
}

function moreInfoBtn_anim(elem,mtop) {
	elem
		.animate({
			marginTop: mtop
		},"fast");
}

function nav_prep() {
	var allLinks = $("nav > ul:first-child li");
	$.each(allLinks,function(i,e) {
		$(e)
			.prepend("<span></span>");
		var bg = $(e).children("span");
		if ($(e).parent().hasClass("sub-menu")) {
			var config = {
				over: function() {
					$(e).children("ul").stop().animate({opacity: 1}, 500);
				},
				out: function() {
					$(e).children("ul").stop().animate({opacity: 0}, 500);
				},
				timeout: 1,
				interval: 20
			};
		} else {
			var config = {
				over: function() {
					nav_anim(bg,0,"#fff");
					$(e).children("ul").stop().animate({opacity: 1}, 500);
				},
				out: function() {
					nav_anim(bg,"100%","#000");
					$(e).children("ul").stop().animate({opacity: 0}, 500);
				},
				timeout: 1,
				interval: 20
			};
		}
		$(e)
			.hoverIntent(config);
	});
}

function nav_anim(elem,btm,color) {
	var aTag = elem.siblings("a");
	aTag
		.animate({
			opacity: 0
		},50,function() {
			elem
				.animate({
					bottom: btm
				},"fast",function() {
					aTag
						.css("color",color)
						.animate({
							opacity: 1
						},50);
				});
		});
}

function scroll() {
	$(".scroll").click(function(event){
		$(".highlighted").removeClass("highlighted");

		//prevent the default action for the click event
		event.preventDefault();
		
		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;
		
		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];
		
		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		
		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
		$("#" + trgt).addClass("highlighted");
	});
	$(".top").click(function() {
		$(".highlighted").removeClass("highlighted");
	});
}
function toggleForm(type) {
	if(type == "summercamp") {
		$("#activeForm").fadeOut("fast",function() {
			$("#activeForm")
				.html($("#summerCampForm").html())
				.fadeIn("fast")
			validateForm();
		});
	} else if(type == "lessons") {
		$("#activeForm").fadeOut("fast",function() {
			$("#activeForm")
				.html($("#newStudentForm").html())
				.fadeIn("fast")
			validateForm();
		});
	} else if(type == "groupLessons") {
		$("#activeForm").fadeOut("fast",function() {
			$("#activeForm")
				.html($("#newStudentFormGroup").html())
				.fadeIn("fast")
			validateForm();
		});
	} else {
		$("#activeForm").fadeOut("fast",function() {
			$("#activeForm")
				.html($("#interviewForm").html())
				.fadeIn("fast")
			validateForm();
		});
	}
}
function validateForm() {
	$("#activeForm").validate({
		groups: {
			student: "Student_First_Name Student_Last_Name Student_Age",
			session: "Session_Length",
			instrument: "Instrument",
			phone1: "Home_Phone Work_Phone",
			phone2: "Mobile_Phone Other_Phone",
			parent: "Parent_First_Name Parent_Last_Name",
			address: "City Zip"
		},
		errorPlacement: function(error, element) {
			var errorMsg = $("<label class='errorArrow'></label>");
			if (element.attr("name") == "Student_First_Name" || element.attr("name") == "Student_Last_Name" || element.attr("name") == "Student_Age" ) {
				$("input[name='Student_Age']").after(errorMsg);
			} else if (element.attr("name") == "Session_Length") {
				$("input[name='Session_Length']").parents("p:first").append(errorMsg);
			} else if (element.attr("name") == "I_agree_to_abide_by_the_policies") {
				$("input[name='I_agree_to_abide_by_the_policies']").parents("p:first").append(errorMsg);
			} else if (element.attr("name") == "Instrument") {
				$("input[name='Instrument']").parents("p:first").append(errorMsg);
			} else if (element.attr("name") == "Home_Phone" || element.attr("name") == "Work_Phone" ) {
				$("input[name='Work_Phone']").after(errorMsg);
			} else if (element.attr("name") == "Mobile_Phone" || element.attr("name") == "Other_Phone" ) {
				$("input[name='Other_Phone']").after(errorMsg);
			} else if (element.attr("name") == "Parent_First_Name" || element.attr("name") == "Parent_Last_Name" ) {
				$("input[name='Parent_Last_Name']").after(errorMsg);
			} else if (element.attr("name") == "City" || element.attr("name") == "Zip" ) {
				$("input[name='Zip']").after(errorMsg);
			} else {
				$(element).after(errorMsg);
			}
		}
	});
}
