/**
 * Iron Router Common templates
 */
Router.configure({

    /**
     * Primary layout template
     */
    layoutTemplate: 'layout',

    /**
     * Page not found template
     *
     * TODO create this template with a link home
     * notFoundTemplate: 'notFound',
     */

    /**
     * Loading template
     *
     * Displays while waitOn calls are waiting
     */
    loadingTemplate: 'loading',

    /**
     * Wait for global subscriptions
     *
     * Put global waits in here for things like notifications, e.g.
     * waitOn: function() {
     *     return [Meteor.subscribe('notifications')]
     * }
     *
     * In our app there aren't any global subscriptions right now.
     */

});


/**
 * Build a controller for showing components
 */
ComponentsListController = RouteController.extend({
    template: 'componentsList',
    increment: 10,
    limit: function() {
        return parseInt(this.params.componentsLimit) || this.increment;
    },
    findOptions: function() {
        return {sort: this.sort, limit: this.limit()};
    },
    waitOn: function() {
        return Meteor.subscribe('components', this.findOptions());
    },
    components: function() {
        return Components.find({}, this.findOptions());
    },
    data: function() {
        var hasMore = this.components().count() === this.limit();
        return {
            components: this.components(),
        };
    }
});


/**
 * Collect the newest components
 *
 * TODO: sort this with more meaningful information
 */
NewComponentsListController = ComponentsListController.extend({
    sort: {order: 1, _id: -1},
});


/**
 * Map URLs to templates
 */
Router.map(function() {

    this.route('home', {
        path: '/',
        controller: NewComponentsListController
    });

    this.route('componentPage', {
        path: '/components/:_id',
        waitOn: function() {
            return [
                Meteor.subscribe('singleComponent', this.params._id),
                Meteor.subscribe('pages', this.params._id)
            ];
        },
        data: function() { return Components.findOne(this.params._id); },
        onBeforeAction: function() {
            console.log('Before action, but only on component pages')
        }

    });

    this.route('pagePage', {
        path: '/pages/:_id',
        waitOn: function() {
            return [
                Meteor.subscribe('singlePage', this.params._id)
            ];
        },
        data: function() { return Pages.findOne(this.params._id); }
    });

});


/**
 * Hook for requiring a login
 *
 * For the future. We could make a feature that would allow people to edit
 * the database on the system
 */
var requireLogin = function(pause) {
    if (! Meteor.user()) {
        if (Meteor.loggingIn())
            this.render(this.loadingTemplate);
        else
            this.render('accessDenied');

        pause();
    }
}

/**
 * Animation control on route change (page navigation)
 */
var IR_AfterHooks = {
    fadeContentOut: function() {
        $('#content').addClass("page-animation fadeOut");
    }
};

//Router.onBeforeAction(function() { console.log 'runs reactively before the action.' });
Router.onBeforeAction('loading');

@@ -2121,126 +2121,8 @@
  %7D,%0A
-        onBeforeAction: function() %7B%0A            console.log('Before action, but only on component pages')%0A        %7D%0A%0A
     

Remove onBeforeAction for componentPage