-# The ammount by which we increate the limit of returned results
+# The amount by which we increase the limit of returned results
 LIMIT_INCREASE_STEP = 10
 
 # List of all session variables that activate views with catalogs (used to determine infinite scrolling)


    return # Make sure CoffeeScript does not return anything

# Make sure onCatalogRendered gets executed once after rendered is done and new elements are in the DOM.
# Otherwise we might increase limit multiple times in a row, before the DOM updates.
onCatalogRenderedRunning = false

onCatalogRendered = (template, variables) ->
  onCatalogRenderedRunning = true

  renderedChildren = $(template.find '.item-list').children().length
  expectedChildren = Math.min(Session.get(variables.count), Session.get(variables.limit))

  if expectedChildren is renderedChildren
    onCatalogRenderedRunning = false
    Session.set variables.limitIncreasing, false
    # Trigger scrolling to automatically start loading more results until whole screen is filled
    $(window).trigger('scroll')
  else
    # Give the engine more time to render things
    setTimeout ->
      onCatalogRendered template, variables
    ,
      500

Template.catalogList.rendered = ->
  onCatalogRendered @, @data.variables unless onCatalogRenderedRunning

  # Focus on the filter
  $(@find '.filter input').focus()


Template.catalogLoading.events
  'click .load-more': (event, template) ->
    event.preventDefault()
    Session.set @variables.limitIncreasing, false # We want to force loading more in every case
    increaseLimit LIMIT_INCREASE_STEP, @variables

