﻿// JScript File
function accordion(el, state){

    // Set state to 2 Show All
    state = 2;

	//variables
	$toHide = jQuery(el); //elements you want to hide
	$expandAllHtml = jQuery('<div id="supportingLinks"><p class="resultsEx"><a href="javascript:void(0);">' + promptexpandall + '</a></p></div>');
	$headings = $toHide.prev();
	
	$toHide.css('display','block');
	
	$toHide.hide();
	$expandAllHtml.insertBefore(jQuery('div#hideResults')); //add expand all control
	
	jQuery('.resultsEx').click(
		function(){
			if ($toHide.prev().hasClass('ExDown')) { // Show.
				$toHide.hide();
				$toHide.prev().removeClass('ExDown');
				jQuery(this).removeClass('resultsCl').children().text(promptexpandall);
				return false;
			} else { // Hide.
				$toHide.show();
				$toHide.prev().addClass('ExDown');
				jQuery(this).addClass('resultsCl').children().text(promptcontractall);
				return false;				
			}
			
		}
	);

	jQuery('.ExUp').live('click', function(e){ //heading links

		var $clickedHeading = jQuery(e.target);
		
		if ($clickedHeading.attr("href") != undefined) {
			$currentHeading = $clickedHeading.parent();
			$currentContent = $clickedHeading.parent().next();
		} else {
			$currentHeading = $clickedHeading;
			$currentContent = $clickedHeading.next();
		}
		
		if ($currentContent.is(':visible')){
			$currentHeading.removeClass('ExDown');
			$currentContent.slideUp('slow');
			
			if ($toHide.parent().find('.ExDown').length <= 1) {
				$toHide.prev().removeClass('ExDown');
				jQuery('.resultsEx').addClass('resultsCl').children().text(promptexpandall);
			}
			
			return false;
		}
		
		$toHide.not(':hidden').slideUp('slow');
		$headings.removeClass('ExDown');
		$currentHeading.addClass('ExDown');
		$currentContent.slideDown('slow');
		
		$toHide.prev().addClass('ExDown');
		jQuery('.resultsEx').addClass('resultsCl').children().text(promptcontractall);
		
		return false;
	});
	
	// Configure state
	switch (state) {
		case 1:
			$(el).hide(); // Hide all.
			break;
		case 2:
			$(el).show(); // Show all.
			$toHide.show();
			$toHide.prev().addClass('ExDown');
			jQuery('.resultsEx').addClass('resultsCl').children().text(promptcontractall);
			break;
		case 3:
			$(el).hide(); // Hide all.
			$(el+":first").show(); // Show first.
			break;
			
		default:
			$(el).hide();
	}
}


