// Everything in this file is wrapped inside an anonymous function, so doesn't
// put anything into the global namespace. You shouldn't even notice it's
// there.
//
// However, it does use jQuery for events and the like, so any other code that
// does event-handling etc should make sure to play nice. More info can be
// found at <http://jquery.com/>

(function ($) {
// ------------

function focus_self_label () {
    var me = $(this);
    if (me.hasClass('showing-label')) {
        me.removeClass('showing-label');
        if (me.val() == me.attr('title')) {
            me.val('');
        }
    }
}

function blur_self_label () {
    var me = $(this);
    if (!me.val() || me.val() == me.attr('title')) {
        me.addClass('showing-label');
        me.val(me.attr('title'));
    }
}

function label_with_title () {
    var me = $(this);
    if (!me.val()) {
        me.val(me.attr('title'));
        me.addClass('showing-label');
    }
    me.focus(focus_self_label);
    me.blur(blur_self_label);
    me.focus().blur();
};

function clear_self_labels () {
    var me = $(this);
    inps = me.find('.self-label');
    inps.each(function () {
        var input = $(this);
        input.unbind('blur');
        input.focus();
        input.blur();
    });
}

// on domready
$(function () {
    $('.self-label').each(label_with_title);
    $('form').submit(clear_self_labels);
});


// --------
})(jQuery);
