document.addEventListener("DOMContentLoaded",function onDOMContentLoaded() {
"use strict";
const Cc = Components.classes;
const Ci = Components.interfaces;
const ObserverService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
const WindowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
const WindowWatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher);
const BrowserSearchService = Cc["@mozilla.org/browser/search-service;1"].getService(Ci.nsIBrowserSearchService);
const tickIconSrc = "/icons/tick.png";
var moduleObj = window.arguments[0];
var api = moduleObj.api;
var storeBranch = api.Settings.getComponentBranchPath() + "searchName";
var gBrowser = WindowMediator.getMostRecentWindow("navigator:browser").gBrowser;
var tree = document.getElementById("qsTree");
var labelFounded = document.getElementById("qs-link-add-founded");
var labelSearch = document.getElementById("qs-link-add-search");
var labelSettings = document.getElementById("qs-link-settings");
window.saveEngineChoice = function saveEngineChoice() {
var tRows = document.querySelectorAll("#qsTree treerow");
var selectedEngineName;
var currentEngineName = moduleObj.currentEngineName;
for (let i = 0;i < tRows.length;i++) {
if (tRows[i].firstChild.getAttribute("src") === tickIconSrc)
{
selectedEngineName = tRows[i].lastChild.getAttribute("label");
break;
}

}

if (selectedEngineName !== currentEngineName)
{
moduleObj.currentEngineName = selectedEngineName;
moduleObj.acceptedDefaultEngineName = "";
ObserverService.notifyObservers({
"name": selectedEngineName},"yandex-omnibox-search-modified",false);
}

window.close();
}
;
var updateList = function updateList() {
tree.removeChild(tree.childNodes[1]);
var container = tree.appendChild(document.createElement("treechildren"));
var engines = moduleObj.getVisibleEngines();
var enginesNames = [];
var currentEngineName = moduleObj.currentEngineName;
engines.forEach(function (item) {
var tItem = document.createElement("treeitem");
var tRow = document.createElement("treerow");
var tCell1 = document.createElement("treecell");
var tCell2 = document.createElement("treecell");
tCell2.setAttribute("label",item.name);
if (item.name === currentEngineName)
{
tCell1.setAttribute("src",tickIconSrc);
}

tRow.appendChild(tCell1);
tRow.appendChild(tCell2);
tItem.appendChild(tRow);
container.appendChild(tItem);
enginesNames.push(item.name);
}
);
container.onclick = function (e) {
var n = tree.currentIndex;
var targetNode = tree.childNodes[1].childNodes[n].childNodes[0].childNodes[0];
if (targetNode && targetNode.getAttribute("src") !== tickIconSrc)
{
Array.prototype.forEach.call(container.querySelectorAll("treerow"),function (tRow) {
tRow.firstChild.removeAttribute("src");
}
);
targetNode.setAttribute("src",tickIconSrc);
}

}
;
var newSearchFound = null;
var openSearches = WindowMediator.getMostRecentWindow("navigator:browser").gBrowser.contentDocument.querySelectorAll("link[rel='search'][type='application/opensearchdescription+xml']");
for (let i = 0;i < openSearches.length;i++) {
if (enginesNames.indexOf(openSearches[i].title) === - 1)
{
newSearchFound = {
"title": openSearches[i].title,
"uri": openSearches[i].href,
"icon": ""};
break;
}

}

labelFounded.hidden = newSearchFound === null;
if (newSearchFound !== null)
{
labelFounded.setAttribute("value",labelFounded.getAttribute("data-prefix") + " " + newSearchFound.title);
labelFounded.onclick = function (e) {
var type = Ci.nsISearchEngine.DATA_XML;
BrowserSearchService.addEngine(newSearchFound.uri,type,newSearchFound.icon,false);
}
;
}

}
;
updateList();
labelSearch.onclick = function labelSearchOnclick(e) {
gBrowser.selectedTab = gBrowser.addTab("https://addons.mozilla.org/firefox/search-tools");
window.close();
}
;
labelSettings.onclick = function labelSettingsOnclick(e) {
window.addEventListener("unload",function openEngineManager() {
window.removeEventListener("unload",openEngineManager,false);
WindowWatcher.openWindow(WindowMediator.getMostRecentWindow("navigator:browser"),"chrome://browser/content/search/engineManager.xul","_blank","chrome,dialog,modal,centerscreen,resizable",undefined);
}
,false);
window.close();
}
;
var changeCurrentEngineListener = {
observe: function changeCurrentEngineListener_observe(aEngine, aTopic, aVerb) {
if (aTopic === "browser-search-engine-modified" && aVerb === "engine-current")
{
updateList();
}

}
};
ObserverService.addObserver(changeCurrentEngineListener,"yandex-omnibox-search-modified",false);
ObserverService.addObserver(changeCurrentEngineListener,"browser-search-engine-modified",false);
window.addEventListener("unload",function cleanup() {
window.removeEventListener("unload",cleanup,false);
ObserverService.removeObserver(changeCurrentEngineListener,"yandex-omnibox-search-modified");
ObserverService.removeObserver(changeCurrentEngineListener,"browser-search-engine-modified");
}
,false);
}
,false);
