       success:  => @render()
       error:    => @remove()
 
+  # Activate the clicked layer or
+  # activate the first layer if called without a click event
   selectLayer: (e) ->
-    id = ($target = $(e.currentTarget)).data 'id'
+    id = if e
+      ($target = $(e.currentTarget)).data 'id'
+    else
+      ($target = @$layerButtons.first()).data 'id'
+
     @__activeLayer__ = @layers.get id
     @activateLayerButton $target
     @$layeredSearchResults.html '<div class="loading-spinner"></div>'
     @fetchAndRenderActiveLayer()
+
+    analytics.track.click "Switched to related artworks: #{@__activeLayer__.label()}" if e
 
   fetchAndRenderActiveLayer: ->
     if @activeLayer().artworks.length

    @set fair: @collection.fair if @get('type') is 'fair'
    @artworks = new Artworks
    @artworks.url = "#{ARTSY_URL}/api/v1/related/layer/#{@get('type')}/#{@id}/artworks?artwork[]=#{@get('artwork_id')}"

module.exports.Layers = class Layers extends Backbone.Collection
  url: "#{ARTSY_URL}/api/v1/related/layers"

Backbone        = require 'backbone'
{ ARTSY_URL }   = require('sharify').data
{ Markdown }    = require 'artsy-backbone-mixins'
analytics       = require '../../../lib/analytics.coffee'

Artworks            = require '../../../collections/artworks.coffee'
ArtworkColumnsView  = require '../../../components/artwork_columns/view.coffee'
