﻿/// <reference path="../../../vendor/jquery/jquery-1.3.2.custom.js"/>

setupWidgetExpandCollapseButtons = function(container)
{
	/// <summary>Apply expand/collapse click behaviors to any widget accordion buttons
	/// found in the supplied container. </summary>
	/// <param name=container type=object>the container to look for accordion buttons in</param>
	/// <returns type=undefined/>

	$(".widget-accordion-button", container).click(toggleWidgetVisibility);

	function toggleWidgetVisibility()
	{
		$button = $(this); // the button that was clicked
		$buttonContents = $(".widget-contents", $button.closest(".widget")); // find the contents that correspond to the button that was clicked
		$accordion = $button.parents(".widget-accordion-group"); // the parent accordion if it exists
		$visibleSiblingWidgets = $(".widget-contents:visible", $accordion); // any visible sibling contents in the accordion
		$expandedButtons = $(".widget-expanded", $accordion); //any expanded buttons (should be as many of these as there are visibleSiblingWidgets)

		// show the button contents and hide any widgets that where visible
		$buttonContents
			.add($visibleSiblingWidgets)
			.toggle('blind', { direction: "vertical" }, 250);
			
		// flip the css classes on the accordion buttons
		$button
			.add($expandedButtons)
			.toggleClass("widget-collapsed")
			.toggleClass("widget-expanded");
	}
}

setupWidgetAccordions = function(container)
{
	/// <summary>Setup all the widget accordions found in the supplied container.
	/// The setup only handles the initial visiblity of the widgets.
	/// The click behavior of the expand/collapse buttons must be appplied separately by calling setupWidgetExpandCollapseButtons</summary>
	/// <param name=container type=object>the container to look for accordions in</param>
	/// <returns type=undefined/>

	$(".widget-accordion-group", container).each(setupWidgetAccordion);

	function setupWidgetAccordion()
	{
		$accordion = $(this);

		// For the moment we support marking the selected widgets by css class
		// A common extension would be to match against the current url
		// A link to mypage.html#myAccordionAnchor would cause the widget 
		// with href #myAccordionAnchor to be open when the page loads
		$selectedWidgets = $(".accordion-selected", $accordion);
		$notSelectedWidgets = $(".widget", $accordion).not($selectedWidgets);

		// show the selected widgets and set the right class on the accordion button
		$(".widget-contents", $selectedWidgets).show();
		$(".widget-accordion-button", $selectedWidgets)
		.removeClass("widget-collapsed")
		.addClass("widget-expanded");

		// hide the not selected widgets and set the right class on the accordion button
		$(".widget-contents", $notSelectedWidgets).hide();
		$(".widget-accordion-button", $notSelectedWidgets)
		.removeClass("widget-expanded")
		.addClass("widget-collapsed");
	}
}

