﻿$(function(){
  $('.js-placeholder').each(function ( )
  {
    (function ( elm )
    {
        // placeholder時の文字色を設定
        var defaultColor    = '#5a4c3e';
        
        // title属性をplaceholder用の文字列として保持
        $.data(elm, 'placeholder-string', $(elm).attr('title'));
 
        // 元の文字色を保持 => 入力があったら戻します
        $.data(elm, 'placeholder-color', $(elm).css('color'));
    
        switch ( $(elm).val() ) {
            case '' :
                $(elm).val($.data(elm, 'placeholder-string'));
            case $.data(elm, 'placeholder-string') :
                $(elm).css('color', defaultColor);
            break;
        }
        // フォーカスされたときに、placeholderを消して色を戻す
        $(elm).focus(function ( )
        {
            if ( $(this).val() == $.data(this, 'placeholder-string') ) {
                $(this).val('');
                $(this).css('color', $.data(this, 'placeholder-color'));
            }
        });
        // フォーカスが外れたときに、valueがカラならplaceholderを代入
        $(elm).blur(function ( )
        {
            if ( $(this).val() == '' ) {
                $(this).val($.data(this, 'placeholder-string'));
                $(this).css('color', defaultColor);
            }
        });
        // フォームのsubmit時に、placeholderのままなら消してから送る
        $(elm).parents().filter('form').submit(function ( )
        {
            if ( $(elm).val() == $.data(elm, 'placeholder-string') ) {
                $(elm).val('');
            }
            return true;
        });
    })( this )
  });
});

