(function($) {

	var config;
	
	var methods = {
	
		init: function( settings ) {
	
			config = {
				focusColor: 'black',
				blurColor: 'lightgray',
				modify: false,
				ajax: false,
				fill: null
			};
			
			status = 0; // new
			
			if ( settings ) $.extend( config, settings );
	
			$(this).each( function() {
				$(this)
					.unbind( 'submit', methods.submitHandler )
					.submit( methods.submitHandler );
					
				if ( config['fill'] )
				{

					$(this).children('[title]').each(function(index) {

						if ( config['fill'][index] === '' )
						{
							$(this)
								.data( 'isEmpty', true )
								.val( $(this).attr( 'title' ) )
								.css( 'color', config['blurColor'] )
								.unbind('focus', methods.focusHandler)
								.focus( methods.focusHandler )
								.unbind('blur', methods.blurHandler)
								.blur( methods.blurHandler );
						}
						else
						{
							$(this)
								.data( 'isEmpty', false )
								.val( config['fill'][index] )
								.css( 'color', config['focusColor'] )
								.unbind('focus', methods.focusHandler)
								.focus( methods.focusHandler )
								.unbind('blur', methods.blurHandler)
								.blur( methods.blurHandler );
						}
						
					});
				}
				else
				{
					$(this).children('[title]').each( function() {
						$(this)
							.data( 'isEmpty', true )
							.val( $(this).attr( 'title' ) )
							.css( 'color', config['blurColor'] )
							.unbind('focus', methods.focusHandler)
							.focus( methods.focusHandler )
							.unbind('blur', methods.blurHandler)
							.blur( methods.blurHandler );
					});
				}
			});
	
			return $(this);
		
		},
		
		reset: function() {
			$(this).each( function() {
				$(this).children('[title]').each( function() {
					$(this)
						.data( 'isEmpty', true )
						.val( $(this).attr( 'title' ) )
						.css( 'color', config['blurColor'] );
				});
			});
			return $(this);
		},
		
		submitHandler: function(eventObject)
		{
			$(eventObject.currentTarget).children('[title]').each(function()
			{
				if ( $(this).data('isEmpty') )
					$(this).val('');
			});
			return !config['ajax'];
		},
		
		focusHandler: function(eventObject)
		{
			var trg = eventObject.currentTarget;
			if ( $(trg).data( 'isEmpty' ) )
				$(trg)
					.data( 'isEmpty', false )
					.val('')
					.css( 'color', config['focusColor'] );
		},
		
		blurHandler: function(eventObject) {
			var trg = eventObject.currentTarget;
			if ( !$(trg).data( 'isEmpty' ) && $(trg).val() === '' )
				$(trg)
					.data( 'isEmpty', true )
					.val( $(trg).attr( 'title' ) )
					.css( 'color', config['blurColor'] );
		}

	}

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

