
// GEOSEARCH /////////////////////////////////////////////////////////

window.setupGeosearch = function() {
  $('#geosearch').keypress(function(e) {
    if((e.keyCode ? e.keyCode : e.which) != 13) return;

    var search = $(this).val();

    if ( window.search(search) ) return;

    e.preventDefault();
  });
  $('#geosearchwrapper img').click(function(){
    map.locate({setView : true, maxZoom: 13});
  });
  $('#geosearch').keyup(function(){$(this).removeClass('search_not_found')});

  window.searchResultLayer = L.layerGroup();
  map.addLayer(window.searchResultLayer);
}

window.search = function(search) {
  window.searchResultLayer.clearLayers();

  var searchMarkerOptions = {color:'red', weight:3, opacity: 0.5, fill:false, dashArray:"5,5", clickable:false};


  if (!runHooks('geoSearch', search)) {
    return true;
  }

  if(search.split(",").length == 2) {
    var ll = search.split(",");
    if(!isNaN(ll[0]) && !isNaN(ll[1])) {
      ll = [parseFloat(ll[0]), parseFloat(ll[1])];
      if(ll[0] >= -90 && ll[0] <= 90 && ll[1] >= -180 && ll[1] <= 180) {
        var ll = L.latLng(ll);
        window.map.setView(ll, 17);

        window.searchResultLayer.clearLayers();
        window.searchResultLayer.addLayer(L.circleMarker(ll,searchMarkerOptions));

        if(window.isSmartphone()) window.show('map');
        return true;
      }
    }
  }

  $.getJSON(NOMINATIM + encodeURIComponent(search), function(data) {
    if(!data || !data[0]) {
      $('#geosearch').addClass('search_not_found');
      return true;
    }
    var b = data[0].boundingbox;
    if(!b) return true;
    var southWest = new L.LatLng(b[0], b[2]),
        northEast = new L.LatLng(b[1], b[3]),
        bounds = new L.LatLngBounds(southWest, northEast);

    window.searchResultLayer.clearLayers();
    if (southWest.equals(northEast)) {
        window.searchResultLayer.addLayer(L.circleMarker(southWest,searchMarkerOptions));
    } else {
      window.searchResultLayer.addLayer(L.rectangle(bounds,searchMarkerOptions));
    }

    window.map.fitBounds(bounds, {maxZoom: 17});
    if(window.isSmartphone()) window.show('map');
  });
}

@@ -196,24 +196,28 @@
 13) return;%0A
+    
 %0A    var sea
@@ -492,292 +492,48 @@
 %7D);%0A
-%0A  window.searchResultLayer = L.layerGroup();%0A  map.addLayer(window.searchResultLayer);%0A%7D%0A%0Awindow.search = function(search) %7B%0A  window.searchResultLayer.clearLayers();%0A%0A  var searchMarkerOptions = %7Bcolor:'red', weight:3, opacity: 0.5, fill:false, dashArray:%225,5%22, clickable:false%7D;%0A%0A%0A
+%7D%0A%0Awindow.search = function(search) %7B%0A  
   if
@@ -564,24 +564,26 @@
  search)) %7B%0A
+  
     return t
@@ -589,19 +589,27 @@
 true;%0A  
-%7D%0A%0A
+  %7D%0A    %0A  
   if(sea
@@ -638,24 +638,26 @@
 == 2) %7B%0A    
+  
 var ll = sea
@@ -672,16 +672,18 @@
 t(%22,%22);%0A
+  
     if(!
@@ -721,16 +721,18 @@
 %7B%0A      
+  
 ll = %5Bpa
@@ -768,16 +768,18 @@
 l%5B1%5D)%5D;%0A
+  
       if
@@ -855,200 +855,60 @@
     
-var ll = L.latLng(ll);%0A        window.map.setView(ll, 17);%0A%0A        window.searchResultLayer.clearLayers();%0A        window.searchResultLayer.addLayer(L.circleMarker(ll,searchMarkerOptions));%0A%0A
+  window.map.setView(L.latLng(ll%5B0%5D, ll%5B1%5D), 17);%0A  
     
@@ -961,24 +961,26 @@
 ');%0A        
+  
 return true;
@@ -990,23 +990,31 @@
     
+  
 %7D%0A    
+  
 %7D%0A  
+  
 %7D%0A%0A  
+  
 $.ge
@@ -1072,24 +1072,26 @@
 ion(data) %7B%0A
+  
     if(!data
@@ -1112,16 +1112,18 @@
 %7B%0A      
+  
 $('#geos
@@ -1159,23 +1159,31 @@
 found');
-%0A
+      %0A 
       
+ 
 return t
@@ -1187,26 +1187,30 @@
 n true;%0A    
-%7D%0A
+  %7D%0A  
     var b = 
@@ -1230,24 +1230,26 @@
 ingbox;%0A    
+  
 if(!b) retur
@@ -1252,24 +1252,26 @@
 eturn true;%0A
+  
     var sout
@@ -1300,24 +1300,26 @@
 %5B0%5D, b%5B2%5D),%0A
+  
         nort
@@ -1360,16 +1360,18 @@
         
+  
 bounds =
@@ -1417,284 +1417,10 @@
 t);%0A
-%0A    window.searchResultLayer.clearLayers();%0A    if (southWest.equals(northEast)) %7B%0A        window.searchResultLayer.addLayer(L.circleMarker(southWest,searchMarkerOptions));%0A    %7D else %7B%0A      window.searchResultLayer.addLayer(L.rectangle(bounds,searchMarkerOptions));%0A    %7D%0A%0A
+  
     
@@ -1464,16 +1464,18 @@
 : 17%7D);%0A
+  
     if(w
@@ -1516,16 +1516,18 @@
 'map');%0A
+  
   %7D);%0A%7D%0A

Revert "search: draw a marker highlighting search results (rectangle for region, 10px circle for point)"