Template.filter.events({
	'change .js-toggle-property-filter': function(event, instance) {
		event.preventDefault();

		var parentInstance = instance.parentInstance();

		parentInstance.filter.add('upcomingEvent', instance.$('#hasUpcomingEvent').prop('checked'));
		parentInstance.filter.add('needsHost', instance.$('#needsHost').prop('checked'));
		parentInstance.filter.add('needsMentor', instance.$('#needsMentor').prop('checked'));
		parentInstance.filter.done();

		parentInstance.updateUrl();
	},

	'mouseover .js-category-selection-label': function(event, instance) {
		var category = this;
		courseFilterPreview(('.category-' + category), true);
		instance.$('.js-category-label.category-' + category).addClass('highlight');
	},

	'mouseout .js-category-selection-label': function(event, instance) {
		var category = this;
		courseFilterPreview(('.category-' + category), false);
		instance.$('.js-category-label.category-' + category).removeClass('highlight');
	},

	'keyup .js-search-categories': function(event, instance) {
		var parentInstance = instance.parentInstance();
		var query = instance.$('.js-search-categories').val();

		parentInstance.updateCategorySearchDebounced(query);
	},

	'click .js-search-categories': function(event, instance) {
		instance.$('.dropdown-toggle').dropdown('toggle');
	},

	'click .js-toggle-subcategories': function(event, instance) {
		var category = this;

		instance.$(".js-sub-category" + "." + category).toggle();
		instance.$(".js-toggle-subcategories." + category + " span")
		        .toggleClass('fa-angle-down fa-angle-up');
		event.stopPropagation();
	},

	'click .js-category-selection-label': function(event, instance) {
		event.preventDefault();
		var parentInstance = instance.parentInstance();

		// Add to filter
		var category = this; // Event context is the category id
		parentInstance.filter.add('categories', "" + category).done();

		// Clear search field
		instance.$('.js-search-categories').val('');

		parentInstance.updateCategorySearch('');
		parentInstance.updateUrl();

		window.scrollTo(0, 0);
	},

	'click .js-remove-category-btn': function(event, instance) {
		var parentInstance = instance.parentInstance();
		var category = this;

		parentInstance.filter.remove('categories', '' + category).done();
		parentInstance.updateUrl();
		$('#find').select();
	},
});


Template.filter.helpers({
	'toggleChecked': function(name) {
		var parentInstance = Template.instance().parentInstance();
		return parentInstance.filter.get(name) ? 'checked' : '';
	},

	'categories': function() {
		var parentInstance = Template.instance().parentInstance();
		return parentInstance.filter.get('categories');
	},

	'availableCategories': function() {
		var parentInstance = Template.instance().parentInstance();
		return Object.keys(parentInstance.categorySearchResults.get('categorySearchResults'));
	},

	'availableSubcategories': function(mainCategory) {
		var parentInstance = Template.instance().parentInstance();
		return parentInstance.categorySearchResults.get()[mainCategory];
	},

	'categoryNameMarked': function() {
		Session.get('locale'); // Reactive dependency
		var parentInstance = Template.instance().parentInstance();
		var search = parentInstance.categorySearch.get();
		var name = mf('category.'+this);
		return markedName(search, name);
	},

	'isMobile': function() {
		var screenXS = SCSSVars.screenXS;
		return Session.get('viewportWidth') <= screenXS;
	}
});

Template.filter.onRendered(function() {
	var instance = this;

	instance.$('.js-filter-upcoming-events').on('mouseover mouseout', function(e) {
		var activate = e.type == 'mouseover';
		courseFilterPreview('.has-upcoming-events', activate);
	});

	instance.$('.js-filter-needs-host').on('mouseover mouseout', function(e) {
		var activate = e.type == 'mouseover';
		courseFilterPreview('.needs-host', activate);
	});

	instance.$('.js-filter-needs-mentor').on('mouseover mouseout', function(e) {
		var activate = e.type == 'mouseover';
		courseFilterPreview('.needs-mentor', activate);
	});

	instance.$('.filter-categories-select').on('show.bs.dropdown hide.bs.dropdown', function(e) {
		var dropdownMenu = $(e.target).find('.dropdown-menu').first();

		dropdownMenu.stop(true, true).slideToggle(200);
		instance.$('.dropdown-toggle').fadeToggle(200);
	});

	instance.$('.filter-categories-select').on('shown.bs.dropdown', function(e) {
		instance.$('.js-search-categories').select();
	});
});

@@ -545,39 +545,24 @@
 ': function(
-event, instance
 ) %7B%0A%09%09var ca
@@ -630,33 +630,24 @@
 ), true);%0A%09%09
-instance.
 $('.js-categ
@@ -754,39 +754,24 @@
 ': function(
-event, instance
 ) %7B%0A%09%09var ca
@@ -840,33 +840,24 @@
 , false);%0A%09%09
-instance.
 $('.js-categ

revert false scoping