<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin/"?>
<?xml-stylesheet type="text/css" href="chrome://kmprefs/skin/"?>
<!DOCTYPE page SYSTEM "chrome://kmprefs/locale/pref-webfind.dtd">
<page xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="prefWebfind" onload="init()" onunload="Search.exit()">
	<stringbundle id="pref_bundle" src="chrome://kmprefs/locale/pref.properties"/>
	<stringbundle id="xml_bundle" src="chrome://kmprefs/locale/kmprefs/xml.properties"/>
	<script type="application/x-javascript" src="chrome://kmprefs/content/pref.js"/>
	<script type="application/x-javascript" src="chrome://kmprefs/content/kmprefs/xml.js"/>
	<script type="application/x-javascript" src="chrome://kmprefs/content/kplugins/jsbridge.js"/>
	<script type="application/x-javascript">
<![CDATA[
var menu;
CFGtoXML.init("search");
var Engines = new XML("file:///" + getFile(getFolder("KUserSettings"), "search.xml").path);
function init(e) {
	menu = document.createElement("menupopup");
	
	this.searchService = Components.classes["@mozilla.org/browser/search-service;1"]
        .getService(Components.interfaces.nsIBrowserSearchService);
  
  for each (engine in this.searchService.getEngines()) {
  
    if (engine.hidden) continue;
    var submission = engine.getSubmission("", null, "homepage");
    if (!submission) continue;
    var searchURL = submission.uri ? submission.uri.spec : "";
    if (searchURL == "") continue;
        
    item = document.createElement("menuitem");
		item.setAttribute("value", searchURL);
		item.setAttribute("label", engine.name);
		menu.appendChild(item);
  }
	/*for (var j = 0, engines = Engines.data.getElementsByTagName("engine"), item; j < engines.length; j++) {
		item = document.createElement("menuitem");
		item.setAttribute("value", engines[j].getAttribute("url"));
		item.setAttribute("label", engines[j].getAttribute("caption"));
		menu.appendChild(item);
	}*/
	URLBar.init();
	if (kPlugin.load("macros") && kMacrosModule.load("search")) {
		if (JSB.load) {
			document.getElementById("JSBridgeOff").setAttribute("hidden", true);
			document.getElementById("JSBridgeOn").removeAttribute("hidden");
		}
	} else {
		var elem = document.getElementsByTagName("tab");
		elem[1].parentNode.removeChild(elem[1]);
		elem[1].parentNode.removeChild(elem[1]);
		elem = document.getElementsByTagName("tabpanel");
		elem[1].parentNode.removeChild(elem[1]);
		elem[1].parentNode.removeChild(elem[1]);
	}
}
var URLBar = {
	init : function () {
		for (var j = 0, box = document.getElementsByTagName("checkbox"); j < box.length; j++)
			initCheckbox(box[j].id);
		document.getElementById("browser.urlbar.autoFill").disabled = !document.getElementById("browser.urlbar.autocomplete.enabled").checked;
		
		try {
			for (var box = document.getElementsByTagName("textbox"), j = box.length - 1; j > -1; j--)
				initTextbox(box[j].id, false);
		} catch (e) {}
		if (!kPlugin.load("macros") || !kMacrosModule.load("domcomplete")) {
			var elem = document.getElementById("domComplete");
			elem.parentNode.removeChild(elem);
		}
		
		this.initKeyword();
		this.initKeywordMenu();
	},
	initKeyword : function () {
		var val = document.getElementById("keyword.enabled").checked;
		
		document.getElementById("browser.fixup.alternate.enabled").disabled = val;
		document.getElementById("browser.fixup.alternate.prefix").disabled = val;
		document.getElementById("browser.fixup.alternate.suffix").disabled = val;
	},
	initKeywordMenu : function () {
		/*var elem = document.getElementById("keyword.URL");
		
		if (elem.hasChildNodes())
			elem.removeChild(elem.firstChild);
		elem.appendChild(menu.cloneNode(true));
		initMenulist("keyword.URL", "", false);*/
	}
};
var Search = {
	_base : "kmeleon.plugins.macros.search.",
	_dflt : "kmeleon.general.searchEngine",
	_elem : ["engine", "meta"],
	_menu : ["_Search_Engines_Configurable", "_Search_Engines_Metasearch"],
	_cmnd : ["Search_Engine", "Search_Metasearch"],
	_labl : ["", "&Metasearch"],
	done : [false, false],
	init : function (n) {
		if (!this.done[n]) {
			var j = 0;
			var prefID = this._base + this._elem[n] + j + ".url";
			
			var box = document.getElementById(this._elem[n]),
			val = getPrefValue(prefID);
			
			pref.setIntPref(this._base + this._elem[n] + ".max", 0);
			while (box.hasChildNodes())
				box.removeChild(box.firstChild);
			while (val != null) {
				if (val) {
					this._addMenu(n, prefID);
					initMenulist(prefID, "", false);
				}
				j++;
				prefID = this._base + this._elem[n] + j + ".url";
				val = getPrefValue(prefID);
			}
			this.done[n] = true;
		}
	},
	exit : function () {
		for (var j = 0; j < this._elem.length; j++)
			restoreDefault(this._base + this._elem[j] + ".max");
		//Engines.save(getFile(getFolder("KUserSettings"), "search.xml"), ["caption", "url"]);
	},
	add : function (n) {
		this._addMenu(n, this._base + this._elem[n] + document.getElementById(this._elem[n]).getElementsByTagName("menulist").length + ".url");
	},
	_addMenu : function (n, elemID) {
		var elem = document.createElement("menulist"),
		prnt = document.getElementById(this._elem[n]);
		
		elem.id = elemID;
		elem.setAttribute("flex", 1); // omitting this slows rendering!
		elem.setAttribute("oncommand", "Search.setEngine(" + n + ",this)");
		elem.appendChild(menu.cloneNode(true));
		prnt.appendChild(elem);
		
		prnt.lastChild.firstChild.insertBefore(document.createElement("menuseparator"), prnt.lastChild.firstChild.firstChild);
		elem = document.createElement("menuitem");
		elem.setAttribute("value", "");
		elem.setAttribute("label", document.getElementById("pref_bundle").getString("remove_entry"));
		elem.setAttribute("style", "font-weight:bold");
		prnt.lastChild.firstChild.insertBefore(elem, prnt.lastChild.firstChild.firstChild);
		prnt.lastChild.selectedIndex = 0;
		pref.setIntPref(this._base + this._elem[n] + ".max", pref.getIntPref(this._base + this._elem[n] + ".max") + 1);
	},
	setEngine : function (n, elem) {
		var val = elem.value,
		lbl = (val) ? elem.selectedItem.label : "";
		
		if (!n) {
			this._delMenu();
			this._setDefault(elem, val, lbl);
			setCharPref(elem.id.substr(0, elem.id.lastIndexOf(".")) + ".name", lbl);
		}
		setCharPref(elem.id, val);
		if (!val) {
			this._sanitize(this._elem[n]);
			this.done[n] = false;
			this.init(n);
		}
		this._setMenu(n);
	},
	_setDefault : function (elem, value, label) {
		if (getPrefValue(elem.id) == getPrefValue(this._dflt))
			if (value) {
				setCharPref(this._dflt, value);
				setCharPref(this._dflt + "Name", label);
			} else
				pref.setBoolPref(this._base + "locked", false);
	},
	_delMenu : function () {
		for (var j = 0; j < pref.getIntPref(this._base + this._elem[0] + ".max"); j++)
			JSB.setmenu(this._menu[0], "macro", "", this._cmnd[0] + "(" + j + ")", "");
	},
	_setMenu : function (n) {
		var max = pref.getIntPref(this._base + this._elem[n] + ".max");
		
		if (n)
			JSB.setmenu(this._menu[n], "macro", this._labl[n], (max) ? this._cmnd[n] : "", "");
		else
			for (var j = 0; j < max; j++)
				JSB.setmenu(this._menu[n], "macro", getPrefValue(this._base + this._elem[0] + j + ".name"), this._cmnd[n] + "(" + j + ")", "");
		//   JSB.rebuildmenu(this._menu[n]);
	},
	_sanitize : function (prefPart) {
		var max = pref.getIntPref(this._base + prefPart + ".max");
		
		for (var j = 0; j < max; j++)
			if (getPrefValue(this._base + prefPart + j + ".url") == "") {
				for (var i = j + 1; i < max; i++) {
					setCharPref(this._base + prefPart + (i - 1) + ".url", getPrefValue(this._base + prefPart + i + ".url"));
					if (prefPart == "engine")
						setCharPref(this._base + prefPart + (i - 1) + ".name", getPrefValue(this._base + prefPart + i + ".name"));
				}
				setCharPref(this._base + prefPart + (max - 1) + ".url", "");
				if (prefPart == "engine")
					setCharPref(this._base + prefPart + (max - 1) + ".name", "");
			}
		for (var j = 0, url; j < max; j++) {
			url = this._base + prefPart + j + ".url";
			if (getPrefValue(url))
				continue;
			restoreDefault(url);
			if (getPrefValue(url) == null) {
				if (prefPart == "engine")
					restoreDefault(this._base + prefPart + j + ".name");
			} else
				setCharPref(url, "");
		}
	}
};
var Database = {
	_add : false,
	_ndx : false,
	_cap : null,
	_url : null,
	
	done : false,
	tree : null,
	searchService : null,
	init : function () {
     this.searchService = Components.classes["@mozilla.org/browser/search-service;1"]
        .getService(Components.interfaces.nsIBrowserSearchService);
    
 		if (!this.done) {
			this.done = true;
			this.tree = document.getElementById("database");
			while (this.tree.hasChildNodes())
				this.tree.removeChild(this.tree.firstChild);
		//for (var j = 0, engines = Engines.data.getElementsByTagName("engine"), titem, tcell; j < engines.length; j++) {

		  for each (engine in this.searchService.getEngines()) {
		    if (engine.hidden) continue;
			  var submission = engine.getSubmission("", null, "homepage");
			  if (!submission) continue;
        var searchURL = submission.uri ? submission.uri.spec : "";
        if (searchURL == "") continue;

				titem = document.createElement("treeitem");
				titem.appendChild(document.createElement("treerow"));
				tcell = document.createElement("treecell");
				tcell.setAttribute("label", engine.name);
				titem.firstChild.appendChild(tcell);
				tcell = document.createElement("treecell");
				tcell.setAttribute("label", searchURL);
				titem.firstChild.appendChild(tcell);
				this.tree.appendChild(titem);
			}
			this.tree.parentNode.view.selection.select(0);
		}
	},
	del : function () {
		this._ndx = this.tree.parentNode.currentIndex;
		if (this._ndx > -1) {
      var caption = this.tree.childNodes[this._ndx].firstChild.firstChild.getAttribute("label");
      this.tree.removeChild(this.tree.childNodes[this._ndx]);		
		  //var _default = this.tree.childNodes[this._ndx].getAttribute("default");
      var engine = this.searchService.getEngineByName(caption);
			if (engine) this.searchService.removeEngine(engine);
     // Engines.set("engine", this._ndx, ["url"], [''], 0);
			this._refresh();
		}
	},
	add : function () {
		this._ndx = -1;
		this._add = true;
		this._cap = null;
		this._url = null;
		window.openDialog("chrome://kmprefs/content/kmprefs/dlg-webfind.xul", "engnAdd", "chrome,modal=yes,resizable=no", this);
	},
	edit : function () {
		this._ndx = this.tree.parentNode.currentIndex;
		this._add = false;
		this._cap = this.tree.childNodes[this._ndx].firstChild.firstChild.getAttribute("label");
		this._url = this.tree.childNodes[this._ndx].firstChild.firstChild.nextSibling.getAttribute("label");
		window.openDialog("chrome://kmprefs/content/kmprefs/dlg-webfind.xul", "engnEdit", "chrome,modal=yes,resizable=no", this);
	},
	select : function () {
		var ndx = this.tree.parentNode.currentIndex;
		document.getElementById("edit").setAttribute("disabled", (ndx == -1));
		document.getElementById("delete").setAttribute("disabled", (ndx == -1));
	},
	set : function (caption, url) {
		if ((this._cap != caption) || (this._url != url)) {
			//Engines.set("engine", this._ndx, ["caption", "url"], [caption, url], 0);
			var engine = this.searchService.getEngineByName(this._cap);
			if (engine) this.searchService.removeEngine(engine);
      this.searchService.addEngineWithDetails(caption, null, '', '', 'get', url);
			this._refresh();
			this.done = false;
			this.init();
		}
	},
	_refresh : function () {
		init(false);
		for (var j = 0; j < Search.done.length; j++)
			Search.done[j] = false;
	}
};
var SearchHow = {
	init : function () {
		var search_button = "kmeleon.plugins.macros.searchtyped.openurl",
		sb_list = document.getElementById(search_button);
		for (var a in sb_list.firstChild.childNodes)
			sb_list.firstChild.childNodes[a].value == getPrefValue(search_button) ? sb_list.selectedIndex = a : 0;
		var search_selected = "kmeleon.plugins.macros.selected.openurl",
		ss_list = document.getElementById(search_selected);
		for (var a in ss_list.firstChild.childNodes)
			ss_list.firstChild.childNodes[a].value == getPrefValue(search_selected) ? ss_list.selectedIndex = a : 0;
	}
};
]]>
	</script>
	<keyset>
		<key id="addKey" keycode="VK_INSERT" oncommand="Database.add()"/>
		<key id="delKey" keycode="VK_DELETE" oncommand="Database.del()"/>
	</keyset>
	<vbox flex="1" style="overflow:auto">
		<dialogheader title="&webfind.header;"/>
		<separator class="thin"/>
		<tabcontrol flex="1">
			<tabbox flex="1">
				<tabs>
					<tab label="&webfind.urlbar;"/>
					<tab label="&webfind.websearch;" oncommand="Search.init(0)" onfocus="Search.init(0)"/>
					<tab label="&webfind.metasearch;" oncommand="Search.init(1)" onfocus="Search.init(1)"/>
					<tab label="&webfind.database;" oncommand="Database.init()" onfocus="Database.init()"/>
					<!--        <tab label="&webfind.searchbutton;" oncommand="SearchHow.init()" onfocus="SearchHow.init()"/>	-->
				</tabs>
				<tabpanels flex="1">
					<tabpanel flex="1" orient="vertical" style="overflow:auto">
						<!-- URL Bar -->
						<groupbox>
							<caption>
								<label>&autocomplete.label;</label>
							</caption>
							<checkbox oncommand="toggleCheckbox(this.id);this.nextSibling.disabled=!this.checked" id="browser.urlbar.autocomplete.enabled" label="&autocomplete.enable;"/>
							<checkbox oncommand="toggleCheckbox(this.id)" id="browser.urlbar.autoFill" label="&autofill.enable;" style="margin-left:2em"/>
						</groupbox>
						<groupbox id="domComplete">
							<caption>
								<label>&domCompletion.label;</label>
							</caption>
							<separator class="thin"/>
							<grid>
								<columns>
									<column style="padding-right:1em"/>
									<column flex="1"/>
									<column flex="1"/>
								</columns>
								<rows>
									<row align="center">
										<label value="&whenPressing;"/>
										<label value="&domCompletion.prefix;"/>
										<label value="&domCompletion.suffix;"/>
									</row>
									<row>
										<separator class="thin"/>
									</row>
									<row align="center">
										<label value="&domCompletion.key0;"/>
										<textbox id="kmeleon.plugins.macros.domComplete0.prefix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
										<textbox id="kmeleon.plugins.macros.domComplete0.suffix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
									</row>
									<row align="center">
										<label value="&domCompletion.key1;"/>
										<textbox id="kmeleon.plugins.macros.domComplete1.prefix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
										<textbox id="kmeleon.plugins.macros.domComplete1.suffix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
									</row>
									<row align="center">
										<label value="&domCompletion.key2;"/>
										<textbox id="kmeleon.plugins.macros.domComplete2.prefix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
										<textbox id="kmeleon.plugins.macros.domComplete2.suffix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
									</row>
								</rows>
							</grid>
							<separator class="thin"/>
							<description>&macrosNote;</description>
						</groupbox>
						<groupbox>
							<caption>
								<label>&fixupAlt.label;</label>
							</caption>
							<checkbox oncommand="toggleCheckbox(this.id)" id="browser.fixup.alternate.enabled" label="&fixupAlt.enable;"/>
							<separator class="thin"/>
							<grid>
								<columns>
									<column style="padding-right:1em"/>
									<column flex="1"/>
									<column flex="1"/>
								</columns>
								<rows>
									<row align="center">
										<label value="&domCompletion.key2;" style="visibility:hidden"/>
										<label value="&domCompletion.prefix;"/>
										<label value="&domCompletion.suffix;"/>
									</row>
									<row>
										<separator class="thin"/>
									</row>
									<row align="center">
										<label value="&domCompletion.key2;" style="visibility:hidden"/>
										<textbox id="browser.fixup.alternate.prefix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
										<textbox id="browser.fixup.alternate.suffix" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
									</row>
								</rows>
							</grid>
							<separator class="thin"/>
						</groupbox>
						<groupbox>
							<caption>
								<label>&keyword.label;</label>
							</caption>
							<checkbox oncommand="toggleCheckbox(this.id);URLBar.initKeyword()" id="keyword.enabled" label="&keyword.enable;"/>
							<!--<vbox id="keyword">
								<separator class="thin"/>
								<hbox align="center">
									<label value="&domCompletion.key2;" style="visibility:hidden;padding-right:1em"/>
									<label value="&keyword.engine;"/>
									<menulist id="keyword.URL" oncommand="setCharPref(this.id,this.value)" flex="1"/>
								</hbox>
							</vbox>-->
						</groupbox>
						<!-- URL Bar -->
					</tabpanel>
					<tabpanel flex="1" orient="vertical">
						<!-- Web Search -->
						<separator class="thin"/>
						<description>&websearch.descr;</description>
						<separator class="thin"/>
						<vbox id="engine" flex="1" style="overflow:auto"/>
						<vbox flex="1000"/>
						<separator class="thin"/>
						<hbox>
							<button label="&webfind.addEntry;" oncommand="Search.add(0);"/>
							<hbox flex="1"/>
						</hbox>
						<separator class="thin"/>
						<description id="JSBridgeOn" hidden="true">&macrosNote;</description>
						<description id="JSBridgeOff">&websearch.note;</description>
						<!-- Web Search -->
					</tabpanel>
					<tabpanel flex="1" orient="vertical">
						<!-- Metasearch -->
						<separator class="thin"/>
						<description>&meta.descr;</description>
						<separator class="thin"/>
						<vbox id="meta" flex="1" style="overflow:auto"/>
						<vbox flex="1000"/>
						<separator class="thin"/>
						<hbox>
							<button label="&webfind.addEntry;" oncommand="Search.add(1);"/>
							<hbox flex="1"/>
						</hbox>
						<separator class="thin"/>
						<description>&macrosNote;</description>
						<!-- Metasearch -->
					</tabpanel>
					<tabpanel flex="1" orient="vertical" style="overflow:auto">
						<!-- Database -->
						<separator class="thin"/>
						<description>&database.descr;</description>
						<separator class="thin"/>
						<tree flex="1" seltype="single" hidecolumnpicker="true" onselect="Database.select()">
							<treecols>
								<treecol label="&database.caption;" flex="1"/>
								<splitter class="tree-splitter"/>
								<treecol label="&database.url;" flex="2"/>
							</treecols>
							<treechildren id="database"/>
						</tree>
						<separator class="thin"/>
						<hbox>
							<button label="&add;" oncommand="Database.add()" key="addKey"/>
							<button id="edit" label="&edit;" oncommand="Database.edit()" disabled="true"/>
							<button id="delete" label="&delete;" oncommand="Database.del()" key="delKey" disabled="true"/>
							<hbox flex="1"/>
						</hbox>
						<!-- Database -->
					</tabpanel>
					<!--        <tabpanel flex="1" orient="vertical" style="overflow:auto">	-->
					<!-- Search how 
