(function() {
  var app = angular.module('marketFinder', ['marketFilters']);
  
  app.controller('marketsController', ['$http', '$scope', function ($http, $scope) {
    var markets = this;
    markets.loaded = false;

    function navSuccess(pos) {
      markets.position = pos.coords;
      markets.markets = [];
      
      $http.get("http://search.ams.usda.gov/farmersmarkets/v1/data.svc/locSearch?lat=" + markets.position.latitude + "&lng=" + markets.position.longitude).
      success(function(data){
        markets.markets = data.results;
        angular.forEach(markets.markets, function (market) {
          $http.get("http://search.ams.usda.gov/farmersmarkets/v1/data.svc/mktDetail?id=" + market.id).
          success(function(data) {
            market.details = data.marketdetails;
          });
        });
      }).
      error(function(data, status, headers, config) {
        alert("Something is up with the API. Investigations have commenced!!");
        market.details = data;
      });
      markets.loaded = true;
    };

    $scope.zipSearch = function() 
    {
      markets.loaded = false;
      // markets.markets = [];
      // markets.zip = 11215
      $http.get("http://search.ams.usda.gov/farmersmarkets/v1/data.svc/zipSearch?zip=" + markets.zip).success(function(data){
        markets.markets = data.results;
        angular.forEach(markets.markets, function (market) {
          $http.get("http://search.ams.usda.gov/farmersmarkets/v1/data.svc/mktDetail?id=" + market.id).success(function(data) {
            market.details = data.marketdetails;
          });
        });
      });
      $scope.$apply();
      markets.loaded = true;
    };

    $scope.init = function() {navigator.geolocation.getCurrentPosition(navSuccess);}
    $scope.init();
  }]);

  app.controller('marketController', ['$http', function($http) {
    var market = this;
    market.show = false;

    this.showDetail = function() {
      market.show = true;
    };

    this.hideDetail = function() {
      market.show = false;
    };
  }]);

  angular.module('marketFilters', []).
  filter('formatMarketName', function() {
    return function (input) {
      var distance = input.match(/^\d.\d/).join();
      return input.replace(distance, "")
    };
  }).filter('formatMarketDistance', function() {
    return function (input) {
      return input.match(/^\d.\d/).join();
    };
  }).filter('removeBreak', function() {
    return function (input) {
      if (input) {
        var cleanedInput =  input.replace(/<br>/g, "");
        return cleanedInput
      }
    };
  }).filter('listProducts', function() {
    return function (input) {
      if (input) { 
        var cleanedInput =  input.replace(/\;/g, " *");
        return cleanedInput
      }
    };
  }).filter('embedMap', function() {
    return function (input) {
      if (input) {
        var cleanedInput =  "https://www.google.com/maps/embed/v1/place" + input.match(/\?q=.+/);
        return cleanedInput
      }
    };
  });
})();

@@ -875,24 +875,302 @@
 , config) %7B%0A
+        console.log(%22=====data=====%22);%0A        console.log(data);%0A        console.log(%22=====status=====%22);%0A        console.log(status);%0A        console.log(%22=====headers=====%22);%0A        console.log(headers);%0A        console.log(%22=====config=====%22);%0A        console.log(config);%0A
         aler
@@ -1241,39 +1241,8 @@
 %22);%0A
-        market.details = data;%0A
     

update error logging