/* ------------------------------------------------------------
 * PROJECT        : 
 * FILENAME       : jq.toggleModule.js
 * ------------------------------------------------------------
 * LAST UPDATED   : 30 Nov 2009
 * ------------------------------------------------------------
 * AUTHOR(S)      : Kevin Scholl (kevin@ksscholl.com)
 * ------------------------------------------------------------
 * NOTE(S)        : 
 * ------------------------------------------------------------ */

/*

SUMMARY

This plugin provides a visibility toggle to the specified block element, and adds a control to its previous sibling element. The user can then show and hide only those modules they need to see at a given time.

OPTIONAL SETTINGS

initialState
A string specifying whether the associated module should be displayed or hidden on initial page load. Available options include "visible" (default) and "hidden".

moreText
A string used to provide additional detail to the control label. Default values are "Show" and "Hide" depending on the module's visibility.

USAGE

$("el").toggleModule();
$("el").toggleModule({ options });

where el is the block to be displyed/hidden.

EXAMPLE

$(document).ready(function() {
	$("#el").toggleModule({
		initialState : "hidden",
		moreText     : "Example 3"
		});
	});

MINIMUM BROWSER SUPPORT

Apple Safari 3, Google Chrome 1, Microsoft Internet Explorer 6, Mozilla Firefox 3, Opera 9

*/

jQuery.fn.toggleModule = function(settings) {
	settings = jQuery.extend({
		initialState : "visible", // visible | hidden
		moreText     : ""
		}, settings);
	this.each(function() {
		var thisElemID = $(this).attr("id");
		if (settings.initialState == "visible") 
		  var panelAction = "Hide";
		else {
			$(this).hide();
			var panelAction = "Show";
			}
		$(this)
		  //.siblings("p.moduleTitle")
		  .prev()
			.prepend("<span class=\"toggleDisplay\"><a "
			  + "class=\"panel" + panelAction + "\" href=\"#\" "
			  + "onclick=\"toggleDisplayWithText(this,\'" + $(this).attr("id") + "\',\'" + settings.moreText + "\'); "
			  + "return false;\">" + panelAction + " " + settings.moreText + "</a></span>")
			.dblclick(function () {
			  toggleDisplayWithText($(this).find("span a"),thisElemID,settings.moreText);
			  });
		});
	};

/* ------------------------------------------------------------
 * FUNCTIONS
 * ------------------------------------------------------------ */

function toggleDisplayWithText(lnk, el, txt) {
	// $('#' + el).slideToggle();
	$('#' + el)
	  // .css("display") == "none" ? $('#' + el).fadeIn(1500) : $('#' + el).hide();
	  .css("display") == "none" ? $('#' + el).slideDown(1000) : $('#' + el).slideUp(500);
	$(lnk).text($(lnk).text() == "Show " + txt ? "Hide " + txt : "Show " + txt);
	$(lnk)
	  .toggleClass("panelShow")
		.toggleClass("panelHide");
	}
