(function ($) {

    $.placeholder = function () {

        // monkey-patch the val() method so that it ignores placeholder text
        // but don't use it as a getter in this plugin
        var oldmethod = $.fn.val;

        $.fn.val = function () {

            if (arguments.length === 0 && oldmethod.call(this) === this.data('placeholder_text')) {
                return '';
            } else {
                return oldmethod.apply(this, arguments);
            }

        };

        // the actual plugin
        var inputs = $('input[type=text][title], textarea[title]'),
            ghostClass = 'd3_clr_note d3_fine';

        return inputs.each(function () {

            var el = this,
                $el = $(this),
                placeholder_text = el.getAttribute('title');

            $.data(el, 'placeholder_text', placeholder_text);
            el.removeAttribute('title');

            if (el.value === '' || el.value === placeholder_text) {
                $el.addClass(ghostClass).val(placeholder_text);
            } else {
                $el.removeClass(ghostClass);
            }

            $el.bind('focusin', function () {
                if (el.value === placeholder_text) {
                    $el.removeClass(ghostClass).val('');
                }
            }).bind('focusout', function () {
                if (el.value === '') {
                    $el.addClass(ghostClass).val(placeholder_text);
                }
            });

        });

    };

}(jQuery));
