/**
 * jQuery.induce - jQuery Plugin
 *
 * Under The MIT License
 * Copyright (c) 2011 Tetsuya MORI. (http://monry.jp/)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Version: 1.0.0
 * Revision: $Rev$
 * Date: $Date$
 */
(
  function($) {
    // {{{ 示唆テキスト処理
    $.fn.induce = function(options) {
      var defaults = {
        'color_induce': '#c0c0c0',
        'color_base': '#000000',
        'induce_text': 'Input here...'
      };

      if (typeof options == 'string') {
        options = {
          'induce_text': options
        };
      }

      var setting = $.extend(defaults, options);

      var f_focus = function(event) {
        if ($(this).val() == $(this).data('setting').induce_text) {
          $(this).val('');
        }
        $(this).css('color', $(this).data('setting').color_base);
      };
      var f_blur = function(event) {
        if (!$(this).val() || $(this).val() == $(this).data('setting').induce_text) {
          $(this).css('color', $(this).data('setting').color_induce);
          $(this).val($(this).data('setting').induce_text);
        }
      };

      this.each(
        function() {
          $(this).addClass('jquery-plugin-has-induce');
          $(this).data('setting', setting);
          $(this).focus(f_focus);
          $(this).bind('dummy-focus', f_focus);
          $(this).blur(f_blur);
          $(this).bind('dummy-blur', f_blur);
          $(this).trigger('dummy-blur');
        }
      );

      return this;
    };
    // }}}

    // {{{ 設定を変更
    $.fn.changeInduce = function(options) {
      if (typeof options == 'string') {
        options = {
          'induce_text': options
        };
      }
      $(this).trigger('dummy-focus');
      var setting = $(this).data('setting');
      setting = $.extend(setting, options);
      $(this).data('setting', setting);
      $(this).trigger('dummy-blur');
      return this;
    }
    // }}}

    // {{{ フォーム送信時に示唆テキストが残っているとマズいので、強引に消す
    $('form').live(
      'submit'
      , function(event) {
        $('input.jquery-plugin-has-induce, textarea.jquery-plugin-has-induce', $(this)).trigger('dummy-focus');
      }
    );
    // }}}
  }
)(jQuery);


