$(function() {
	if ($.browser.msie) {
		$("#main a").click(function(){
			var pdf = /.pdf/;
			var link = $(this).attr("href");
			if (link.match(pdf)) {
				$(this).attr("target","_blank");
			}
		});
	}
	
	// Find where we are
	var page_name = location.pathname;
	var section = page_name.substr(1,3);

	if (section != "") {
		// Build the menu
		$("#flash img").after('<ul id="subNav"></ul>');
		getMenu(section, page_name);
	}
});

function getMenu(section, page_name) {
	// Starts the AJAX
	$.get("/inc/menu.php", { section: section},
		// If we get data back...
		function(data) {
			// Append the data to #subnav
			$("#subNav").append(data);
			// Hide and then animate the nav in
			$("#subNav").width(0).animate({width:289},600);
			// Hide any sub-subnav
			$(".subSubNav ul").hide();
			showMenu(page_name,0);

			// On hover...
			$(".subSubNav").hover(function(){
				var lh = $(this).css("height");
				if (lh == "19px" || $.browser.opera) {
					// Hide all sub-subnav
					$(".subSubNav").height(19);
					$(".subSubNav ul").hide();
					
					showMenu(page_name);

					// Count how many LIs are in the sub-subnav
					var c = $(this).children("ul").children("li").length;
					//c + 1 to add itself! - 2 at the end because I have no idea!
					var h = ((c + 1) * 21) + (c * 2) - 2;
					// Make the height big enough to show the whole list
					$(this).height(h).css("overflow","visible");
				
					//Hide Children LI
	        		$(this).children("ul").children("li").hide(0);
        
	        		$(this).children("ul").slideDown(1, function() {
	          			$(this).children("li").slideDown(250)
	        		});
				}
			}, function(){
				showMenu(page_name,1);
			});
		}
	)
}

function showMenu(page_name,condition) {
	$.each($(".subSubNav ul li a"), function() {
		if ($(this).attr("href")==page_name){
			
			$(".subSubNav").height(19);
			$(".subSubNav ul").hide();
		
			// Count how many LIs are in the sub-subnav
			var c = $(this).parent().parent().children("li").length;
			//c + 1 to add itself! - 2 at the end because I have no idea!
			var h = ((c + 1) * 21) + (c * 2) - 2;
			// Make the height big enough to show the whole list
			$(this).parent().parent().parent().height(h).css("overflow","visible");

			$(this).parent().show().parent().show();
		}
	});
}