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

(function (ns) {

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

        spec = spec || {};
        spec.viewGate = spec.viewGate || '.inner';

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

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

        my.$viewNode = spec.viewNode;
        my.$viewGate = my.$viewNode.children(spec.viewGate);
        my.$closeButton = my.$viewNode.find('.closeButton a');

        that = {};

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

        var init = function () {
            my.logger.debug('init()');
            my.$closeButton.click(function (e) {
                close();
                e.preventDefault();
            });
            return that;
        };

        var open = function () {
            my.$viewNode.show();
            my.$viewNode.bind('clickoutside', close);
        };

        var close = function (opt) {
            opt = opt || {};
            my.$viewNode.unbind('clickoutside', close);
            if (opt.fade) {
                my.$viewNode.fadeOut();
            } else {
                my.$viewNode.hide();
            }
        };

        var setContent = function (newContent) {
            my.$viewGate.empty();
            my.$viewGate.append(newContent);
        };

        // ------------------------------------------------------------------
        // Public methods
        // ------------------------------------------------------------------

        that.open = open;
        that.close = close;
        that.setContent = setContent;

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

        return init();
    };

    // TODO (yvdm): move to separate file?
    ns.setCreationForm = function (spec, my) {
        var that = {};

        spec = spec || {};

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

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

        my.$viewNode = spec.viewNode;
        my.$setSelector = my.$viewNode.find('select');
        my.$setCreationFields = my.$viewNode.find('.setCreationFields');

        // ------------------------------------------------------------------
        // Private properties
        // ------------------------------------------------------------------

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

        var init = function () {
            my.logger.debug('init()');

            my.$viewNode.find('select').change(function (e) {
                my.logger.debug(this, e.type);
                draw();
            });

            draw();

            return that;
        };

        var draw = function () {
            my.logger.debug('draw()');
            var value = my.$setSelector.val();
            my.$setCreationFields[value === '' ? 'slideDown' : 'slideUp']();
        };

        // ------------------------------------------------------------------
        // Public methods
        // ------------------------------------------------------------------

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

        return init();
    };

})(window.cmu);

