jQuery(function($){

$.fn.watermark = function(options){
	var defaults = {
		cssClass: null,
		onBlur: null,
		onFocus: null
	}
	var options = $.extend(defaults,options);
	var onBlur = function(){
					var $this = $(this);
					var $ref = $this;
					var isEmpty = ($this.val()=='');
					if($this.data('watermark.clone'))
						$ref = $this.data('watermark.clone');
					$this.data('watermark.value',$this.val());
					$ref.data('watermark.value',$this.val());
					if(isEmpty){
						if($this.data('watermark.clone')){
							$this.hide()
								.data('watermark.clone')
									.show();
						}
						$this.data('watermark.isEmpty',true);
						$this.val($this.data('watermark.default'))
						$ref.one('focus',function(){
											if($this.data('watermark.clone')){
												$this.show().focus();
												$ref.hide();
											}
											$this.val('')
												.removeClass(options.cssClass);;
											if(options.onFocus)
												options.onFocus.apply($this);
										}
							).addClass(options.cssClass);
					}else{
						$this.data('watermark.isEmpty',false);
						if(options.onFocus)
							$this.one('focus',options.onFocus);
						$ref.removeClass(options.cssClass);
					}
					if(options.onBlur)
						options.onBlur.apply($this,[isEmpty]);
				};
	this.each(
			function(){
				var $this = $(this);
				var $ref = $this;
				if($this.attr('type')=='password'){
					$ref = $('<input/>');
					$ref[0].className = $this.className;
					$ref.val($this.val());
					$ref.data('watermark.origin',$this)
						.one('focus',function(){
										$(this).hide()
												.data('watermark.origin')
													.show()
													.focus()
													.val('');
									}
						)
						.insertAfter($this);
					$this.data('watermark.clone',$ref)
						.hide();
				}else
					$this.one('focus',function(){ $(this).val('') });
				$this.blur(onBlur);
				$this.data('watermark.isEmpty',true)
					.data('watermark.default',$this.val());
			}
		)
		return this;
}

});