$(document).ready(startTabs);
	
	$("body").addClass("hasjavascript");



	
	var isCitizen = false;
	var isEnglish = false;
	var isGeography = false;
	
	function hideAllTabs()
	{
	    
		$("#nonespecific-tab").hide();
		$("#citizenship-tab").hide();
		$("#english-tab").hide();
		$("#geography-tab").hide();
		
	}
	
	function hideAllResults()
	{
		$(".subject-wrapper-none").parent().parent().hide();
		$(".subject-wrapper-citizenship").parent().parent().hide();
		$(".subject-wrapper-english").parent().parent().hide();
		$(".subject-wrapper-geography").parent().parent().hide();
		$(".subject-wrapper-none").hide();
		$(".subject-wrapper-citizenship").hide();
		$(".subject-wrapper-english").hide();
		$(".subject-wrapper-geography").hide();
		
	}
	function hideResults(r)
	{
		
		$(r).each(function(){
			$(this).parent().parent().hide();
			$(this).hide();
		})
	}
	
	function showResults(r)
	{
		$(r).each(function(){
			$(this).parent().parent().css("display", "block");
			$(this).css("display", "block");
		})
	}
	
	function makeTab(s)
	{
		var subject = s + "-tab";
		$("#" + subject).css("display", "block");
	}
	
	function startTabs()
	{


	$(".view-resource-bank-search").addClass("area");
	$(".view-resource-bank-search").addClass("area1");
	$(".view-resource-bank-search-earlyyears").addClass("area");
	$(".view-resource-bank-search-earlyyears").addClass("area1");
	$(".view-resource-bank-school-teach").addClass("area");
	$(".view-resource-bank-school-teach").addClass("area1");
	$(".view-resource-bank-school-fundraise").addClass("area");
	$(".view-resource-bank-school-fundraise").addClass("area1");
	

		hideAllTabs();
		$('#resource-bank-tabs').prependTo('.view-content-resource-bank-search');
    	$('#resource-bank-tabs').prependTo('.view-content-resource-bank-search-earlyyears');
		$('#resource-bank-tabs').prependTo('.view-content-resource-bank-school-teach');
		$('#resource-bank-tabs').prependTo('.view-content-resource-bank-school-fundraise');

		$(".subject-wrapper > *").each(function() {
			if ($(this).hasClass('subject-wrapper-citizenship'))
			{
				isCitizen = true;
				makeTab('citizenship');
				hideResults(".subject-wrapper-citizenship");
			}
			
			if ($(this).hasClass('subject-wrapper-english'))
			{
				isEnglish = true;
				makeTab('english');
				hideResults(".subject-wrapper-english");
			
			}
			
			if ($(this).hasClass('subject-wrapper-geography'))
			{
				isGeography = true;
				makeTab('geography');
				hideResults(".subject-wrapper-geography");
		
			}
			
			// if any of the above exist create a tab for it
			// and one for none
			// hide all results except nonspecific initially
			if (isEnglish || isGeography || isCitizen)
			{

			    makeTab('nonespecific');
				hideAllResults();
				showResults(".subject-wrapper-none");

			}
			else
			    {
				$('#resource-bank-tabs').hide();
			    }
			
			// if clicked the relevant results should show
			$("#nonespecific-tab").click(function() {
				hideAllResults();
				showResults(".subject-wrapper-none");
				$(".tab_active").removeClass();
				$("#nonespecific-tab").parent().addClass("tab_active");
			})
			
			$("#citizenship-tab").click(function() {
				hideAllResults();
				showResults(".subject-wrapper-citizenship");
				$(".tab_active").removeClass();
				$("#citizenship-tab").parent().addClass("tab_active");

			})
			
			$("#geography-tab").click(function() {
				hideAllResults();
				showResults(".subject-wrapper-geography");
				$(".tab_active").removeClass();
				$("#geography-tab").parent().addClass("tab_active");
			})
			
			
			$("#english-tab").click(function() {
				hideAllResults();
				showResults(".subject-wrapper-english");
				$(".tab_active").removeClass();
				$("#english-tab").parent().addClass("tab_active");

			})
			
        });
	
	
		
	}

