﻿; (function($) {
    $.fn.titlelabel = function(options) {

        var defaults = {
            label: "title",
            nullClass: "empty"
        };
        var options = $.extend(defaults, options);

        return this.each(function() {

            var $this = $(this);

            if (this.tagName == 'SELECT') {
                $this.change(function() {
                    var selected = $this.find('option:selected');
                    if (selected.val() == '')
                        $this.addClass(options.nullClass);
                    else
                        $this.removeClass(options.nullClass);
                });

                $this.change();
            } else {
                $this.focus(function() {
                if ($this.val() == $this.attr(options.label)) {
                    $this.val("");
                    $this.removeClass(options.nullClass);
                    }
                });

                $this.blur(function() {
                if ($this.val() == "" || $this.val() == $this.attr(options.label)) {
                    $this.val($this.attr(options.label));
                    $this.addClass(options.nullClass);
                    }
                });

                $this.blur();
            }
        });
    };

})(jQuery);
