if (typeof(tracker) == 'undefined') {
    tracker = false;
}


iPhoneOverlays = {};
Object.extend(iPhoneOverlays, Event.Listener);
Object.extend(iPhoneOverlays, {

    initialize: function(overlay) {
        Event.observe(document, 'click', this._triggerClicked.bindAsEventListener(this));
        this.listenForEvent(AC.ViewMaster, 'ViewMasterWillShowNotification', false, this.willShow);
        this.listenForEvent(AC.OverlayPanel.overlay, 'afterClose', false, this.afterClose);
    },

    trackingNameForSection: function(tracker, id, section) {
        tracker.count++;
        if(!id.match('video')){
            if (id.match('install')){ return id;}
            if (tracker.count<2 || id.match('overlay') || id.match('-slideshow')) {
                 return false;
            }
        }
        return id;
    },

    _triggerClicked: function(evt) {
        var link = evt.findElement('a');
        if (!link || !link.hasClassName('OverlayPanel')) {
            return;
        }

        if (link.target) {
            this.initialId = link.target.replace(/.*#/, '');
        }
    },

    willShow: function(evt) {
        var overlay = evt.event_data.data.sender;
        var section = evt.event_data.data.incomingView;
        tracker.setDelegate(this);

        // we want to reset the slideshows
        if (typeof(AC.OverlayPanel.overlay.slideshows) != 'undefined' && section && section.id) {
            if (section.id.match('hasslideshow')) {
                AC.OverlayPanel.overlay.slideshows.each(function(slideshow) {
                    slideshow.start();
                });
            } else if (!section.id.match('slideshow')) {
                AC.OverlayPanel.overlay.slideshows.each(function(slideshow) {
                    slideshow.stop();
                    slideshow.contentController.show(slideshow.contentController.sectionWithId(slideshow.contentController.orderedSections[0]));
                });
            }
        }

        // if we're an overlay, set up the section
        if (overlay.overlayId == 'OverlayPanel') {

            this.overlay = overlay;
            if (section && section.id) {
                if (section.id.match('gallery')) {
                    this.gallery(section);
                } else if (section.id.match('howto')) {
                    this.howTo(section);
                } else if (section.id == 'install') {
                    try { console.log('install'); } catch(e) {}
                    this.overlay.overlay.addClassName('installoverlay');
                    this.overlay.setOverlayShadowImageSrc('http://images.apple.com/uk/iphone/images/overlay-install-bg-20090619.png');
                }
            }
        }

        this.initialId = null;
    },

    gallery: function(section) {
        this.overlay.overlay.addClassName('galleryoverlay');
        this.overlay.setOverlayShadowImageSrc('http://images.apple.com/uk/iphone/images/overlay-gallery-bg-20090608.png');
        if (!section.gallery) {
            // add a className for non-css3 browsers
            section.content.addClassName('galleryoverlay');

            // add the className to the links
            var links = section.content.select('a');
            links.each(function(link) {
                link.addClassName(section.id+'Trigger');
            });

            // create swap view container
            var container = document.createElement('div');
            container.id = section.id+'SwapView';
            container.className = 'gallerySwapView';
            section.content.appendChild(container);

            // set the initialId if we don't have one
            if (!this.initialId) {
                this.initialId = links[0].href.replace(/.*#/, '');
            }

            // set this so we only do this once
            section.gallery = new AC.ViewMaster.Viewer(links, section.id+'SwapView', section.id+'Trigger', { initialId:this.initialId, silentTriggers:true, shouldAnimateContentChange:!(AC.Detector.isIE()) });

        } else if (this.initialId) {
            section.gallery.show(section.gallery.sectionWithId(this.initialId), true);
        }
    },

    howTo: function(section) {
        this.preloadScrollbarImages();

        this.overlay.overlay.addClassName('howtooverlay');
        this.overlay.setOverlayShadowImageSrc('http://images.apple.com/uk/iphone/images/overlay-howto-bg-20090608.png');
        if (!section.howto) {
            // add a className for non-css3 browsers
            section.content.addClassName('howtooverlay');

            // add some hooks
            var content = section.content.down('div');
            content.addClassName('overflow');

            // create the header bar
            var footer = document.createElement('div');
            footer.className = 'footer';
            footer.innerHTML = '<a class="more" href="/benl/iphone/how-to/">Bekijk alle iPhone-oefeningen</a>';
            section.content.appendChild(footer);

            // set a flag so we only do this once
            section.howto = true;
        }
    },

    preloadScrollbarImages: function(evt) {
        if (!this.imagesHaveBeenPreloaded) {
            var images = [
                'http://images.apple.com/uk/iphone/images/scrollbar-handle-20090608.png',
                'http://images.apple.com/uk/iphone/images/scrollbar-handle-hover-20090608.png',
                'http://images.apple.com/uk/iphone/images/scrollbar-handle-active-20090608.png',
                'http://images.apple.com/uk/iphone/images/scrollbar-20090608.png',
                'http://images.apple.com/uk/iphone/images/scrollbar-hover-20090608.png',
                'http://images.apple.com/uk/iphone/images/scrollbar-active-20090608.png'
            ];
            images.each(function(image) {
                (new Image()).src = image;
            });

            this.imagesHaveBeenPreloaded = true;
        }
    },

    afterClose: function(evt) {
        var overlay = evt.event_data.data;

        // remove anything from the overlay we may have added
        overlay.overlay.removeClassName('howtooverlay');
        overlay.overlay.removeClassName('galleryoverlay');
        if (typeof(AC.OverlayPanel.overlay.slideshows) != 'undefined') {
            AC.OverlayPanel.overlay.slideshows.each(function(slideshow) {
                slideshow.stop();
            });
        }
        overlay.setOverlayShadowImageSrc(null);
    }

});



Event.onDOMReady(function() {
    iPhoneOverlays.initialize();

    if (tracker == false) {
        tracker = new AC.ViewMaster.Tracker('click');
    }
});
