(function( $ ){

	var innerLabel = {

		init : function( options ) {

			// Default options
			var defaults = {
				labelClass: 'innerlabel-processed',
				fieldClass: 'innerlabel-default',
				once: false
			};

			options = $.extend({}, defaults, options);

			return this.each(function(){
				$this = $(this);
				
				var txt;
				var label = $this.prev('label');

				// Put label text in field (if it's empty)
				txt = label.text();
				label.addClass(options.labelClass);
				
				if ( $this.val() == '' ) {
					$this.val(txt).addClass(options.fieldClass);
				}
				// Add title attr for tooltip when field is filled in
				// to remind user which field it is
				$this.attr('title', txt);

				// Blank field on focus if unmodified
				$this.focus( function() {if ( $(this).val() == txt ) $(this).val('').removeClass(options.fieldClass);} );
				// Restore help text on blur if empty
				if ( !options.once )
					$this.blur( function() {if ( $(this).val() == '' ) $(this).val(txt).addClass(options.fieldClass);} );
			});
		}
	};

	$.fn.innerLabel = function( method ) {

		// Method calling logic
		if ( innerLabel[method] ) {
			return innerLabel[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return innerLabel.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.innerLabel' );
		}    

	};

})( jQuery );
