3 OSM.NewNote = function (map) {
 
   4   var noteLayer = map.noteLayer,
 
   5       content = $("#sidebar_content"),
 
   7       addNoteButton = $(".control-note .control-button"),
 
  13       iconUrl: OSM.NEW_NOTE_MARKER,
 
  18       iconUrl: OSM.OPEN_NOTE_MARKER,
 
  23       iconUrl: OSM.CLOSED_NOTE_MARKER,
 
  29   addNoteButton.on("click", function (e) {
 
  33     if ($(this).hasClass("disabled")) return;
 
  35     OSM.router.route("/note/new");
 
  38   function createNote(marker, form, url) {
 
  39     var location = marker.getLatLng().wrap();
 
  41     marker.options.draggable = false;
 
  42     marker.dragging.disable();
 
  44     $(form).find("input[type=submit]").prop("disabled", true);
 
  53         text: $(form.text).val()
 
  55       success: function (feature) {
 
  56         noteCreated(feature, marker);
 
  60     function noteCreated(feature, marker) {
 
  61       content.find("textarea").val("");
 
  62       updateMarker(feature);
 
  64       noteLayer.removeLayer(marker);
 
  65       addNoteButton.removeClass("active");
 
  66       OSM.router.route("/note/" + feature.properties.id);
 
  70   function updateMarker(feature) {
 
  71     var marker = L.marker(feature.geometry.coordinates.reverse(), {
 
  72       icon: noteIcons[feature.properties.status],
 
  76     marker.id = feature.properties.id;
 
  77     marker.addTo(noteLayer);
 
  81   page.pushstate = page.popstate = function (path) {
 
  82     OSM.loadSidebarContent(path, function () {
 
  87   function newHalo(loc, a) {
 
  88     var hasHalo = halo && map.hasLayer(halo);
 
  90     if (a === "dragstart" && hasHalo) {
 
  91       map.removeLayer(halo);
 
  93       if (hasHalo) map.removeLayer(halo);
 
  95       halo = L.circleMarker(loc, {
 
 106   page.load = function (path) {
 
 107     if (addNoteButton.hasClass("disabled")) return;
 
 108     if (addNoteButton.hasClass("active")) return;
 
 110     addNoteButton.addClass("active");
 
 112     map.addLayer(noteLayer);
 
 114     var params = Qs.parse(path.substring(path.indexOf("?") + 1));
 
 117     if (params.lat && params.lon) {
 
 118       markerLatlng = L.latLng(params.lat, params.lon);
 
 120       markerLatlng = map.getCenter();
 
 123     map.panInside(markerLatlng, {
 
 127     newNote = L.marker(markerLatlng, {
 
 133     newNote.on("dragstart dragend", function (a) {
 
 134       newHalo(newNote.getLatLng(), a.type);
 
 137     newNote.addTo(noteLayer);
 
 138     newHalo(newNote.getLatLng());
 
 140     newNote.on("remove", function () {
 
 141       addNoteButton.removeClass("active");
 
 142     }).on("dragstart", function () {
 
 143       $(newNote).stopTime("removenote");
 
 144     }).on("dragend", function () {
 
 145       content.find("textarea").focus();
 
 148     content.find("textarea")
 
 149       .on("input", disableWhenBlank)
 
 152     function disableWhenBlank(e) {
 
 153       $(e.target.form.add).prop("disabled", $(e.target).val() === "");
 
 156     content.find("input[type=submit]").on("click", function (e) {
 
 158       createNote(newNote, e.target.form, "/api/0.6/notes.json");
 
 161     return map.getState();
 
 164   page.unload = function () {
 
 165     if (newNote) noteLayer.removeLayer(newNote);
 
 166     if (halo) map.removeLayer(halo);
 
 167     addNoteButton.removeClass("active");