$(document).ready(function() {
	
	var animating = false;
	var t;
	
	// calculate max height
	max = 0;
	$("ul#menu-home li").each(function() {
		if($(this).height() > max) { max = $(this).height(); }
	});
	
	// set menu items to be the same height
	$("ul#menu-home li").each(function() {
		$(this).find("span.description").css("height", (max-65) + "px");
	});
	
	// fit menu container to height of the highest menu item
	$("ul#menu-home").css("height", max + "px");
	
	$("ul#menu-home").mouseleave(function() {
		t = setTimeout(function() {
			closeActiveMenu();
			$("div#hero img").fadeOut(500);
			$("div#hero img.active").fadeIn(500);
			$("div#hero .hero").fadeIn(500);
		}, 2000);
	});

	// homepage menu
	$("ul#menu-home li").mouseover(function() {
		
		clearTimeout(t);
		
		// return immediately if something is animating
		if(animating) { 
			return false;
		}
		
		if(!$(this).hasClass("active")) {
			
			// if there's an active menu item, reset it
			if($("ul#menu-home li.active").length) {
				closeActiveMenu();			
			}
			
			// work out current left margin - 10px (for border)
			leftOver = $(this).css("margin-left");
			leftOver = parseInt(leftOver.substring(0, leftOver.length-2));
			leftOver = (leftOver-10) + "px";
		
			// put this menu item *behind* the others, so the border doesn't overlap the other menu items
			$(this).addClass("active");
		
			// add border and change margin
			$(this).css("border-left", "10px solid #FFF").css("border-right", "10px solid #FFF").css("margin-left", leftOver);
			
			// add bottom coloured border
			$(this).find("div").addClass("active");
		
			// animate up
			animating = true;
			$(this).animate({
				marginTop: "-27px"
			}, 200, "easeInQuad", function() {
				animating = false;
			});		
			$(this).find("span.more").slideDown(200, "easeInQuad");
			
			// change image
			id = $(this).attr("id");
			$("div#hero img").fadeOut(500);
			$("div#hero img." + id).fadeIn(500);
			$("div#hero .hero").fadeOut(500);
		}
		
	});
	
	// section homepages, make box clickable
	$("ul#section-boxes li").click(function() {
		window.location.href = $(this).find("a").attr("href");
	});

	// section homepages, make boxes equal height
	$("ul#section-boxes li.first").each(function() {
		first = $(this).height();
		last = $(this).next().height();
		if(last!=null) {
			if(last > first) { $(this).css("height", last+"px"); }
			if(last < first) { $(this).next().css("height", first+"px"); }
		}
	});

	// latest news, make row clickable
	$("div#latest-news ul li").click(function() {
		window.location.href = $(this).find("a").attr("href");
	});

	// section homepages, make boxes equal height
	$("ul.premier-member-listing li.first").each(function() {
		first = $(this).height();
		last = $(this).next().height();
		if(last!=null) {
			if(last > first) { 
				full = last - $(this).find("a").height();
				half = full/2;
				$(this).find("a").css("padding-top", half+"px").css("padding-bottom", half+"px");	
			}
			if(last < first) {
				full = first - $(this).next().find("a").height();
				half = full/2;
				$(this).next().find("a").css("padding-top", half+"px").css("padding-bottom", half+"px");
			}
			// set heights to be equal, as .5px padding doesn't work in IE so boxes will not be same height
			$(this).css("height", $(this).next().height() + "px");			
		}	
	});

	// interior fancybox
	$("div#secondary a:not(.profile-link)").fancybox({
		"overlayOpacity": 0.8
	});

	// latest news
	$("div#latest-news ul").cycle({
		fx: "scrollUp",
		pause: 1,
		timeOut: 8000,
		speed: 2000,
		slideExpr: "li"
	});
	$("div#latest-news").css("padding-bottom", "0");
	
	// replace app form submit button
	$("input#application-form-submit").hide().after("<a href=\"#\" class=\"btn\" id=\"application-form-submit-alt\"><span><span>Continue &raquo;</span></span></a>");
	$("#application-form-submit-alt").click(function() { $("#application-form-form").submit(); return false; });
	
	$("input.application-form-send").hide().after("<a href=\"#\" class=\"btn application-form-send-alt\"><span><span>Send Application</span></span></a>");
	$(".application-form-send-alt").click(function() { $("#application-form-form").submit(); return false; });
	
	$("input.declaration-send").hide().after("<a href=\"#\" class=\"btn declaration-send-alt\"><span><span>Send Annual Declaration</span></span></a>");
	$(".declaration-send-alt").click(function() { $("#application-form-form").submit(); return false; });
	
	// profile button
	$("input#profile-submit").hide().after("<a href=\"#\" class=\"btn\" id=\"profile-submit-alt\"><span><span>Update Profile &raquo;</span></span></a>");
	$("#profile-submit-alt").click(function() { $("#application-form-form").submit(); return false; });

	// replace search button
	$("input#search-form-submit").hide().after("<a href=\"#\" class=\"btn\" id=\"search-form-submit-alt\"><span><span>Go</span></span></a>");
	$("#search-form-submit-alt").click(function() { $("#search-form").submit(); return false; });
	// fit box and button
	w = $("#search-form-submit-alt").width();
	$("#header-search").css("width", 220 - w + "px");
	// clear search
	$("#header-search").focus(function() {
		if($(this).val()=="Search this site...") {
			$(this).val("");
		}
	});
	
	// add class to checkboxes
	$("input").each(function() {
		if($(this).attr("type")=="checkbox") {
			$(this).addClass("checkbox");
		}
	});
	
	// corporate members accordion
	$(".corporate").find("div.corporate-content").hide();
	$(".corporate h3 a").click(function() {
		$(this).parent().next().slideToggle("fast");
		$(this).parent().parent().toggleClass("open");	
		return false;
	});
	
	// profile - courses accordion
	$(".profile-courses").find("div").hide();
	$(".profile-courses h4 a").click(function() {
		if(!$(this).hasClass("no-description")) {	
			$(this).parent().next().slideToggle("fast");
			$(this).toggleClass("open");	
		}
		return false;
	});
	
	// profile - photo slideshow
	$(".profile-photos").cycle({
		fx: "fade",
		pause: 1,
		timeOut: 10000,
		speed: 2000,
		slideExpr: "li"
	});
	$(".profile-photos a").fancybox({
		"overlayOpacity": 0.8
	});
	$(".profile-current").fancybox({
		"overlayOpacity": 0.8
	});

});

function closeActiveMenu() {
	
	// reset appearance of active menu item
	$("ul#menu-home li.active").find("div").removeClass("active");
	$("ul#menu-home li.active").find("span.more").hide();

	// reset margins of active menu item
	$("ul#menu-home li.active").css("border-left", "none").css("border-right", "none");

	leftOld = $("ul#menu-home li.active").css("margin-left");
	leftOld = parseInt(leftOld.substring(0, leftOld.length-2));
	leftOld = (leftOld + 10) + "px";
	
	$("ul#menu-home li.active").css("margin-left", leftOld);
	$("ul#menu-home li.active").css("margin-top", "0px");
	
	// remove the old active item
	$("ul#menu-home li.active").removeClass("active");
	
}
