/**
 * Make Nice Input (http://http://www.adrianpalmer.me/projects/jquery/mNI)
 * 
 * Version 1.0b
 * November 21, 2008
 *
 * Copyright (c) 2008 Adrian Palmer (http://www.adrianpalmer.me) Developed whilst under tenure at Revium (http://revium.com.au)
 * Licensed under the GPL licenses.
 * http://www.gnu.org/licenses/gpl.txt
 **/

jQuery.fn.makeNiceInput = function(value) {

    if (typeof (value) == 'undefined') var mNI_value = false;
    else var mNI_value = value;

    this.each(function() {

        name = $(this).attr('name');
        $(this).addClass('inactive');

        if (mNI_value == false) value = getLabel(this).hide().text();
        else {

            value = mNI_value;

            // Needed so we know what value to reinstate if required.
            $(this).addClass('mNI');

        }

        $(this).val(value);

        $(this).focus(function() {

            if ($(this).val() == getLabel(this).text() || ($(this).hasClass('mNI') && $(this).val() == mNI_value)) {

                $(this).val('');
                $(this).addClass('current');
                $(this).removeClass('inactive');

            };

        });

        $(this).blur(function() {

            if ($(this).val() != '') return;

            $(this).removeClass('current');
            $(this).addClass('inactive');

            if (mNI_value == false) $(this).val(getLabel(this).text());
            else $(this).val(mNI_value);

        });

    });

    $('form').submit(function() { $(this).find('input.inactive').val(''); });

};

function getLabel(textBox) {
    var label = $('label[for=' + $(textBox).attr('name') + ']');
    if (label.length == 0) {
        label = $('label[for=' + $(textBox).attr('id') + ']');
    }
    return label;
}