//
// watemark
//
(function ($) {
	//
	// plugin definition
	//
	$.fn.watermark = function (options) {
		// build main options before element iteration
		var opts = $.extend({}, $.fn.watermark.defaults, options);
		// iterate and reformat each matched element
		return this.each(function () {

			var $this = $(this), $i = $this.find('input');
			// build element specific options
			var data = $this.metadata();
			//debug(inspect(data));
			var o = data ? $.extend({}, opts, data) : opts;
			var name = (o.name) ? o.name : $this.text();
			// call our markup function
			var $wm = $("<div class='watermark'></div>").html(o.text).insertAfter($this);
			$wm.offset($i.offset());
			var hasFocus = false;
			$this.bind("keyup", function (e) {
				if (!hasFocus) toggleWatermark($wm, $i.val() == '');
			}).bind("focusout", function (e) {
				hasFocus = false;
				toggleWatermark($wm, $i.val() == '');
			}).bind("focusin", function (e) {
				hasFocus = true;
				toggleWatermark($wm, false);
			});
			$wm.click(function (e) {
				$wm.hide();
				$this.focus();
				return false;
			});
		});
	};

	function toggleWatermark($wm, show) {
		if (show)
			$wm.show();
		else
			$wm.hide();
	}
	// plugin defaults
	//
	$.fn.watermark.defaults = {
		text: 'Search'
	};

})(jQuery);	

