/**
 * Grouped Facilities JS
 * @author mjames
 * @requires jQuery 1.2.2 or YUI 2.2.1
 */

(function() {
	var NewMind = window.NewMind || {}; //namespace
	NewMind.platform = window.NewMind.platform || {};

	/**
	* grouped facilities for the JQuery library
	*/
	NewMind.platform.groupedFacilitiesJQuery = function() {
		//private
		/**
		* sets up the grouped facilities
		* @param {Object} sContainer - facilities container selector
		*/
		var prodFacilities = function(sContainer) {
			$(sContainer + " h3")
			.wrapInner('<a href="#" title="Show / Hide"></a>')
			.parent().find("ul, h4").hide().end()
			.find('a').toggle(showFacilities, hideFacilities);
		};

		/**
		* shows the facilities
		*/
		var showFacilities = function() {
			var className = $(this).parent()[0].className;
			var parent = $(this).parent().parent();
			parent.find("ul." + className + ", ul." + className + " + h4").show();
		};

		/**
		* Hides the facilities
		*/
		var hideFacilities = function() {
			var className = $(this).parent()[0].className;
			var parent = $(this).parent().parent();
			parent.find("ul." + className + ", ul." + className + " + h4").hide();
		};

		return {
			init: function() {
				prodFacilities("div.facilities");
			},

			unbind: function() {
				$("div.facilities a").unbind('click');
			},

			rebind: function() {
				$("div.facilities h3 a[href='#']").toggle(showFacilities, hideFacilities);
			}
		};
	} ();


	/** 
	* grouped facilties main method works out which library to use
	*/
	$('document').ready(function() {
		NewMind.platform.groupedFacilitiesJQuery.init();
	});

})();
