(function($) {
	$.fn._type = function(type) {
		return this.each( function() {
			var $this = $(this);
			if ( !$this.get(0).clone )
				return;
			var $clone = $this.get(0).clone;
			if ( !type )
			{
				$this.hide();
				$clone.show();
			}
			else
			{
				$this.show();
				$clone.hide();
			}
		});
	};
	$.fn.autoclean = function()
	{
		return this.each( function() {
			var $this = $(this);
			var $val = $this.attr('default');
			var $type = $this.attr('type');
			if ( $type != 'text' )
			{
				$this.get(0).clone = $('<input type="text">').attr({
					'class': $this.attr('class'),
					'style': $this.attr('style'),
					'value': $this.attr('default')
				}).insertBefore($this).bind('focus', function() {$this.focus();});
			}

			if ( $val )
			{
				if ($type != 'text')
					$this._type( $this.val() == '' ? null : 'text');
				else if ( $this.val() == '' )
					$this.val($val); 

				if ( $type == 'text')
				{
					$this.bind('focus', function() { if ( $this.val() == $val ) $this.val(''); });
					$this.bind('blur',  function() { if ( $this.val() == '' ) $this.val($val); });
				}
				else
				{
					$this.bind('focus', function() { if ( $this.val() == '' ) $this._type('text'); });
					$this.bind('blur',  function() { if ( $this.val() == '' ) $this._type(); });
				}
			}
		});
	};	
	$(document).ready(function() { $('.autoclean').autoclean().removeClass('autoclean'); });
})(jQuery);