1 OSM.NewNote = function(map) {
 
   2   var noteLayer = map.noteLayer,
 
   3     content = $('#sidebar_content'),
 
   5     addNoteButton = $(".control-note .control-button"),
 
  11       iconUrl: OSM.NEW_NOTE_MARKER,
 
  16       iconUrl: OSM.OPEN_NOTE_MARKER,
 
  21       iconUrl: OSM.CLOSED_NOTE_MARKER,
 
  27   addNoteButton.on("click", function (e) {
 
  31     if ($(this).hasClass('disabled')) return;
 
  33     OSM.router.route('/note/new');
 
  36   function createNote(marker, form, url) {
 
  37     var location = marker.getLatLng().wrap();
 
  39     marker.options.draggable = false;
 
  40     marker.dragging.disable();
 
  42     $(form).find("input[type=submit]").prop("disabled", true);
 
  51         text: $(form.text).val()
 
  53       success: function (feature) {
 
  54         noteCreated(feature, marker);
 
  58     function noteCreated(feature, marker) {
 
  59       content.find("textarea").val("");
 
  60       updateMarker(feature);
 
  62       noteLayer.removeLayer(marker);
 
  63       addNoteButton.removeClass("active");
 
  64       OSM.router.route('/note/' + feature.properties.id);
 
  68   function updateMarker(feature) {
 
  69     var marker = L.marker(feature.geometry.coordinates.reverse(), {
 
  70       icon: noteIcons[feature.properties.status],
 
  74     marker.id = feature.properties.id;
 
  75     marker.addTo(noteLayer);
 
  79   page.pushstate = page.popstate = function (path) {
 
  80     OSM.loadSidebarContent(path, function () {
 
  85   function newHalo(loc, a) {
 
  86     if (a === 'dragstart' && map.hasLayer(halo)) {
 
  87       map.removeLayer(halo);
 
  89       if (map.hasLayer(halo)) map.removeLayer(halo);
 
  91       halo = L.circleMarker(loc, {
 
 102   page.load = function (path) {
 
 103     if (addNoteButton.hasClass("disabled")) return;
 
 104     if (addNoteButton.hasClass("active")) return;
 
 106     addNoteButton.addClass("active");
 
 108     map.addLayer(noteLayer);
 
 110     var params = querystring.parse(path.substring(path.indexOf('?') + 1));
 
 113     if (params.lat && params.lon) {
 
 114       markerLatlng = L.latLng(params.lat, params.lon);
 
 116       var markerPosition = map.latLngToContainerPoint(markerLatlng),
 
 117           mapSize = map.getSize(),
 
 118           panBy = L.point(0, 0);
 
 120       if (markerPosition.x < 50) {
 
 121         panBy.x = markerPosition.x - 50;
 
 122       } else if (markerPosition.x > mapSize.x - 50) {
 
 123         panBy.x = 50 - mapSize.x + markerPosition.x;
 
 126       if (markerPosition.y < 50) {
 
 127         panBy.y = markerPosition.y - 50;
 
 128       } else if (markerPosition.y > mapSize.y - 50) {
 
 129         panBy.y = 50 - mapSize.y + markerPosition.y;
 
 134       markerLatlng = map.getCenter();
 
 137     newNote = L.marker(markerLatlng, {
 
 138       icon: noteIcons["new"],
 
 143     newNote.on("dragstart dragend", function(a) {
 
 144       newHalo(newNote.getLatLng(), a.type);
 
 147     newNote.addTo(noteLayer);
 
 148     newHalo(newNote.getLatLng());
 
 150     newNote.on("remove", function () {
 
 151       addNoteButton.removeClass("active");
 
 152     }).on("dragstart",function () {
 
 153       $(newNote).stopTime("removenote");
 
 154     }).on("dragend", function () {
 
 155       content.find("textarea").focus();
 
 158     content.find("textarea")
 
 159       .on("input", disableWhenBlank)
 
 162     function disableWhenBlank(e) {
 
 163       $(e.target.form.add).prop("disabled", $(e.target).val() === "");
 
 166     content.find('input[type=submit]').on('click', function (e) {
 
 168       createNote(newNote, e.target.form, '/api/0.6/notes.json');
 
 171     return map.getState();
 
 174   page.unload = function () {
 
 175     noteLayer.removeLayer(newNote);
 
 176     map.removeLayer(halo);
 
 177     addNoteButton.removeClass("active");