var $ = require('jquery'),
    _ = require('underscore');

var location = function(hasGeolocation) {
  var lat, lon;

  if ( hasGeolocation ) {
    navigator.geolocation.getCurrentPosition(function(position) {
      lat = position.coords.latitude;
      lon = position.coords.longitude;
    }, function(error) {
      console.log(error);
    });
  } else {
    // for local development
    lat = 37.782355;
    lon = -122.409825;
  }

  return {lat: lat, lon: lon};
};

var dumpToDOM = function(results) {
  var resDiv = $('#results');
  resDiv.empty();
  resDiv.append("results available at $('#results').data('results')\n\n");

  
  results.data.forEach(function(fac) {
    fac.set("services",
      _.map(fac.get('services'), function(s) { return s.attributes; } )
      );
    resDiv.append(JSON.stringify(fac, null, '  '));
    resDiv.append("\n");
  });
  resDiv.data('results', results);
};

var queryFunction = function(runWhere) {
  if ( runWhere === 'cloud' ) {
    return _.partial(Parse.Cloud.run, "browse");
  } else {
    return require('cloud/lib/browse');
  }
};

var submit = function(params) {
  // choose where to run the query
  var query = queryFunction(params.runwhere);

  // add location if proximity sorting
  if ( params.sort === 'near' ) {
    // false until we start testing geolocation
    $.extend(params, location(false));
  }

  query(params, {
    success: function(result) {
      dumpToDOM(result);
    }, error: function(err) {
      console.error(err);
    }
  });
};

module.exports = { submit: submit };

@@ -467,437 +467,8 @@
 %7D;%0A%0A
-var dumpToDOM = function(results) %7B%0A  var resDiv = $('#results');%0A  resDiv.empty();%0A  resDiv.append(%22results available at $('#results').data('results')%5Cn%5Cn%22);%0A%0A  %0A  results.data.forEach(function(fac) %7B%0A    fac.set(%22services%22,%0A      _.map(fac.get('services'), function(s) %7B return s.attributes; %7D )%0A      );%0A    resDiv.append(JSON.stringify(fac, null, '  '));%0A    resDiv.append(%22%5Cn%22);%0A  %7D);%0A  resDiv.data('results', results);%0A%7D;%0A%0A
 var 

stop dumping to DOM