/* This code is licensed under Creative Commons Attribution 3.0    *
 * You may share and remix the script so long as you attribute the *
 * original author, Andrew January.                                *
 * http://creativecommons.org/licenses/by/3.0/                     */

$(document).ready(function() {
  // Check to see if the browser already supports placeholder text (introduced in HTML5). If it does,
  // then we don't need to do anything.
  var i = document.createElement('input');
  if ('placeholder' in i) {
    return;
  }

  var isPassword = function(input) {
    return $(input).attr('realType') == 'password';
  }

  var valueIsPlaceholder = function(input) {
    return input.value == $(input).attr('placeholder');
  }

  var showPlaceholder = function(input, loading) {
    // FF and IE save values when you refresh the page. If the user refreshes the page
    // with the placeholders showing they will be the default values and the input fields won't
    // be empty. Using loading && valueIsPlaceholder is a hack to get around this and highlight
    // the placeholders properly on refresh.
    if (input.value == '' || (loading && valueIsPlaceholder(input))) {
      if (isPassword(input)) {
        // Must use setAttribute rather than jQuery as jQuery throws an exception
        // when changing type to maintain compatability with IE.
        // We use our own "compatability" method by simply swallowing the error.
        try {
          input.setAttribute('type', 'input');
        } catch (e) {}
      }
      input.value = $(input).attr('placeholder');
      $(input).addClass('placeholder');
    }
  }

  var hidePlaceholder = function(input) {
    if (valueIsPlaceholder(input) && $(input).hasClass('placeholder')) {
      if (isPassword(input)) {
        try {
          input.setAttribute('type', 'password');
          // Opera loses focus when you change the type, so we have to refocus it.
          input.focus();
        } catch (e) {}
      }

      input.value = '';
      $(input).removeClass('placeholder');
    }
  }

});
