(function($){
	var ClearDefaultText = function(ele, opts) {
		this.ele = $(ele)
		.focus($.proxy(this._onFocus, this))
		.blur($.proxy(this._onBlur, this));
		
		this.type = this.ele.attr("type").toLowerCase();
		
		var obj = this.ele.ParseClass(true);
		if (obj.defaultValue) {
			this.ele.attr("defaultValue", obj.defaultValue);
		}
		
		if (this.type == "password") {
			this.clone = $('<input class="text" style="width: ' + this.ele.css("width") + ';" value="' + this.ele.attr("defaultValue") + '" />')
			.focus($.proxy(this._onFocus, this));
			this.ele.after(this.clone);
			this.ele.hide();
			setTimeout($.proxy(function () {
				this.clone.attr("tabIndex", this.ele.attr("tabIndex"));
			}, this), 10);
		}
	};
	$.extend(ClearDefaultText.prototype, {
		_onFocus: function(e) {
			if (this.ele.val() == this.ele.attr("defaultValue")) {
				this.ele.val("");
			}
			if (this.type == "password" && !this.ele.is(":visible")) {
				this.ele.show();
				this.clone.hide();
				this.ele.focus();
			}
		},
		_onBlur: function(e) {
			var ele = $(e.currentTarget);
			if (ele.val() == "") {
				ele.val(ele.attr("defaultValue"));
				if (this.type == "password") {
					this.ele.hide();
					this.clone.show();
				}
			}
		}
	});
	
	$.fn.extend({
		ClearDefaultText: function(opts) {
			var $this = this.not(function(i) {
				return $.data(this, "ClearDefaultTextObj");
			}).each(function() {
				$.data(this, "ClearDefaultTextObj", new ClearDefaultText(this, opts));
			});
			return this;
		}
	});
	$.ClearDefaultText = {
		init: function(sel) {
			$(sel || "input.clearDefaultText").ClearDefaultText();
		}
	};
	$(function() {
		$.ClearDefaultText.init();
	});
})(jQuery);
