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


var guidedTourDelegate = {

    trackingNameForSection: function(tracker, id, section) {
        if (id == idToIgnore) {
            return false;
        }
        return id;
    },

    changeToDimensions: function(newDimensions) {
        var container = this.view.view.view();
        if (container) {
            var currentDimensions = container.getDimensions();

            if (this.view.options.shouldAnimateContentChange) {
                new Effect.Parallel([
                    new Effect.Scale(container, ((newDimensions.width / currentDimensions.width) * 100), { scaleY:false, scaleContent:false, sync:true }),
                    new Effect.Scale(container, ((newDimensions.height / currentDimensions.height) * 100), { scaleX:false, scaleContent:false, sync:true })
                ], { duration:this.view.options.animationDuration });
            } else {
                container.style.width = newDimensions.width+'px';
                container.style.height = newDimensions.height+'px';
            }
        }
    },

    didAppendContent: function(view) {
        this.init(view);

        var incoming = view.currentSection;
        if (incoming) {
            this.changeToDimensions(incoming.content.getDimensions());
        }
    },

    init: function(view) {
        if (!this.view) this.view = view;

        this.setShare();

        if (!this.backLink) this.backLink = $$('#backtovideo .toggleshare')[0];
    },

    willShow: function(view, outgoing, incoming) {
        this.init(view);

        if (incoming && incoming.id != 'sharecontainer') {
            view.options.shouldAnimateContentChange = true;
        }

        if (outgoing && outgoing.id == 'sharecontainer') {
            view.options.shouldAnimateContentChange = true;
        }

        if (incoming && incoming.id == 'sharecontainer') {
            try { console.log('yay'); } catch(e) {}
            Share.toggleVideo({
                sharetoggles: [new Element('div'), new Element('div')]
            });
        }
    },

    didShow: function(view, outgoing, incoming) {
        this.init(view);

        view.options.shouldAnimateContentChange = false;

        if (incoming && incoming.id !== 'sharecontainer') {
            if (this.backLink) this.backLink.href = '#'+incoming.id;
        }
    },

    setShare: function(view) {
        var backToVideo = $$('#backtovideo .toggleshare')[0];
        if (backToVideo.hasClassName('replay')) {
            backToVideo.removeClassName('replay');
        }

        this.shareToggles = $$('.toggleshare');
        this.shareToggles.each(function(toggle, i) {
            toggle.observe('click', this.toggleShare.bind(this));
        }.bind(this));

        this.setShare = function() {};
    },

    toggleShare: function(evt) {
        if (evt) evt.stop(evt);

        var toggles = [ 'one', 'two' ];
        toggles.each(function(toggle, i) {
            toggle = new Element('div');
            document.body.appendChild(toggle);
            toggles[i] = toggle;
        })
        Share.toggleVideo({ sharetoggles:toggles });

        if (!!Share.shown) {
            this.view.show(null, true);

            new Effect.Appear('sharecontainer');

            this.view.options.shouldAnimateContentChange = true;
            this.changeToDimensions($('sharecontainer').getDimensions());
            this.view.options.shouldAnimateContentChange = false;
        } else {
            new Effect.Fade('sharecontainer');
            this.view.show(this.view.sectionWithId(this.backLink.href.replace(/.*#/, '')));
        }
    }
}

Event.observe(window, 'load', function() {
    var splash = $('moviecontainer').down('.section');
    if (AC.Detector.isMobile()) {
        splash.addClassName('mobile');
    } else {
        splash.addClassName('standard');

        var sections = $$('.section')

        // grab the default section
        idToIgnore = splash.identify();

        // set the height of the container to the height of the first content
        var height = splash.getHeight();
        $('moviecontainer').style.height = height+'px';

        // For IE we need to remove this node before swap view takes over
        // because swap view's innerHTML = '', breaks the splash
        splash.remove();

        if (!tracker) {
            tracker = new AC.ViewMaster.Tracker('click');
        }
        tracker.setDelegate(guidedTourDelegate);

        // set up the view master
        var view = new AC.ViewMaster.Viewer(sections, 'moviecontainer', 'size', { initialId:idToIgnore, animationDuration:.4, alwaysShowSection:true, shouldAnimateContentChange:true });
        view.setDelegate(guidedTourDelegate);
        view.didAppendContent(splash);

        // special tracking on the download links
        var downloadLinks = $$('.quicklinks .download');
        if (downloadLinks.length > 0) {
            downloadLinks[0].observe('mousedown', function(evt) {
                var ua = navigator.userAgent.toLowerCase();
                if (ua.match('safari')) {
                    AC.Tracking.trackClick({
                        pageName: AC.Tracking.pageName()
                    }, this, 'd', evt.href);
                }
            });
        }
    }
});
