"use strict";

let prompts = Services.prompt,
    prefs = Services.prefs,
    ww = Services.ww;

const PREFS_BRANCH = Services.prefs.getBranch("extensions.bmreplace."),
      PREF_VERSION = "version",
      BUTTON_ID = "bmreplace-button",
      KEYSET_ID = "bmreplace-keyset";

let main = {
  action: function() {
    let window = Services.wm.getMostRecentWindow("navigator:browser"),
        doc = window.content.document,
        url = doc.location.toString(),
        title = _("label");
    
    if (!bm.DOMAIN_REGEX.test(url)) {
      prompts.alert(window, title, _("urlNotSupported"));
      return;
    }
    if (bm.isBookmarked(url)) {
      prompts.alert(window, title, _("alreadyBookmarked"));
      return;
    }
    let bookmarks = bm.getRelatedBookmarks(url);
    if (!bookmarks.length) {
      let btn = prompts.confirmEx(window, title, _("relatedNotFound"),
                                  prompts.STD_YES_NO_BUTTONS +
                                  prompts.BUTTON_POS_1_DEFAULT,
                                  "", "", null, null, {});
      if (btn == 0) {
        bm.showAddBookmark(url, doc.title);
      }
      return;
    }
    let titles = [b.title for each (b in bookmarks)],
        states = [b.checked for each (b in bookmarks)],
        result = {},
        ok = main.select(window, title, _("selectBookmark"), titles.length,
                         titles, states, result);
    if (ok) {
      let checked = result.checked,
          idx = result.value,
          bookmark = bookmarks[idx];
      if (checked != states[idx]) {
        bm.setKeepTitle(bookmark.id, checked);
      }
      bm.replaceBookmark(bookmark.id, url, !checked && doc.title);
    } else if (result.addNew) {
      bm.showAddBookmark(url, doc.title);
    }
  },

  select: function(window, title, text, count, options, states, result) {
    function modifySelect(subject, topic) {
      if (topic == "domwindowopened") {
        ww.unregisterNotification(modifySelect);
        
        runOnLoad(subject, function(window) {
          let doc = window.document,
              cb = doc.createElement("checkbox"),
              list = $(doc, "list"),
              accept = doc.documentElement.getButton("accept"),
              extra2 = doc.documentElement.getButton("extra2");
          
          cb.setAttribute("label", _("keepOldTitle"));
          list.parentNode.appendChild(cb);
          extra2.hidden = false;
          extra2.label = _("newBookmark");
          extra2.parentNode.querySelector("spacer").hidden = false;
          list.setAttribute("rows", 7);
          doc.querySelector("vbox").style.width = "26em";
          window.sizeToContent();
          
          list.addEventListener("select", function() {
            cb.checked = states[list.selectedIndex];
          }, false);
          
          accept.addEventListener("command", function() {
            result.checked = cb.checked;
          }, false);
          
          extra2.addEventListener("command", function() {
            result.addNew = true;
            window.close();
          }, false);
        }, "");
      }
    }
    
    ww.registerNotification(modifySelect);
    
    return prompts.select(window, title, text, count, options, result);
  },
  
  getLastVersion: function() {
    try {
      return PREFS_BRANCH.getCharPref(PREF_VERSION);
    } catch(e) {
      return null;
    }
  },
  
  setLastVersion: function(version) {
    PREFS_BRANCH.setCharPref(PREF_VERSION, version);
  }
};


@@ -2210,24 +2210,128 @@
 , %22list%22),%0D%0A
+              hbox = doc.createElement(%22hbox%22),%0D%0A              resizer = doc.createElement(%22resizer%22),%0D%0A
             
@@ -2512,24 +2512,278 @@
 dTitle%22));%0D%0A
+          cb.setAttribute(%22flex%22, %221%22);%0D%0A          resizer.setAttribute(%22dir%22, %22right%22);%0D%0A          resizer.style.marginBottom = %223px%22;%0D%0A          resizer.style.marginRight = %226px%22;%0D%0A          hbox.appendChild(cb);%0D%0A          hbox.appendChild(resizer);%0D%0A
           li
@@ -2800,34 +2800,36 @@
 ode.appendChild(
-cb
+hbox
 );%0D%0A          ex

Add resizer