;(function($) {
	// Show form labels in the form field and toggle them when clicked/typed in.
	$.fn.toggleLabels = function(options) {
		var opts = $.extend({}, $.fn.toggleLabels.defaults, options);

		return this.each(function() {
			var $this = $(this);
			
			// Support for the Metadata Plugin.
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			$this.find('label').each(function() {
				// Get the label text.
				var label = $(this).html();
				
				// Hide the label tag.
				$(this).hide();
				
				// Get the form field the label belongs to.
				var field = $('#'+$(this).attr('for'));
				
				field.css('color', o.label_color);
				
				// Set the form field's value to be the label text.
				field.val(label);
				
				// On focus set the value to blank if the label is the same as the field value.
				field.focus(function() {
					if ($(this).val() === label) {
						$(this).val('');
						$(this).css('color', '');
					}
				});
				
				// On focus set the value to the label text if the form field is empty.
				field.blur(function() {
					if ($(this).val() === '') {
						$(this).val(label);
						$(this).css('color', o.label_color);
					}
				});
			});
			
			// On submit set the field value to empty if its contents is the same as the label.
			$this.submit(function() {
				$this.find('label').each(function() {
					var label = $(this).html();
					var field = $('#'+$(this).attr('for'));
					
					if (field.val() === label) {
						field.val('');
					}
					return true;
				});
			});
		});

		// private function for debugging
		function debug($obj) {
			if (window.console && window.console.log) {
				window.console.log($obj);
			}
		}
	};
	
	// default options
	$.fn.toggleLabels.defaults = {
		'label_color': '#868686'
	};

})(jQuery);
