NestedOverlay = {};
Object.extend(NestedOverlay, Event.Listener);
Object.extend(NestedOverlay, Event.Publisher);
Object.extend(NestedOverlay, {
    overlayShadowImageSrc: '/global/elements/zoomerlay/zoomerlay_bg.png',

    init: function() {
        this.listenForEvent(AC.ViewMaster, 'ViewMasterWillShowNotification', false, this.willShow);
        this.listenForEvent(AC.ViewMaster, 'ViewMasterDidShowNotification', false, this.didShow);
    },

    willShow: function(evt) {
        var data = evt.event_data.data;
        // incomingView outgoingView sender

        // pause the in page vidoes when the overlay opens, and play it again when it closes
        if (typeof(movieToPause) != 'undefined') {
            if (data.sender == AC.OverlayPanel.overlay) {
                if (data.incomingView) {
                    if (movieToPause && typeof(movieToPause.sectionWithId) == 'function') {
                        var section = movieToPause.sectionWithId('movieposter');
                        if (section && movieToPause.currentSection && movieToPause.currentSection._movieController) {
                            this.wasPlaying = movieToPause.currentSection._movieController.playing();
                            this.movieType = movieToPause.currentSection._movieController.movieType();
                            if (this.wasPlaying === true && this.movieType == 'Video') {
                                movieToPause.currentSection._movieController.pause();
                            } else if (!movieToPause.currentSection.content.down('.endState')) {
                                movieToPause.show(section);
                            }
                        }
                    }
                } else {
                    if (movieToPause && typeof(movieToPause.sectionWithId) == 'function') {
                        var section = movieToPause.sectionWithId('movie');
                        if (this.wasPlaying === true && this.movieType == 'Video') {
                            movieToPause.currentSection._movieController.play();
                        } else if (movieToPause.currentSection != section) {
                            movieToPause.show(section);
                        }
                    }
                    this.wasPlaying = null;
                    this.movieType = null;
                }
            }
        }
    },

    didShow: function(evt) {
        var data = evt.event_data.data;
        // incomingView outgoingView sender

        // set up the overlay galleries
        if (data.incomingView) {
            var content = data.incomingView.content;
            if (content.match('.nested')) {
                AC.OverlayPanel.overlay.setOverlayShadowImageSrc(this.overlayShadowImageSrc);
                this.createNested(data.incomingView);
            }
        }
    },

    createNested: function(section) {
        if (!section.nested) {
            var id = section.id.replace(/overlay-/, '');
            var swapId = 'nested-'+id;
            var triggerClassName = 'nested-trigger-'+id;

            var swap = section.content.down('.nested-swap');
            swap.id = swapId;

            var links = section.content.select('a.nested-trigger');
            for (var i=links.length-1; i>=0; i--) {
                links[i].addClassName(triggerClassName);
            }

            section.nested = new AC.ViewMaster.Viewer(links, swap, triggerClassName, { silentTriggers:true, shouldAnimateContentChange:false });
        }
    }

});

Event.onDOMReady(NestedOverlay.init.bind(NestedOverlay));

