/*
	jQuery toggleForm v1.0
	
	Wouter Beeftink
	wouter@footsteps.nl
*/
(function($) {
	$.fn.toggleForm = function(selector, options) {
		
		// Options
		var options = $.extend({
			action : 'toggle',
			event : 'click',
			startHidden : true
		}, options);
		
		// Global
		var node = $(selector);
		var elements = node.children('[nodeType=1]');
		
		// Show or hide the target node
		options.startHidden ? node.hide() : node.show();
		
		// Disable form controls if target is hidden
		toggleControls();
		
		// Bind event and action
		return this.bind(options.event, function() {
			switch(options.action) {
				case 'show' :
					node.show();
					break;
				case 'hide' :
					node.hide();
					break;
				default :
					node.toggle();
			};
			toggleControls();
		});
		
		function toggleControls() {
			if(node.is(':visible')) {
				elements.each(function() {
					if($(this).data('required') == true)
						$(this).attr('_required', '');
				});
			} else {
				elements.each(function() {
					var $this = $(this);
					if($this.attr('_required') == '') {
						$this
							.removeAttr('_required')
							.data('required', true);
					};
				});				
			};	
		};

	};
})(jQuery);