function Location(attributes) {
  this.name = attributes.name
  this.id = attributes.id
}

Location.prototype.renderTr = function() {
  var html = "<tr><td>"
  html += `<a href="/locations/${this.id}">${this.name}</a></td>`
  html += "<td></td><td></td><td></td><td></td><td>"
  html += `<a href="/locations/${this.id}/events">0</a></td></tr>`

  return html
}

Location.prototype.resetForm = function() {
  $("input[type='submit']").removeAttr("disabled")
  $("#location_name").val("")
}

Location.postSuccess = function(json) {
  var location = new Location(json)
  var locationTr = location.renderTr()
  location.resetForm()

  $('tbody').append(locationTr)
}

Location.formSubmit = function(e) {
  e.preventDefault();

  $.ajax({
    type: "POST",
    url: this.action,
    dataType: "json",
    data: $(this).serialize(),
    success: Location.postSuccess
  });
}

$(document).on('turbolinks:load', function() {

  $("#new_location").on('submit', Location.formSubmit);

  $("a.load_next_location, a.load_previous_location").on("click", function(e) {
    e.preventDefault();

    const indexes = $(this).data("indexes")
    const current_id = +$(this).attr("data-current")
    const current_index = indexes.indexOf(current_id)
    var modified_index = $(this).attr('class') === "load_next_location" ? current_index + 1 : current_index - 1
    if (modified_index >= indexes.length) { modified_index = 0; }
    if (modified_index < 0) { modified_index = indexes.length - 1; }
    const previous_index = modified_index > 0 ? modified_index - 1 : indexes.length - 1
    const next_index = modified_index < indexes.length - 1 ? modified_index + 1 : 0
    const previous_id = indexes[previous_index]
    const next_id = indexes[next_index]

    $.get(this.href).success(function(json) {
      $("div.location_events").hide()
      $("a.load_previous_location")[0].href = "/locations/" + previous_id
      $("a.load_next_location")[0].href = "/locations/" + next_id
      $("a.load_previous_location").attr("data-current", json.id)
      $("a.load_next_location").attr("data-current", json.id)
      $("#name").html("Name: " + json.name)
      $("#street_address").html("Street Address: " + json.street_address)
      $("#city").html("City: " + json.city)
      $("#state").html("State: " + json.state)
      $("#zipcode").html("Zipcode: " + json.zipcode)
    });
  });  

  // Location show events
  $("a.load_location_events").on("click", function(e) {
    e.preventDefault();

    $.get(this.href).success(function(json) {
      $("a.load_location_events").hide()

      var $events = $("div.events")
      var html = "<ol>"

      json.forEach(function(event) {
        html += `<li><a href="/events/${event.id}">${event.name}</a></li>`
      })

      html += "</ol>"

      $events.html(html)
    });
  });  
});

@@ -867,178 +867,32 @@
 %0A%7D%0A%0A
-$(document).on('turbolinks:load', function() %7B%0A%0A  $(%22#new_location%22).on('submit', Location.formSubmit);%0A%0A  $(%22a.load_next_location, a.load_previous_location%22).on(%22click%22,
+Location.clickNavigate =
  fun
@@ -896,34 +896,32 @@
 function(e) %7B%0A  
- 
- 
 e.preventDefault
@@ -925,18 +925,16 @@
 ult();%0A%0A
-  
   const 
@@ -967,18 +967,16 @@
 dexes%22)%0A
-  
   const 
@@ -1020,18 +1020,16 @@
 ent%22)%0A  
- 
- 
 const cu
@@ -1070,18 +1070,16 @@
 ent_id)%0A
-  
   var mo
@@ -1180,18 +1180,16 @@
 dex - 1%0A
-  
   if (mo
@@ -1246,18 +1246,16 @@
  0; %7D%0A  
- 
- 
 if (modi
@@ -1311,18 +1311,16 @@
  - 1; %7D%0A
-  
   const 
@@ -1399,18 +1399,16 @@
 h - 1%0A  
- 
- 
 const ne
@@ -1479,18 +1479,16 @@
 + 1 : 0%0A
-  
   const 
@@ -1525,18 +1525,16 @@
 _index%5D%0A
-  
   const 
@@ -1564,18 +1564,16 @@
 index%5D%0A%0A
-  
   $.get(
@@ -1604,26 +1604,24 @@
 ion(json) %7B%0A
-  
     $(%22div.l
@@ -1644,26 +1644,24 @@
 .hide()%0A    
-  
 $(%22a.load_pr
@@ -1712,34 +1712,32 @@
 previous_id%0A    
-  
 $(%22a.load_next_l
@@ -1780,18 +1780,16 @@
 next_id%0A
-  
     $(%22a
@@ -1844,26 +1844,24 @@
 son.id)%0A    
-  
 $(%22a.load_ne
@@ -1904,26 +1904,24 @@
 son.id)%0A    
-  
 $(%22#name%22).h
@@ -1946,18 +1946,16 @@
 n.name)%0A
-  
     $(%22#
@@ -2018,26 +2018,24 @@
 ddress)%0A    
-  
 $(%22#city%22).h
@@ -2060,18 +2060,16 @@
 n.city)%0A
-  
     $(%22#
@@ -2109,18 +2109,16 @@
 te)%0A    
-  
 $(%22#zipc
@@ -2154,33 +2154,223 @@
 .zipcode)%0A  
-  %7D);%0A  %7D
+%7D);%0A%7D%0A%0A$(document).on('turbolinks:load', function() %7B%0A%0A  $(%22#new_location%22).on('submit', Location.formSubmit);%0A  $(%22a.load_next_location, a.load_previous_location%22).on(%22click%22, Location.clickNavigate
 );  %0A%0A  // L

Refactor Location show page navigation into a new Location function