   $(".categories-chooser").chosen()
 
 Template.category.helpers
-  'events': -> getEvents(categories: Session.get "category")
+  'events': -> get_events(categories: Session.get "category")
 
 Template.search.helpers
   'found_events': ->
     q = Session.get("q")
     re = new RegExp("#{q}.*", 'i')
-    getEvents($or: [{name: re}, {description: re}, {categories: re}, {location: re}])
+    get_events($or: [{name: re}, {description: re}, {categories: re}, {location: re}])

      event_ids = []
      not_flat = Meteor.users.find("profile.admin": true).map (admin) -> admin?.profile?.events or []
      event_ids = event_ids.concat.apply(event_ids, not_flat)
    getEvents({_id: {$in: event_ids}})

# user template
Template.user.helpers
  'info': ->
    Meteor.users.findOne(Session.get("user_id")) or {}
  'user_events': ->
    getEvents(creator: Session.get("user_id")) or []
  'following': ->
    Meteor.user()?.profile?.following.indexOf(Session.get("user_id")) > -1


    else
      return []
  return categories

parse_event_from_form = (form) ->
  event = form.serializeObject()
  event.categories = fix_categories(event.categories)

  date_start = moment(event.date_start, DATE_FORMAT)
  time_start = moment(event.time_start, TIME_FORMAT)

  delete event['date_start']
  delete event['time_start']

  event.from = date_start
    .hour(time_start.hour())
    .minute(time_start.minute())
    .toDate()

  date_end = moment(event.date_end, DATE_FORMAT)
  time_end = moment(event.time_end, TIME_FORMAT)

  delete event['date_end']
  delete event['time_end']

  event.to = date_end
    .hour(time_end.hour())
    .minute(time_end.minute())
    .toDate()

  return event

get_events = (criteria, projection) ->
  if not criteria?
    criteria = {}
  if not projection?
    projection = {}
  _.extend(criteria, {to: {$gte: new Date()}})
  _.extend(projection, {sort: {from: 1}})
  Events.find(criteria, projection).fetch()

Template.topbar.events
  'click .logout': (e) -> Meteor.logout()
