(function($) {
 
    // plugin definition
    $.fn.overlabel = function(options) {
        // build main options before element iteration
        var opts = $.extend({}, $.fn.overlabel.defaults, options);
 
        var selection = this.filter('label[for]').map(function() {
 
            var label = $(this);
            var id = label.attr('for');
            var field = document.getElementById(id);
 
            if (!field) return;
 
            // build element specific options
            var o = $.meta ? $.extend({}, opts, label.data()) : opts;
 
            label.addClass(o.labelClass);
 
            var hide_label = function() { label.css(o.hideCss) };
            var show_label = function() { this.value || label.css(o.showCss) };
 
            $(field)
                 .parent().addClass(o.wrapperClass).end()
                 .focus(hide_label).blur(show_label).each(hide_label).each(show_label);
 
            return this;
 
        });
 
        return opts.filter ? selection : selection.end();
    };
 
    // publicly accessible defaults
    $.fn.overlabel.defaults = {
        labelClass: 'overlabel-apply',
        wrapperClass: 'overlabel-wrapper',
        hideCss: { 'text-indent': '-1000px', 'display': 'none' },
        showCss: { 'text-indent': '-0px', 'cursor': 'text', 'display': 'inline' },
        filter: false
    };
 
})(jQuery);


