/**
 * Default text is a plugin for showing a string
 * of text as a "title" in an input field. When the
 * user focuses on the field, if it is in its default
 * state, the field is wiped. When they unfocus (or blur)
 * from the field, the default string is replaced if they
 * did not enter any content.
 *
 * @author     Benjamin Borowski <ben.borowski@typeoneerror.com>
 * @copyright  Copyright (c) Typeoneerror Studios http://typeoneerror.com
 * @version    $Id: jquery.defaultText.js 325 2010-03-31 15:33:04Z ben $
 *
 * @uses jquery.min.js
 */
(function()
{
    $.fn.defaultText = function(action, options)
    {
        if (!action) action = "initialize";

        if (typeof(action) == "object")
        {
            options = action;
            action = "initialize";
        }

        var options = $.extend({}, $.fn.defaultText.defaults, options);

        return $(this).each(function()
        {
            var self = $(this);

            self.data("defaultAttr", options.attr);
            self.data("self", self);

            switch (action)
            {
                case "initialize":

                    if (options.defaultValue != undefined)
                    {
                        self.attr(options.attr, options.defaultValue);
                        self.addClass(options.activeClass);
                    }

                    self.focus(function(event)
                    {
                        if (self.isDefaultText())
                        {
                            self.removeClass(options.activeClass);
                            self.val("");
                        }
                    });

                    self.blur(function(event)
                    {
                        if (self.val() == "")
                        {
                            self.addClass(options.activeClass);
                            self.val(self.attr(options.attr));
                        }
                    });

                    self.blur();

                    if (self.isDefaultText())
                    {
                        self.addClass(options.activeClass);
                    }

                    break;

                case "reset":
                    self.val(self.attr(options.attr));
                    self.addClass(options.activeClass);
                    break;
            }
        });
    };

    $.fn.defaultText.defaults = {
        activeClass  : "defaultTextActive",
        attr         : "title",
        defaultValue : null
    }

    $.fn.isDefaultText = function()
    {
        var attr = ($(this).data("defaultAttr") == undefined) ? $.fn.defaultText.defaults.attr : $(this).data("defaultAttr");
        return $(this).val() == $(this).attr(attr);
    }

})(jQuery);
