// Extend jQuery to add a function for implementing "Nice Inputs"
jQuery.fn.makeNiceInput = function() {


    jQuery(this).each(function() {

        if (jQuery('label[for=' + jQuery(this).attr('id') + ']').length > 0) {

            SetMniMaxlength(this, "maxlength", "mni-maxlength");

            jQuery(this).val(jQuery('label[for=' + jQuery(this).attr('id') + ']').addClass('isMNice').hide().text()).addClass('isMNice');

        }

    });

    jQuery(this).bind('focus', function() {
        if (jQuery(this).hasClass('isMNice') && jQuery(this).val() == jQuery('label.isMNice[for=' + jQuery(this).attr('id') + ']').text()) {

            SetMniMaxlength(this, "mni-maxlength", "maxlength");

            jQuery(this).val('')
            jQuery(this).addClass('MNice_current')

        }

    });

    jQuery(this).bind('blur', function() {

        if (jQuery(this).hasClass('isMNice') && jQuery(this).val() == '') {

            SetMniMaxlength(this, "maxlength", "mni-maxlength");

            jQuery(this).val(jQuery('label.isMNice[for=' + jQuery(this).attr('id') + ']').text())
            jQuery(this).removeClass('MNice_current')

        }

    });

    jQuery('form').bind('submit', function() {

        jQuery(this).find('.isMNice').each(function() {

            if (jQuery(this).val() == jQuery('label.isMNice[for=' + jQuery(this).attr('id') + ']').text()) {

                jQuery(this).val('');

            }

        });

    });

}

function SetMniMaxlength(elem, cml, nml) {
    if ($(elem).filter("[" + cml + "]").length) {
        $(elem).attr(nml, $(elem).attr(cml)).removeAttr(cml);
    }
}
