var GroceryList = Class.extend({

  init: function() {
    this.getGroceryList(true);
    this.observeProductEntries();
    this.observeProductDeleteClicks();
    this.observeZipCodeEntry();
    this.observeClearClick();
  },

  getGroceryList: function(forceRefresh) {

    var self = this;

    if (!forceRefresh) {
      forceRefresh = false;
    } else {
      forceRefresh = true;
    }

    $.ajax({
      url: getGroceryListUrl,
      data: {
        "force_refresh":forceRefresh
      },
      success: function(data) {
        self.updateGroceryList(data);
      },
      failure: function(data) {
        alert("Update failed.");
      }
    });
  },

  saveLocation: function(zip) {

    if (this.zip == zip) {
      return;
    }

    this.zip = zip;
    var self = this;

    $.ajax({
      url: geocodeZipUrl,
      data: {
        "zip":zip
      },
      success: function(data) {
        self.getGroceryList(true);
        $("#latitude-label").html("Latitude: " + data.latitude);
      },
      failure: function(data) {
        alert("Update failed.");
      }
    });
  },

  observeZipCodeEntry: function() {

    var self = this;

    $("#zip").keyup(function() {
      if ($(this).val().length == 5) {
        self.saveLocation($(this).val());
      }
    });

    $("#zip").blur(function() {
      self.saveLocation($(this).val());
    });
  },

  observeProductDeleteClicks: function() {

    $("a.delete").click(function() {

      var productName = $(this).parents("li").find(".product-name a").html();

      // If this is the last item and we're deleting it, just hide the whole list.
      if ($("#grocery-list tr.item").length == 1) {
        $("#grocery-list").hide();
      }

      $(this).parents("li").remove();

      $.ajax({
        url: groceryListDeleteProductUrl,
        data: {
          "product":productName
        },
        failure: function(data) {
          alert("Update failed.");
        }
      });

      return false;
    });
  },

  observeClearClick: function() {
    $("#clear-list").click(function() {
      $("#grocery-row-container").html("");

      $.ajax({
        url: groceryListClearUrl,
        failure: function(data) {
          alert("Update failed.");
        }
      });

      return false;
    });
  },

  observeProductEntries: function() {

    var self = this;

    $("#grocery-list-form").submit(function() {

      $.ajax({
        url: groceryListAddProductUrl,
        data: {
          "product":$("#item").val()
        },
        success: function(data) {
          self.updateGroceryList(data);
        },
        failure: function(data) {
          alert("Update failed.");
        }
      });

      $("#item").val("");

      return false;
    });
  },

  updateGroceryList: function(items) {

    var row;
    var link;
    $("#grocery-row-container li").remove();

    $.each(items, function(count, item) {

      if (item.closest_seller_url != '') {
        link = "<a href=\"" + item.closest_seller_url + "\">" + item.closest_seller_name + "</a>";
      } else {
        link = item.closest_seller_name;
      }

      row = "<li class=\"item " + item.rating + "\">"
        +   "  <div class=\"item-info\">"
        +   "    <h3 class=\"product-name first\"><a href=\"" + item.url + "\">" + item.name + "</a></h3>"
        +   "    <p>" + item.in_season + "</p>"
        +   "    <p>Closest seller: " + link + "</p>"
        +   "  </div>"
        +   "  <a href=\"#\" class=\"delete\">X</a>"
        +   "  <div class=\"clear\"></div>"
        +   "</li>";

      $("#grocery-row-container").append(row);
    });

    if (items.length > 0) {
      $("#grocery-list").show();
    }

    this.observeProductDeleteClicks();
  }
});

