﻿/*	Show / hide toggle function */
/*	RM 24/04/08	
				*/
(function($) {
	$.fn.toggleExpand = function(options) {
		var defaults = {
			toggleObject: jQuery("#stationFacilities table"),					// (object) The object to be shown or hidden
			toggleTrigger: jQuery("#stationFacilities h3 + table").prev("h3"),	// (object) The object that does the show / hide
			toggleElement: "table",												// (string) Short version of first option (selects specific item that triggered the event)
			toggleTriggerElement: "h3",											// (string) Short version of second option (selects specific item that triggered the event)
			toggleExpandClass: "expand",										// (string) CSS class to indicate expansion functionality
			toggleCollapseClass: "collapse",									// (string) CSS class to indicate collapse functionality
			toggleExpandTitle: "Click or select the heading and hit enter to expand this section",		// (string) Title to be added to the second option for accessibility when expand is shown
			toggleCollapseTitle: "Click or select the heading and hit enter to collapse this section",	// (string) Title to be added to the second option for accessibility when collapse is shown
			toggleSpeed: 200													// (int) Speed of show / hide
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			// Hide whatever needs hiding
			options.toggleObject.hide();
						
			// Add a link to the item that triggers the show / hide functionality
			// Don't really want to do this as a link - I'd rather bind the enter keypress event to the trigger
			// Add indicators so people know the functionality is there and how to use it
			options.toggleTrigger
				.wrapInner("<a href=''>")
				.addClass(options.toggleExpandClass)
				.attr("title", options.toggleExpandTitle);
			
			// On enter keypress or click, show or hide the content and reset the indicators
			$(options.toggleTriggerElement).toggle(function() {
				$(this).next(options.toggleElement).slideToggle(options.toggleSpeed);
				$(this)
					.removeClass(options.toggleExpandClass)
					.addClass(options.toggleCollapseClass)
					.attr("title", options.toggleCollapseTitle);
				return false;
			}, function() {
				$(this).next(options.toggleElement).slideToggle(options.toggleSpeed);
				$(this)
					.removeClass(options.toggleCollapseClass)
					.addClass(options.toggleExpandClass)
					.attr("title", options.toggleExpandTitle);
				return false;
			});
		});
	};
})(jQuery);