<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin/"?>
<?xml-stylesheet type="text/css" href="chrome://kmprefs/skin/"?>
<?xul-overlay href="chrome://kmprefs/content/kplugins/favoritesbar.xul"?>
<!DOCTYPE page SYSTEM "chrome://kmprefs/locale/pref-kfavorites.dtd">
<page xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="prefFavorites" onload="kFavorites.init()">
	<stringbundle id="pref_bundle" src="chrome://kmprefs/locale/pref.properties"/>
	<stringbundle id="brws_bundle" src="chrome://kmprefs/locale/pref-browsing.properties"/>
	<script type="application/x-javascript" src="chrome://kmprefs/content/pref.js"/>
	<script type="application/x-javascript">
<![CDATA[
var kFavorites = {
	_base : null,
	_pref : null,
	_bndl : null,
	_url : null,
	_mAD : null,
	_mML : null,
	__sO : null,
	load : null,
	init : function () {
		this.load = kPlugin.load("favorites");
		this._base = "kmeleon.plugins.favorites.";
		this._pref = new Array("load");
		this._url = this._base + "openurl";
		this._mAD = this._base + "menuAutoDetect";
		this._mML = this._base + "maxMenuLength";
		this.__sO = this._base + "sortOrder";
		this._bndl = document.getElementById("brws_bundle");
		this._setControls();
		document.getElementById("openURLInCurrent").label = this._bndl.getString(kTabs() ? "tab_current" : "window_current");
		document.getElementById("openURLInTab").label = this._bndl.getString("tab_new");
		document.getElementById("openURLInWindow").label = this._bndl.getString("window_new");
		for (var j = 0; j < this._pref.length; j++)
			initCheckbox(this._base + this._pref[j]);
		kFavSubfolder.init("newitemFolder");
		kFavSubfolder.init("menuFolder");
		kFavoritesBar.init();
	},
	setLength : function (elemID, prefVal) {
		var val = parseInt(prefVal);
		if (prefVal)
			if (isNaN(val) || val < 0)
				alert(document.getElementById("pref_bundle").getString("positive_integer_expected"));
			else {
				pref.setBoolPref(this._mAD, false);
				// bug: Favorites menu disappears when kmeleon.plugins.favorites.maxMenuLength is in [1,6]
				pref.setIntPref(this._mML, (val < 7) ? 7 : val);
			}
		else
			restoreDefault(elemID);
	},
	setOpen : function () {
		pref.setCharPref(this._url, document.getElementById(this._url).value);
		this._initOpen();
	},
	setOrder : function () {
		var val = document.getElementById(this.__sO).value;
		
		pref.setIntPref(this.__sO, document.getElementById("foldersFirst").checked ? (val * 8 + 1) : val);
	},
	toggle : function () {
		toggleCheckbox(this._base + this._pref[0]);
		this.load = !this.load;
		this._setControls();
		kFavoritesBar._setControls();
	},
	toggleForce : function (elem) {
		pref.setBoolPref(this._mAD, !elem.checked);
		elem.nextSibling.disabled = !elem.checked;
		if (elem.nextSibling.value == 9999)
			elem.nextSibling.value = "";
		elem.nextSibling.focus();
	},
	toggleOpen : function (chkBox) {
		if (chkBox.checked)
			pref.setCharPref(this._url, chkBox.id);
		else
			pref.setCharPref(this._url, chkBox.previousSibling.value);
		this._initOpen();
	},
	toggleSpillover : function () {
		var val = document.getElementById(this._mAD).value;
		
		val = (val == "true") ? true : false;
		pref.setBoolPref(this._mAD, val);
		if (!val)
			pref.setIntPref(this._mML, 9999);
		this._initSpillover();
	},
	_initOpen : function () {
		var grp = document.getElementById(this._url),
		val;
		
		try {
			val = pref.getCharPref(this._url);
		} catch (e) {
			if (this.load)
				for (var j = 0, c = document.getElementById(this._url).childNodes; j < c.length; j++)
					c[j].setAttribute("disabled", true);
		}
		document.getElementById("ID_OPEN_LINK_IN_BACKGROUNDTAB").checked = false;
		document.getElementById("ID_OPEN_LINK_IN_BACKGROUND").checked = false;
		switch (val) {
		case "ID_OPEN_LINK":
			grp.selectedIndex = 0;
			break;
		case "ID_OPEN_LINK_IN_NEW_TAB":
			grp.selectedIndex = 1;
			break;
		case "ID_OPEN_LINK_IN_BACKGROUNDTAB":
			grp.selectedIndex = 1;
			document.getElementById("ID_OPEN_LINK_IN_BACKGROUNDTAB").checked = true;
			break;
		case "ID_OPEN_LINK_IN_NEW_WINDOW":
			grp.selectedIndex = 2;
			break;
		case "ID_OPEN_LINK_IN_BACKGROUND":
			grp.selectedIndex = 2;
			document.getElementById("ID_OPEN_LINK_IN_BACKGROUND").checked = true;
			break;
		default:
			grp.selectedIndex = -1;
		}
	},
	_initOrder : function () {
		var grp = document.getElementById(this.__sO),
		val;
		
		try {
			val = pref.getIntPref(this.__sO);
		} catch (e) {
			grp.selectedIndex = -1;
			for (var j = 0, c = grp.childNodes; j < 3; j++)
				c[j].setAttribute("disabled", true);
			return;
		}
		
		if (val % 8 == 1) {
			val = (val - 1) / 8;
			document.getElementById("foldersFirst").checked = true;
		}
		switch (val) {
		case 2:
			grp.selectedIndex = 0;
			break;
		case 6:
			grp.selectedIndex = 1;
			break;
		default:
			grp.selectedIndex = -1;
		}
	},
	_initSpillover : function () {
		var auto,
		grp = document.getElementById(this._mAD),
		val;
		try {
			auto = pref.getBoolPref(this._mAD);
		} catch (e) {
			grp.selectedIndex = -1;
			for (var j = 0, c = grp.childNodes; j < 2; j++)
				c[j].setAttribute("disabled", true);
			document.getElementById("force").disabled = true;
			document.getElementById(this._mML).disabled = true;
			return;
		}
		try {
			val = pref.getIntPref(this._mML);
		} catch (e) {
			val = "";
		}
		document.getElementById(this._mML).value = val;
		if (isNaN(val))
			val = 0;
		if (auto) {
			grp.selectedIndex = 1;
			document.getElementById("force").checked = false;
			document.getElementById("force").disabled = false;
			document.getElementById(this._mML).disabled = true;
		} else {
			grp.selectedIndex = (val == 9999) ? 0 : 1;
			document.getElementById("force").checked = (val > 6 && val < 9999) ? true : false;
			document.getElementById("force").disabled = (grp.selectedIndex == 0) ? true : false;
			document.getElementById(this._mML).disabled = !document.getElementById("force").checked;
		}
	},
	_setControls : function () {
		if (this.load) {
			disableAll(false);
			this._initOpen();
			this._initOrder();
			this._initSpillover();
			document.getElementById("openURLInTab").disabled = !kTabs();
			document.getElementById("ID_OPEN_LINK_IN_BACKGROUNDTAB").disabled = !kTabs();
		} else {
			this._initOpen();
			this._initOrder();
			this._initSpillover();
			disableAll(true);
			document.getElementById("force").disabled = true;
			document.getElementById(this._mML).disabled = true;
		}
		document.getElementById(this._base + this._pref[0]).disabled = kPlugin.absent("favorites");
	}
};
]]>
	</script>
	<vbox flex="1" style="overflow:auto">
		<dialogheader title="&favorites.header;"/>
		<separator class="thin"/>
		<checkbox oncommand="kFavorites.toggle()" id="kmeleon.plugins.favorites.load" label="&favorites.enable;"/>
		<separator class="thick"/>
		<tabcontrol flex="1">
			<tabbox flex="1">
				<tabs>
					<tab id="kFavoritesGnrlTab" label="&general;"/>
					<tab id="kFavoritesMenuTab" label="&favorites.menu;"/>
					<tab id="kFavoritesTBarTab"/>
				</tabs>
				<tabpanels flex="1">
					<tabpanel flex="1" orient="vertical" style="overflow:auto">
						<!-- General -->
						<separator class="thin"/>
						<groupbox>
							<caption>
								<label>&favorites.open;</label>
							</caption>
							<separator class="thin"/>
							<radiogroup id="kmeleon.plugins.favorites.openurl">
								<radio oncommand="kFavorites.setOpen()" value="ID_OPEN_LINK" id="openURLInCurrent"/>
								<radio oncommand="kFavorites.setOpen()" value="ID_OPEN_LINK_IN_NEW_TAB" id="openURLInTab"/>
								<checkbox oncommand="kFavorites.toggleOpen(this)" id="ID_OPEN_LINK_IN_BACKGROUNDTAB" label="&inBackground;" style="margin-left:2em"/>
								<radio oncommand="kFavorites.setOpen()" value="ID_OPEN_LINK_IN_NEW_WINDOW" id="openURLInWindow"/>
								<checkbox oncommand="kFavorites.toggleOpen(this)" id="ID_OPEN_LINK_IN_BACKGROUND" label="&inBackground;" style="margin-left:2em"/>
							</radiogroup>
							<separator class="thin"/>
						</groupbox>
						<separator class="thick"/>
						<hbox align="center">
							<label value="&favorites.newitemFolder;"/>
							<textbox id="kmeleon.plugins.favorites.newitemFolder" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
							<button label="&browse;" oncommand="kFavSubfolder.select(this.previousSibling.id)"/>
							<button label="&restoreDefault;" oncommand="restoreDefault(this.previousSibling.previousSibling.id);kFavSubfolder.init('newitemFolder')"/>
						</hbox>
						<description>&restartNote1;</description>
						<!-- General -->
					</tabpanel>
					<tabpanel flex="1" orient="vertical" style="overflow:auto">
						<!-- Favorites Menu -->
						<separator class="thin"/>
						<hbox>
							<groupbox>
								<caption>
									<label>&spillover.label;</label>
								</caption>
								<separator class="thin"/>
								<description>&spillover.descr;</description>
								<radiogroup id="kmeleon.plugins.favorites.menuAutoDetect">
									<radio oncommand="kFavorites.toggleSpillover()" value="false" label="&spillover.arrows;"/>
									<radio oncommand="kFavorites.toggleSpillover()" value="true" label="&spillover.submenu;"/>
									<hbox align="center">
										<checkbox oncommand="kFavorites.toggleForce(this)" id="force" label="&spillover.forceBefore;" style="margin-left:2em"/>
										<textbox id="kmeleon.plugins.favorites.maxMenuLength" size="4" class="uri-element" oninput="kFavorites.setLength(this.id,this.value)"/>
										<label value="&spillover.forceAfter;"/>
									</hbox>
								</radiogroup>
								<separator class="thin"/>
							</groupbox>
							<groupbox flex="1">
								<caption>
									<label>&menu.order;</label>
								</caption>
								<separator class="thin"/>
								<radiogroup id="kmeleon.plugins.favorites.sortOrder" oncommand="kFavorites.setOrder()">
									<radio value="2" label="&menu.sortAscending;"/>
									<radio value="6" label="&menu.sortDescending;"/>
									<checkbox oncommand="kFavorites.setOrder()" id="foldersFirst" label="&menu.foldersFirst;"/>
								</radiogroup>
								<separator class="thin"/>
							</groupbox>
						</hbox>
						<separator class="thick"/>
						<hbox align="center">
							<label value="&menu.folder;"/>
							<textbox id="kmeleon.plugins.favorites.menuFolder" flex="1" class="uri-element" oninput="setCharPref(this.id,this.value)"/>
							<button label="&browse;" oncommand="kFavSubfolder.select(this.previousSibling.id)"/>
							<button label="&restoreDefault;" oncommand="restoreDefault(this.previousSibling.previousSibling.id);kFavSubfolder.init('menuFolder')"/>
						</hbox>
						<vbox flex="1"/>
						<description>&restartNote1;</description>
						<!-- Favorites Menu -->
					</tabpanel>
					<tabpanel id="kFavoritesTBar"/>
				</tabpanels>
			</tabbox>
		</tabcontrol>
	</vbox>
</page>