(function($){
 $.fn.autofill = function(options) {

  var defaults = {
		password: false,
		swapclass: ''
  };
	
  var options = $.extend(defaults, options);
    
  return this.each(function() {
		obj = $(this);
		var type = obj.attr('type');
		var title = obj.attr('title');

		if(title =! '' && (type == 'text' || type == 'password')) {
			if(obj.val() == ''){
				if(type == 'password') obj = createAutofillObject(obj, 'text');
				obj.val(obj.attr('title'));
				handleInputFocus(obj, type);
			}
		}
		
  });
 };
})(jQuery);

function handleInputFocus (obj, type) {
	obj.focus(function(){
		if(obj.val() == '' || obj.val() == obj.attr('title')) obj.val('');
		if(type == 'password'){
			obj = createAutofillObject(obj, 'password');
			obj.focus();
		}
		handleInputBlur(obj, type);
	});
}

function handleInputBlur (obj, type) {
	obj.blur(function(){
		if(type != 'password') if(obj.val() == '' || obj.val() == obj.attr('title')) obj.val(obj.attr('title'));	
	});
}

function createAutofillObject (obj, type) {
	var rep = $("<input type='"+type+"' />").attr("style", obj.attr("style") != null ? obj.attr("style") : '').attr("id", obj.attr("id")).attr("name", obj.attr("name")).attr("class", obj.attr("class")).attr("title", obj.attr("title")).val(obj.val()).insertBefore(obj);
	obj.remove();
	return rep;
}
