﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// JScript File encapsulates the functionality for Expand All | Collapse All feature
// It uses the functionality in jQuery library to expand/collapse a list of items that are in a set of div elements
// 
// NOTE: This script should be used in conjunction with CollapsibleList.js and/or ToggleDetails.js, as it can handle both
//		 scenarios
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(document).ready(function()
{	
	// Event handler for "Expand All" span element
	$("div span.toggle-list:first-child").click(function()
	{		
		var children = null;
		
		// If we find the custom attribute "singleexpand"
		if ($(this).parents("table").attr("singleexpand") == "true")
		{			
			// We apply script on table
			$(this).parents("table").find("div.header:has(div.content)").css("background-image", "url(/_layouts/images/minus.gif)").children().show("slow");			
			
			children = $(this).parents("table").find("div.answer").children();
		}
		else
		{
			// We apply script on all page
			$("div.header:has(div.content)").css("background-image", "url(/_layouts/images/minus.gif)").children().show("slow");			
			
			children = $("div.answer").children();
		}	
		
		ToggleChildren("show", children);
	});   
  
	// Event handler for "Collapse All" span element
	$("div span.toggle-list:last-child").click(function()
	{
		var children = null;
		
		// If we find the custom attribute "singleexpand"
		if ($(this).parents("table").attr("singleexpand") == "true")
		{			
			// We apply script on table			
			$(this).parents("table").find("div.header:has(div.content)").css("background-image", "url(/_layouts/images/plus.gif)").children().hide("slow");
			
			children = $(this).parents("table").find("div.answer").children();			
		}
		else
		{
			// We apply script on all page
			$("div.header:has(div.content)").css("background-image", "url(/_layouts/images/plus.gif)").children().hide("slow");			
			
			children = $("div.answer").children();
		}
		
		ToggleChildren("hide", children);
	});   
   
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Name        : ToggleChildren
	// Description : Sets the text of the div elements with class "read-more" and hides/shows the elements with class "details"
	// Parameters  : 
	//				children = the children elements that belong to the current div element having class "answer"
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function ToggleChildren(action, children)
	{
		var attribute = "";		
		
		if (action == "" || children == null) { return false; }
		
		attribute = (action == "show") ? "textHide" : "textShow";
		
		for (var i=0; i<children.length; i++)
		{
			if ($(children[i]).is(".read-more"))
			{				
				$(children[i]).text($(children[i]).attr(attribute));
			}			
			else if ($(children[i]).is(".details"))
			{
				if (action == "show")
				{
					$(children[i]).show("slow");
				}
				else
				{
					$(children[i]).hide("slow");
				}
			}
		}
	}   
});


