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

(function (ns) {

    /* TODO (ydvm): consider renaming, since this is really only some glue between some buttons and the lightbox */
    ns.objectDetail = function (spec, my) {
        var that = {};

        spec = spec || {};
        spec.imageViewer = spec.imageViewer || '.imageViewer';
        spec.lightbox = spec.lightbox || '#lightbox';
        spec.lightboxButton = spec.lightboxButton || '.fullscreen';

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

        my = my || {};
        my.logger = gj.logging.getLogger('cmu.objectDetail');
        my.open = false;

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

        my.$viewNode = spec.viewNode;
        my.$imageViewer = $(spec.imageViewer);
        my.$lightbox = $(spec.lightbox);
        my.$lightboxImage = my.$lightbox.find('.main .image');
        my.$lightboxButton = $(spec.lightboxButton);
        my.$filmStrip = null; // Enabled when the lightbox is first opened

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

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

            if (!my.$imageViewer.length) {
                my.logger.warning('No image viewer found!');
                return null;
            }
            if (!my.$lightbox.length) {
                my.logger.warning('No lightbox found!');
                return null;
            }

            my.$imageViewer.addClass('lightboxEnabled');
            activateBindings('init');

            if (window.cmu._lightboxOpen) {
                openLightbox();
            }

            return that;
        };

        var toggleLightbox = function () {
            if (!my.open) {
                openLightbox();
            } else {
                closeLightbox();
            }
        };

        var openLightbox = function () {
            my.logger.debug('openLightbox()');
            my.$lightbox.show();

            if (!my.$filmStrip) {
                my.$lightbox.find('.filmStrip').each(function () {
                    my.$filmStrip = cmu.filmStrip({
                        viewNode: $(this),
                        minimumItems: 11,
                        navigationBatchSize: 7
                    });
                });
            }

            activateBindings('lightbox');
            my.$lightboxImage.bind('clickoutside', closeLightbox);
            my.open = true;
            window.cmu._lightboxOpen = my.open;
        };

        var closeLightbox = function () {
            my.logger.debug('closeLightbox()');
            my.$lightbox.hide();
            my.$lightboxImage.unbind('clickoutside', closeLightbox);
            deactivateBindings('lightbox');
            my.open = false;
            window.cmu._lightboxOpen = my.open;
        };

        var previousImage = function () {
            my.logger.debug('previousImage()');
            window.cmu.lightbox.previousImage();
        };

        var nextImage = function () {
            my.logger.debug('nextImage()');
            window.cmu.lightbox.nextImage();
        };

        var activateBindings = function (category) {
            var key, binding;
            for (key in my.keyBindings[category]) {
                binding = my.keyBindings[category][key];
                $(document).bind('keydown', key, binding);
            }
        };
        var deactivateBindings = function (category) {
            var key, binding;
            for (key in my.keyBindings[category]) {
                binding = my.keyBindings[category][key];
                $(document).unbind('keydown', binding);
            }
        };

        my.keyBindings = {
            'init': {
                'l': toggleLightbox
            },
            'lightbox': {
                'esc': closeLightbox,
                'left': previousImage,
                'right': nextImage
            }
        };

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

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

        my.$imageViewer.find('.main li').click(function () {
            openLightbox();
            return false;
        });

        my.$lightboxButton.find('a').click(function () {
            my.logger.debug('click()', this);
            openLightbox();
            return false;
        });

        my.$lightbox.find('.closeButton a, .main .image').click(function () {
            closeLightbox();
            return false;
        });

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

        return init();
    };

})(window.cmu);