<separator class="thin"/>
<vbox id="searchbutton">
<groupbox flex="1">
	<caption><label>&searchbutton.HowST;</label></caption>
	<vbox>
		<separator class="thin"/>
		<description value="&searchbutton.descr;"/>
			<menulist id="kmeleon.plugins.macros.searchtyped.openurl" oncommand="setCharPref(this.id,this.value)">
				<menupopup style="margin-left:5px;">
					<menuitem value="open" label="&searchbutton.open;"/>
					<menuitem value="opennew" label="&searchbutton.opennew;"/>
					<menuitem value="openbg" label="&searchbutton.openbg;"/>
					<menuitem value="opentab" label="&searchbutton.opentab;"/>
					<menuitem value="openbgtab" label="&searchbutton.openbgtab;"/>
				</menupopup>
			</menulist>
	</vbox>
</groupbox>
</vbox>
<groupbox id="searchselected">
	<caption><label>&searchselected.HowST;</label></caption>
	<vbox>
		<separator class="thin"/>
		<description value="&searchselected.descr;"/>
			<menulist id="kmeleon.plugins.macros.selected.openurl" oncommand="setCharPref(this.id,this.value)">
				<menupopup style="margin-left:5px;">
					<menuitem value="ID_OPEN_LINK" label="&searchselected.open;"/>
					<menuitem value="ID_OPEN_LINK_IN_NEW_WINDOW" label="&searchselected.opennew;"/>
					<menuitem value="ID_OPEN_LINK_IN_BACKGROUND" label="&searchselected.openbg;"/>
					<menuitem value="ID_OPEN_LINK_IN_NEW_TAB" label="&searchselected.opentab;"/>
					<menuitem value="ID_OPEN_LINK_IN_BACKGROUNDTAB" label="&searchselected.openbgtab;"/>
				</menupopup>
			</menulist>
	</vbox>
</groupbox>
	Search how -->
					<!--   </tabpanel>	-->
				</tabpanels>
			</tabbox>
		</tabcontrol>
	</vbox>
</page>