(function($){
  $.fn.placeholder = function(){
    function valueIsPlaceholder(input){
      return ($(input).val() == $(input).attr('placeholder'));
    }
    return this.each(function(){
      $(this).each(function(){
        if($(this).attr('type') == 'password'){
          var new_field = $("<input type='text'>");
          new_field.attr('value',$(this).attr('placeholder'));
          $(this).parent().append(new_field);
          new_field.hide();
          
          function showPasswordPlaceHolder(input){
            if( $(input).val() == '' | valueIsPlaceholder(input)) {
              $(input).hide();
              $("input[type=text]",$(input).parent()).css('color','#cccccc');
              $("input[type=text]",$(input).parent()).show();
            };
          };
          
          new_field.focus(function(){
            $(this).hide();
            $('input[type=password]',$(this).parent()).css('color','#000000');
            $('input[type=password]',$(this).parent()).show().focus();
          });
          
          $(this).blur(function(){
            showPasswordPlaceHolder(this);
          });
          
          showPasswordPlaceHolder(this);
        } else {
          //値をプレースホルダテキストと置き換える
          //オプションのreloadパラメータはFFとIEが
          //フィールドの値をキャッシュする問題を解決する
          function showPlaceholder(input,reload) {
            if( $(input).val() == '' || (reload && valueIsPlaceholder(input)) || reload) {
              $(input).css('color','#cccccc');
              $(input).val($(input).attr('placeholder'));
            };
          };
          $(this).focus(function(){
            $(this).css('color','#000000');
            if($(this).val() == $(this).attr('placeholder')){
              $(this).val('');
            };
          });
          
          $(this).blur(function(){
            showPlaceholder($(this),false);
          });
          
          showPlaceholder($(this),true);
        };
      });
      
      //フォームがデフォルト値で送信されるのを阻止する
      $(this).submit(function(){
        $(this).find(':input').each(function(){
          if($(this).val() == $(this).attr('placeholder')) {
            $(this).val('');
          };
        });
      });
    });
  };
})(jQuery);

