var qtGalleryDelegate = {
    willShow: function(view, outgoing, incoming) {

        if (outgoing) {
            outgoing.triggers();
            outgoing._triggers.each(function(trigger) {
                var item = trigger.up('li');
                if (item) item.removeClassName('active');
                var playing = trigger.down('em');
                if (playing) playing.remove();
            });
        }

        if (incoming) {
            incoming.triggers();
            incoming._triggers.each(function(trigger) {
                var item = trigger.up('li');
                if (item) item.addClassName('active');
                trigger.insert({ top:'<em>playing&hellip;</em>' });
            });
        }
    },

    didShow: function(view) {
        view.currentSection.triggers();
        view.currentSection._triggers.each(function(trigger) {
            if (!trigger.down('b')) view.delegate.willShow(view, null, view.currentSection);
        });

        // only do this the first time
        view.delegate.didShow = null;
    }
}



Event.onDOMReady(function() {
    var contents = $$('.qtcontent');

    // For IE we need to remove this node before swap view takes over
    // because swap view's innerHTML = '', breaks the splash
    if(contents && contents[0]) {
    	contents[0].remove();
	    var link = contents[0].down('a');
    	link.href = link.href.replace(/.*#/, '#');
	}

    var links = $$('a.qtlink');
    links.each(function(link, i) {
        var movieLink = link.cloneNode(true);
        movieLink.addClassName('movieLink');

        var content = contents[i+1];
        if (content) {
            content.appendChild(movieLink);
            link.href = '#'+content.id;
            content.remove();
        }
    });

    var qtGallery = new AC.ViewMaster.Viewer(contents, 'qtview', 'qtlink', { silentTriggers:true, shouldAnimateContentChange:false });
    qtGallery.setDelegate(qtGalleryDelegate);
    qtGallery.show(qtGallery.currentSection);
});
