// JavaScript Document

$(document).ready(function() {
	var closeOthers = true;


	// check which sections are open
	function checkOpen() {
		// how many sections are open
		var openCount = $('#container .readMore:visible').length;
		// how many sections are there
		var totalCount = $('#container .readMore').length;
		// set closeOthers var based on if there are 1 or 0 sections open
		if (openCount < 2) closeOthers = true;
		// change the text in the expand link based on if
		// there are more or less than half of the sections open
		if (openCount > totalCount / 2) {
			//$('#container .expand').addClass("btnSwitch2");
			$('#container .expand').html("Collapse All");
			$('.expand').addClass("btnSwitch2");
		}
		else {
			//$('#container .expand').removeClass("btnSwitch2");
			$('#container .expand').html("Expand All");
			$('.expand').removeClass("btnSwitch2");
		}
	}
	// hide all sections
	$('#container .readMore').hide();
	// show the first section
	//$('#container .readMore:first').show();


	// actions taken upon clicking any headline
	$('#container .reviewBtn').click(function() {
		//sets default state of button
		// set checkSection to the section next to the headline clicked
		// zzzzzzzzzzzz  i modified to display "previous" instead of next so the control is below the content zzzzzzzzzzzzzzzzzz
		var checkSection = $(this).prev();
		// if the section is open, close it, and call checkOpen
		if (checkSection.is(':visible')) {
			checkSection.slideUp('slow', checkOpen);
			//this creates toggle effect when each button pressed individually
			$(this).toggleClass("btnSwitch");
			return false;
		}
		// if the section is closed and closeOthers
		// is true, close all other open sections
		else {
			if (closeOthers) {
				$('#container .readMore:visible').slideUp('slow');
				//this removes the toggle and class when another button is pressed
				$('.reviewBtn').removeClass("btnSwitch");
			}
			//this ensures pressed button maintains active class
			$(this).addClass("btnSwitch");
			// open the section and call checkOpen
			checkSection.slideDown('slow', checkOpen);

			return false;
		}
	});


	// actions taken upon clicking the expand link
	/* this didn't work
	$('#container .expand').click( function() {
	if ($('#container .expand').hasClass() == "") {
	*/
	// actions taken upon clicking the expand link
	$('#container .expand').click(function() {
		// if the expand link's text is 'expand all', set closeOthers
		// to false, open all sections and call checkOpen

		if ($('#container .expand').html() == "Expand All") {
			closeOthers = false;
			$('.expand').addClass("btnSwitch2");
			$('#container .readMore').slideDown('slow', checkOpen);
			//ensures btnSwitch state is added to all open sections - all sections when this button is used
			$('.reviewBtn').addClass("btnSwitch");
		}

		// if the expand link's text is 'expand all', set closeOthers
		// to true, hide all sections, and call checkOpen
		else {
			closeOthers = true;
			//keep this one - it works
			//removes the btnSwitch class if "close all" is clicked
			$('.reviewBtn').removeClass("btnSwitch");
			// the 1 prevents nasty flickering in some browsers
			$('#container .readMore').hide(1, checkOpen);
			//this creates a smoother transition but doesn't work right in ie - $('#container .readMore').hide('slow', checkOpen);
			return false;
		}
	});

	$(".form-slide").click(function() {
		$(this).parent().next("div").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});

	$(".writeResponseBtn").click(function() {
		$(this).parent().next("div").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});

	$(".schoolTitle").click(function() {
		$(this).parent().next("div").slideToggle("slow");
		//$(this).toggleClass("active");
		$(this).toggleClass("schoolTitleIE6Active");
		//$(".showHideSelector").toggleClass("active"); 
		$(this).next("a.showHideSelector").toggleClass("showHideSelectorActiveIE6");
		return false;
	});

	$(".showHideSelector").click(function() {
		$(this).parent().next("div").slideToggle("slow");
		//$(this).toggleClass("active");
		//$(".schoolTitle").toggleClass("active"); 
		//$(this).prev(".schoolTitle").toggleClass("active");
		//$(this).toggleClass("schoolTitleIE6Active"); 
		//$(this).next(".showHideSelector").toggleClass("showHideSelectorActiveIE6");
		$(this).toggleClass("showHideSelectorActiveIE6");
		$(this).prev("a.schoolTitle").toggleClass("schoolTitleIE6Active");
		return false;
	});
	/*
	$(".ecomContentSub .showHideSelector").click(function(){
	$(this).next("div").slideToggle("slow");
	$(this).toggleClass("active"); return false;
	});
	*/
	//ecomSectionalWrapper


	/*
	$(".ecomTitleBarSub").click(function(){
	$(this).next(".ecomSectionalWrapper").slideToggle("slow"); //you can use "next" to select only the next div or leave that off to select ALL divs
	$(this).toggleClass("active"); return false;
	});
	*/

/*
	$(".ecomTitleBarSub").click(function() {
		$(this).parent().next("div").slideToggle("slow");
		$(this).toggleClass("ecomTitleBarSubActive");//.toggleClass("active");
		$(this).next(".showHideSelectorSub").toggleClass("showHideSelectorSubActive");//.toggleClass("active");
		return false;
	});
	*/
	$(".ecomTitleBarSub").click(function() {
		$(this).parent().next("div").slideToggle("slow");
		$(this).toggleClass("ecomTitleBarSubActive");//.toggleClass("active");
		$(this).next(".showHideSelectorSub").toggleClass("showHideSelectorSubActive");//.toggleClass("active");
		//$(this).parent().siblings("a.showHideSelectorSub2").trigger("click");
		return false;
	});
/*
	$(".showHideSelectorSub").click(function() {
		$(this).parent().next("div").slideToggle("slow");
		$(this).toggleClass("active");
		$(this).prev(".ecomTitleBarSub").toggleClass("active");
		return false;
	});
	*/
	$(".showHideSelectorSub").click(function() {
		$(this).prev(".ecomTitleBarSub").trigger("click");//.toggleClass("active");
		return false;
	});


	$(".showHideSelectorSub2").click(function() {
		$(this).next("div").slideToggle("slow");
		$(this).toggleClass("showHideSelectorSub2active"); return false;
	});

	/*this causes the click to trigger the showhide*/
	$(".btnH5").click(function() {
		$(this).parent().siblings("a.showHideSelectorSub2").trigger("click"); //note: since the title is wrapped in a span on the Ektron site i had to add .next before siblings
		return false;
	});

	//btnH5v2 - this is used for any h5 not wrapped in a span
	$(".btnH5v2").click(function() {
		$(this).parent().siblings("a.showHideSelectorSub2").trigger("click"); //note: since the title is wrapped in a span on the Ektron site i had to add .next before siblings
		return false;
	});

	/*for specific open/close*/
	$(".cancel").click(function() {
		$(this).next("div").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});

	$(".upload").click(function() {
		$(this).next("div").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});

	checkOpen();
});