(function($) {
  $.textinputhover = { version: '0.1' };
  $.fn.textinputhover = function(options) {
    $(this).each(function (){
      $.extend(this, {
	defaultMessage: 'Search',
	clear: false
      }, options || {});
      if (this.clear) this.value = '';
      setClass(this);
    });
    $(this).blur(blur);
    $(this).focus(focus);
    return $(this);
  };

  function blur(e) {
    setClass(this);
  }

  function focus(e) {
    //alert(this.className);
    if ($(this).hasClass('textinputhover_select_none')) {
      $(this).removeClass('textinputhover_select_none').attr('value', '');
    }
  }

  function setClass(obj) {
    if ($.trim($(obj).attr('value')).length == 0) {
      $(obj).attr('value', obj.defaultMessage)
	.addClass('textinputhover_select_none');
      if ($(obj).hasClass('textinputhover_select')) {
	$(obj).removeClass('textinputhover_select');
      }
    } else {
      if (!$(obj).hasClass('textinputhover_select')) {
	$(obj).addClass('textinputhover_select');
      }
    }
  }
})(jQuery);
