window.cmu = window.cmu || {};

(function (ns) {

    ns.inlineLabel = function (spec, my) {
        var that;

        spec = spec || {};

        // ------------------------------------------------------------------
        // Shared properties
        // ------------------------------------------------------------------

        my = my || {};
        my.logger = gj.logging.getLogger('cmu.inlineLabel');

        my.$viewNode = spec.viewNode;
        my.$input = my.$viewNode.find('input[type="text"]');

        my.$form = my.$input.closest('form');
        my.$label = my.$form.find('label[for=' + my.$input.attr('id') + ']');
        my.labelText = $.trim(my.$label.text());

        that = {};

        // ------------------------------------------------------------------
        // Private methods
        // ------------------------------------------------------------------

        var init = function () {
            my.logger.debug('init()', my.$viewNode);
            my.$viewNode.addClass('jsEnabled');
            setInlineLabel();
        };

        var setInlineLabel = function () {
            my.logger.debug('setInlineLabel()');
            my.$input.val(my.labelText);
            my.$input.addClass('labeled');
        };

        // ------------------------------------------------------------------
        // Event handlers
        // ------------------------------------------------------------------

        my.$input.focus(function () {
            my.$input.removeClass('labeled')
            my.$input.addClass('focus');

            if (my.$input.val() === my.labelText) {
                my.$input.val('');
            }
        });
        my.$input.blur(function () {
            my.$input.removeClass('focus');

            if (!my.$input.val()) {
                setInlineLabel();
            }
        });

        // ------------------------------------------------------------------
        // Constructor
        // ------------------------------------------------------------------

        init();
        return that;
    };

})(window.cmu);

