"use strict";

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

const TITLE = "Replace Bookmark",
      URL_NOT_SUPPORTED = "Sorry, the current page's URL is not supported.",
      RELATED_NOT_FOUND = "Sorry, no related bookmarks found.",
      ALREADY_BOOKMARKED = "The current page is already bookmarked.",
      SELECT_BOOKMARK = "Which bookmark do you want to replace?";

const PREFS_BRANCH = Services.prefs.getBranch("extensions.bmreplace."),
      POS_PREF_NAME = "button-position",
      BUTTON_ID = "bmreplace-button";

let main = {
  action: function() {
    let window = Services.wm.getMostRecentWindow("navigator:browser");
    let doc = window.content.document;
    let url = doc.location.toString();
    if (!bm.DOMAIN_REGEX.test(url)) {
      prompts.alert(window, TITLE, URL_NOT_SUPPORTED);
      return;
    }
    if (bm.isBookmarked(url)) {
      prompts.alert(window, TITLE, ALREADY_BOOKMARKED);
      return;
    }
    let bookmarks = bm.getRelatedBookmarks(url);
    if (!bookmarks.length) {
      prompts.alert(window, TITLE, RELATED_NOT_FOUND);
      return;
    }
    let titles = [b.title for each (b in bookmarks)];
    let selected = {};
    let ok = prompts.select(window, TITLE, SELECT_BOOKMARK, titles.length,
                            titles, selected);
    if (ok) {
      let bookmark = bookmarks[selected.value];
      bm.replaceBookmark(bookmark.id, url, doc.title);
    }
  },
  
  /*
   * @return {toolbarId, nextItemId}
   */
  getPrefs: function() {
    try {
      let values = PREFS_BRANCH.getCharPref(POS_PREF_NAME).split(",");
      return {
        toolbarId: values[0],
        nextItemId: values[1]
      };
    } catch(e) {
      return {
        // TODO
      };
    }
  },
  
  setPrefs: function(toolbarId, nextItemId) {
    let value = "";
    toolbarId && (value += toolbarId);
    nextItemId && (value += "," + nextItemId);
    PREFS_BRANCH.setCharPref(POS_PREF_NAME, value);
  }
};


@@ -463,51 +463,85 @@
 ace.
-%22),%0D%0A      POS_
+button-position.%22),%0D%0A      PREF_TB = %22toolbar%22,%0D%0A      
 PREF_N
-AME
+EXT
  = %22
-button-position
+next-item
 %22,%0D%0A
@@ -1596,20 +1596,36 @@
     
-let values =
+return %7B%0D%0A        toolbarId:
  PRE
@@ -1651,351 +1651,344 @@
 ef(P
-OS_PREF_NAME).split(%22,%22);%0D%0A      return %7B%0D%0A        toolbarId: values%5B0%5D,%0D%0A        nextItemId: values%5B1%5D%0D%0A      %7D;%0D%0A    %7D catch(e) %7B%0D%0A      return %7B%0D%0A        // TODO%0D%0A      %7D;%0D%0A    %7D%0D%0A  %7D,%0D%0A  %0D%0A  setPrefs: function(toolbarId, nextItemId) %7B%0D%0A    let value = %22%22;%0D%0A    toolbarId && (value += toolbarId);%0D%0A    nextItemId && (value += %22,%22 + nextItem
+REF_TB),%0D%0A        nextItemId: PREFS_BRANCH.getCharPref(PREF_NEXT)%0D%0A      %7D;%0D%0A    %7D catch(e) %7B%0D%0A      return %7B // default position%0D%0A        toolbarId: %22nav-bar%22,%0D%0A        nextItemId: %22bookmarks-menu-button-container%22%0D%0A      %7D;%0D%0A    %7D%0D%0A  %7D,%0D%0A  %0D%0A  setPrefs: function(toolbarId, nextItemId) %7B%0D%0A    PREFS_BRANCH.setCharPref(PREF_TB, toolbar
 Id);
@@ -2022,28 +2022,29 @@
 ref(
-POS_
 PREF_N
-AME, value
+EXT, nextItemId
 );%0D%0A

Add default position; separate pref in two