/**
 * This jQuery plug-in will store the values the elements has on call time,
 * and remove these on focus. It will restore the original
 * values on blur if the input fields are empty.
 *
 * It supports password fields too, and the way it handles these are that it
 * makes dummy text fields and inserts them as placeholders for the password
 * field which are hidden until dummy gets focus. It's done this way cause 
 * Firefox's login helper will fill in password in the clear text field 
 * before we get a changes of making it into a password field again.
 * 
 * Example of usage
 * ================
 * $("input[value!='']").removeDefaultValuesOnFocus();
 *
 * @author Thorbjørn Hermansen    <thhermansen at gmail dot com>
 */
(function($) {
  $.fn.removeDefaultValuesOnFocus = function(options) {
    var defaults = {};
    options = $.extend(defaults, options);
    

    /**
     * We'll be storing input elements original values, which
     * these two functions will be helping us with
     */
    var storeOriginalValue = function(element) { $(element).data("originalValue", getValueFromElement(element)); };
    var getOriginalValue = function(element) { return $(element).data("originalValue"); };

    /**
     * Methods for extracting and setting value in elements
     */
    var getValueFromElement = function(element) { return $(element).val(); };
    var setValueOnElement = function(element, value) { $(element).val(value); };


    /**
     * Password fields must be taken care of in a different way than
     * normal text fields as they do hide the text. We can't simply change
     * the type to text and change it back to password when it gets focus as
     * Firefox's login helper will fill in password in the clear text field 
     * before we get a changes of making it into a password field again.
     */
    var takeCareOfPasswordField = function(passwordField) {
      // Create the dummy text element and insert it to DOM
      var textField = $('<input type="text" value="'+getValueFromElement(passwordField)+'"/>').insertAfter(passwordField)
      
      
      // Fix some CSS, which could have been fixed by clone the element, but IE doesn't like assigning
      // new type attribute on input elements.
      .width($(passwordField).width())
      .attr("className", passwordField.className)

      // On focus, remove self, show and give focus to original password element
      .focus(function() {
        $(this).hide();
        $(passwordField).show().focus();
      });
      
      // Remove value from password field and display the dummy field again if password is blank on blur
      $(passwordField).hide().attr("value", "").blur(function() {
        if (this.value == "") {
          $(this).hide();
          textField.show();
        }
      });
    };



    /**
     * jQuery iterator returned so it makes this plug-in chaninable as all other
     * jQuery functions.
     */
    return this.each(function() {
      if (this.type == "password") {
        takeCareOfPasswordField(this);
      } else {
        storeOriginalValue(this);
        
        $(this).focus(function() {
          if (getOriginalValue(this) == getValueFromElement(this)) setValueOnElement(this, "");
        })

        .blur(function() {
          if (!getValueFromElement(this)) setValueOnElement(this, getOriginalValue(this));
        });
      }
    });
  };
})(jQuery);

