﻿jQuery.fn.labelOver = function (overClass) {
    return this.each(function () {

        // Find the first label (may be this item or one of its descendants)
        var label = null;
        if ($(this).is('label')) {
            label = $(this);
        }
        else {
            label = $(this).find('label').first();
        }

        if (label) {
            // Find the associated input for the label
            var inputId = label.attr('for');
            if (inputId) {
                var input = $(this).closest('form').find('#' + inputId);
                if (input) {

                    // Move the label to be the sibling just prior to the input
                    input.before(label);

                    label.hide = function () {
                        label.css({ textIndent: -10000 })
                    }

                    label.show = function () {
                        if (input.val() == '') label.css({ textIndent: 0 })
                    }

                    // handlers
                    input.focus(label.hide);
                    input.blur(label.show);
                    label.addClass(overClass).click(function () { input.focus() });

                    if (input.val() != '') label.hide();
                }
            }
        }

    })
}
