$(function() {
  search.content=$('#leftpantab #leftsearch .leftcontent')[0];
});

search.enable = function(){
  osm.map.addLayer(osm.layers.search_marker);
};
search.disable = function(){
  osm.map.removeLayer(osm.layers.search_marker);
};


search.processResults = function(results) {
  try {
    $("#leftsearch .loader").removeClass('on');
    if (results.error) {
      search.content.innerHTML='Произошла ошибка: ' + (results.error);
    } else if (results.find==0) {
      search.q=results.search;
      search.content.innerHTML='<p>Ничего не найдено по запросу "' + (results.search)  + '"</p><br /><br />\
          <p>Оставьте заявку об отсутствующем у нас адресе или неправильной работе поиска<br><br>\
          Комментарий (запрос указывать не надо):\
          </p>\
          <form onsubmit="return search.reportError();">\
          <p><textarea id="rsearch" style="width: 95%;"></textarea></p>\
          <p style="text-align: center;"><input type="submit" style=""></p>\
          </form>';
    }
    else if (results.find==1 && results.accuracy_find==0) {
      search.content.innerHTML='Пожалуйста, уточните запрос "' + (results.search) + '"';
    }
    else {
      var content = '<ul id="ol-search_result">';
      osm.layers.search_marker.clearLayers();
      var matches=results.matches;
      for (var i in matches) {
        zoom = matches[i].this_poi?16:matches[i].addr_type_id*2+4;
        content += ('<li><a href="" onClick="osm.map.setView(new L.LatLng(' + matches[i].lat + ',' + matches[i].lon + '), '+zoom+'); return false;" info="id='+matches[i].id+'  weight='+matches[i].weight+'">' + matches[i].display_name + '</a></li>');
        var marker = new L.Marker(new L.LatLng(matches[i].lat, matches[i].lon), {icon: new search.Icon()});
        if (matches[i].this_poi) {
          osm.poi.createPopup(matches[i].id, marker);
        }
        else {
          marker.bindPopup("<b>Адрес:</b><br /> " + matches[i].display_name);
        }
        osm.layers.search_marker.addLayer(marker);
      }
      zoom = matches[0].this_poi?14:matches[0].addr_type_id*2+4
      osm.map.setView(new L.LatLng(matches[0].lat, matches[0].lon), zoom);
      content += '</ul>';
      search.content.innerHTML = content;
    }
  } catch(e) {
    search.content.innerHTML = 'Ошибка: ' + e.description + '<br /> Ответ поиск.серв.: '+results.error;
  }
};

search.reportError = function() {
  comment=$_('rsearch').value;
  $.get("/api/search_report_add", {search: search.q, comment: comment.replace("\n", " ")} );
  search.content.innerHTML='Спасибо за помощь в улучшении OpenStreetMap';
  return false;
}

search.errorHandler = function(jqXHR, textStatus, errorThrown) {
  $("#leftsearch .loader").removeClass('on');
  search.content.innerHTML = 'Ошибка: ' + textStatus + '<br />' + errorThrown.message;
};

search.search = function(inQuery) {
  inQuery = inQuery || osm.input.value;
  osm.input.value = inQuery;
  if (inQuery.length < 1)
    return false;
  $("#leftsearch .loader").addClass('on');
  mapCenter=osm.map.getCenter();
  osm.leftpan.toggle('leftsearch');
  $.getJSON('/api/search', {q: inQuery, accuracy: 1, lat: mapCenter.lat, lon: mapCenter.lng}, search.processResults)
  .error(search.errorHandler);
  return false;
};

search.inLoad = function() {
  var query = get['q'] || '';
  if (query != '')
    search.search(query);
};

search.Icon =  L.Icon.extend({
  options: {
    iconUrl: 'img/marker-addr.png',
    iconSize: new L.Point(31, 31),
    shadowSize: new L.Point(0, 0),
    iconAnchor: new L.Point(16, 30),
    popupAnchor: new L.Point(0, -11)
  }
});


@@ -1192,16 +1192,18 @@
 ntent = 
+$(
 '%3Cul id=
@@ -1222,17 +1222,17 @@
 esult%22%3E'
-;
+)
 %0A      o
@@ -1340,24 +1340,28 @@
 ) %7B%0A        
+var 
 zoom = match
@@ -1411,258 +1411,8 @@
 +4;%0A
-        content += ('%3Cli%3E%3Ca href=%22%22 onClick=%22osm.map.setView(new L.LatLng(' + matches%5Bi%5D.lat + ',' + matches%5Bi%5D.lon + '), '+zoom+'); return false;%22 info=%22id='+matches%5Bi%5D.id+'  weight='+matches%5Bi%5D.weight+'%22%3E' + matches%5Bi%5D.display_name + '%3C/a%3E%3C/li%3E');%0A
     
@@ -1729,80 +1729,185 @@
     
-osm.layers.search_marker.addLayer(marker);%0A      %7D%0A      zoom = 
+var a = $('%3Ca href=%22%22%3E');%0A        a.attr('search_id',matches%5Bi%5D.id);%0A        a.text(matches%5Bi%5D.display_name);%0A        a.bind(%22click%22, %7B%0A            center: new L.LatLng(
 matches%5B
 0%5D.t
@@ -1906,31 +1906,24 @@
 hes%5B
-0%5D.this_poi?14:
+i%5D.lat, 
 matches%5B
 0%5D.a
@@ -1922,103 +1922,227 @@
 hes%5B
-0%5D.addr_type_id*2+4%0A      osm.map.setView(new L.LatLng(matches%5B0%5D.lat, matches%5B0%5D.lon), zoom
+i%5D.lon),%0A            zoom: zoom,%0A            marker: marker%0A            %7D, function (e)%7B%0A          osm.map.setView(e.data.center, e.data.zoom);%0A          e.data.marker.openPopup();%0A          return false;%0A        %7D
 );%0A
+  
     
@@ -2154,61 +2154,223 @@
 tent
- += '%3C/ul%3E';%0A      search.content.innerHTML = content
+.append(%0A          $('%3Cli%3E').append(a)%0A        );%0A        osm.layers.search_marker.addLayer(marker);%0A      %7D%0A      $(search.content).empty().append(content);%0A      $('#ol-search_result a', search.content)%5B0%5D.click()
 ;%0A  

fix #127 - open active popup in search