var idToIgnore = false;


var guidedTourDelegate = {

    trackingNameForSection: function(tracker, id, section) {
        if (id == idToIgnore||id=='hero') {
            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() {
    if (AC.Detector.isMobile()) {
        $('hero').addClassName('mobile');
    } else {
        $('hero').addClassName('standard');

        var sections = $$('.section')
        sections.unshift($('hero'));

        // grab the default section
        idToIgnore = sections[0];

        // set the height of the container to the height of the first content
        var height = sections[0].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
        sections[0].remove();

        var tracker = new AC.ViewMaster.Tracker('click');
        tracker.setDelegate(guidedTourDelegate);

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