(function($) {
	$(document).ready(function() {
		$("#header .mod_navigation li.submenu").hover(function() {
			$("ul.level_2", this).show();
		}, function() {
			$("ul.level_2", this).hide();
		})
		
		var last_tab_id = 1;
		$(".tabs").each(function() {
			var tab_container = $(this);
			var tab_head = $('<div class="head"></div>');
			tab_head.prependTo(tab_container);
			tab_container.children(".block").each(function() {
				var tab = $(this);
				var id = "tab"+(last_tab_id++);
				
				var handle = tab.find("h1:first").remove().appendTo(tab_head);
				handle.attr("class", tab.attr("class"));
				handle.wrapInner("<span></span>");
				handle.click(function() {
					tab_container.children(".tab").removeClass("active").hide();
					$("#"+id).addClass("active").show();
					tab_head.children().removeClass("active");
					$(this).addClass("active");
				});
				
				tab.attr("id", id);
				tab.addClass("tab");
			});
			
			tab_container.children(".tab").hide();
			tab_container.children(".tab:first").addClass("active").show();
			tab_head.children("h1:first").addClass("active");
		});
        
        
        
		// Single Foldout 
		$(".foldout .foldout_heading").toggle(function() {
		    $(this).parent().addClass("active").find(".foldout_content").slideDown();
		    $(this).parent().addClass("active").find(".foldout_content_noshadow").slideDown();
		    }, function() {
		    $(this).parent().removeClass("active").find(".foldout_content").slideUp();
		     $(this).parent().removeClass("active").find(".foldout_content_noshadow").slideUp();
		});
		
		// Toggle All
		$(".foldout_all").toggle(function() {
			$(this).siblings(".archivetree").find(".heading").each(function() {
				$(this).parent().addClass("active").children(".content, .content_noshadow, .subitems").slideDown();
				$(this).find(".arrow").addClass("active");
			});
			$(this).siblings(".foldout").find(".foldout_heading").each(function() {
				$(this).parent().addClass("active").children(".foldout_content, .foldout_content_noshadow").slideDown();
				$(this).find(".arrow").addClass("active");
			});
			$(".foldout_all .inside").html('Close all');
		}, function() {
			$(this).siblings(".archivetree").find(".heading").each(function() {
				$(this).parent().removeClass("active").children(".content, .content_noshadow, .subitems").slideUp();
				$(this).find(".arrow").removeClass("active");
			});
			$(this).siblings(".foldout").find(".foldout_heading").each(function() {
				$(this).parent().removeClass("active").children(".foldout_content, .foldout_content_noshadow").slideUp();
				$(this).find(".arrow").removeClass("active");
			});
			$(".foldout_all .inside").html('Open all');
		});
		
		
		   // FPE ARCHIVE 
		   
		$(".archivetree .content, .archivetree .subitems").hide();
		
		$(".heading").toggle(function() {
		    $(this).parent().addClass("active").children(".content, .content_noshadow, .subitems").slideDown();
		    $(this).find(".arrow").addClass("active");
		}, function() {
		    $(this).parent().removeClass("active").children(".content, .content_noshadow, .subitems").slideUp();
		    $(this).find(".arrow").removeClass("active");
		});
		
	});
})(jQuery);
