/**
 * watermark plugin 1.0
 *
 * Watermarks input fields using custom attributes, or text parameter
 * sample usage: $('input').watermark('text here'); || $('input').watermark() w/ <input watermark="text here"/>
 *
 * Copyright (c)2008 Troy Kruthoff (http://blit.com/)
 * MIT license (blit.com/licenses/mit.html)
 */

;(function($) {

$.fn.watermark = function(options) {
  if (options && options.substring) {options = {text:options}}
  else if (!options) {options = {}};
  if (!options.color) {options.color = '#bbb';}
  this.filter('input').map(function(){
    var $this = jQuery(this);
    var text = $this.attr('watermark') || options.text;
    if (!text) {return(null)};
    $this.data('watermark',{text:text,color:options.color,orig_color:$this.css('color')});
    set_watermark(this);
    return(this);
  }).focus(function(){remove_watermark(this)}).blur(function(){set_watermark(this)});
  return(this);

  /* helper functions */
  function set_watermark(input) {
    var $this = jQuery(input);
    var watermark = $this.data('watermark');
    var currval = String($this.val());
    if (currval.replace(' ','')==''||currval==watermark.text) {
      $this.css('color',watermark.color);
      $this.val(watermark.text);
    }
  }
  function remove_watermark(input) {
    var $this = jQuery(input);
    var watermark = $this.data('watermark');
    if ($this.val()==watermark.text) {$this.val('')};
    $this.css('color',watermark.orig_color);
  }
}
})(jQuery);
