<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<!DOCTYPE prefwindow [
	<!ENTITY % localLocale SYSTEM "chrome://feedbar/locale/locale.dtd">
	%localLocale;
	<!ENTITY % backupLocale SYSTEM "chrome://feedbar-default-locale/content/locale.dtd">
	%backupLocale;
]>
<prefwindow
	title="Feed Sidebar - &feedbar.options;"
	buttons="accept,cancel"
	onload="FEEDBAR_PREFS.onload();"
	ondialogaccept="FEEDBAR_PREFS.unload();"
	xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
	<script type="application/x-javascript">
		<![CDATA[
		var FEEDBAR_PREFS = {
			prefs : null,
			browserPrefs : null,
			
			strings : {
				_backup : null,
				_main : null,

				initStrings : function () {
					if (!this._backup) { this._backup = document.getElementById("feedbar-backup-string-bundle"); }
					if (!this._main) { this._main = document.getElementById("feedbar-string-bundle"); }
				},

				getString : function (key) {
					this.initStrings();

					var rv = "";

					try {
						rv = this._main.getString(key);
					} catch (e) {
					}

					if (!rv) {
						try {
							rv = this._backup.getString(key);
						} catch (e) {
						}
					}

					return rv;
				},

				getFormattedString : function (key, args) {
					this.initStrings();

					var rv = "";

					try {
						rv = this._main.getFormattedString(key, args);
					} catch (e) {
					}

					if (!rv) {
						try {
							rv = this._backup.getFormattedString(key, args);
						} catch (e) {
						}
					}

					return rv;
				}
			},
			
			onload : function () {
				this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.feedbar.");
				this.browserPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("browser.preferences.");
				
 				if (!("@mozilla.org/alerts-service;1" in Components.classes)) {
					document.getElementById("growl").hidden = true;
				}
				
				var displayPeriod = this.prefs.getIntPref("displayPeriod");
				
				if (displayPeriod) {
					document.getElementById("display-period").selectedIndex = 1;
					this.showOnly(displayPeriod);
				}
				else {
					document.getElementById("display-period").selectedIndex = 0;
					this.showAll();
				}
				
				var featuredFeeds = this.prefs.getCharPref("featuredFeeds");
				
				if (!featuredFeeds) {
					document.getElementById("featured-pane").style.display = "none";
				}
				else {
					featuredFeeds = JSON.parse(featuredFeeds);
					
					if (featuredFeeds.length == 0) {
						document.getElementById("featured-pane").style.display = "none";
					}
					else {
						function sorter(a, b) {
							return 0.5 - Math.random();
						}
						
						featuredFeeds.sort(sorter);
						
						for (var i = 0; i < featuredFeeds.length; i++) {
							var box = document.createElement("groupbox");
							box.setAttribute("orient", "horizontal");
							
							if ("image" in featuredFeeds[i]) {
								var image = document.createElement("image");
								image.style.height = "64px";
								image.style.width = "64px";
								image.style.maxHeight = "64px";
								image.style.marginRight = "10px";
								image.setAttribute("src", featuredFeeds[i].image);
								
								var ibox = document.createElement("vbox");
								var s1 = document.createElement("spring");
								s1.setAttribute("flex", 1);
								var s2 = document.createElement("spring");
								s2.setAttribute("flex", 1);
								
								ibox.appendChild(s1);
								ibox.appendChild(image);
								ibox.appendChild(s2);
								box.appendChild(ibox);
							}
							
							var vbox = document.createElement("vbox");
							vbox.setAttribute("flex", 1);
							
							var label = document.createElement("label");
							label.setAttribute("value", featuredFeeds[i].name);
							label.setAttribute("class", "text-link");
							label.setAttribute("href", featuredFeeds[i].siteUrl);
							label.style.paddingLeft = 0;
							label.style.marginLeft = 0;
							
							var description = document.createElement("description");
							description.appendChild(document.createTextNode(featuredFeeds[i].description));
							
							var isSubscribed = this.isSubscribed(featuredFeeds[i].url);
							
							var button = document.createElement("button");
							
							button.feedUrl = featuredFeeds[i].url;
							button.siteUrl = featuredFeeds[i].siteUrl;
							button.name = featuredFeeds[i].name;
							button.description = featuredFeeds[i].description;
							
							if (isSubscribed) {
								button.setAttribute("label", FEEDBAR_PREFS.strings.getString("feedbar.featured.unsubscribe"));
							}
							else {
								button.setAttribute("label", FEEDBAR_PREFS.strings.getString("feedbar.featured.subscribe"));
							}
							
							button.setAttribute("oncommand", "if (FEEDBAR_PREFS.isSubscribed(this.feedUrl)) { this.setAttribute('label', FEEDBAR_PREFS.strings.getString('feedbar.featured.subscribe')); FEEDBAR_PREFS.unsubscribe(this.feedUrl); } else { this.setAttribute('label', FEEDBAR_PREFS.strings.getString('feedbar.featured.unsubscribe')); FEEDBAR_PREFS.subscribe(this.name, this.feedUrl, this.siteUrl, this.description); }");
							
							vbox.appendChild(label);
							vbox.appendChild(description);
							vbox.appendChild(button);
							
							box.appendChild(vbox);
							
							document.getElementById("featured-feeds").appendChild(box);
						}
						
						sizeToContent();
					}
				}
			},
			
			isSubscribed : function (url) {
				for (var i = 0; i < FEED_GETTER.feedsToFetch.length; i++) {
					if (FEED_GETTER.feedsToFetch[i].feed == url) {
						return true;
					}
				}
				
				return false;
			},
			
			subscribe : function (title, feedUrl, siteUrl, description) {
				var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
				var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Components.interfaces.nsINavBookmarksService);
				var annotationService = Components.classes["@mozilla.org/browser/annotation-service;1"].getService(Components.interfaces.nsIAnnotationService);
				var livemarkService = Components.classes["@mozilla.org/browser/livemark-service;2"].getService(Components.interfaces.nsILivemarkService);
				var menu = Application.bookmarks.menu;
				var uri = ioService.newURI(siteUrl, null, null);
				var feedUri = ioService.newURI(feedUrl, null, null);
				var lm = livemarkService.createLivemarkFolderOnly(Application.bookmarks.menu.id, title, uri, feedUri, -1);
				annotationService.setItemAnnotation(lm, "bookmarkProperties/description", description, 0, Components.interfaces.nsIAnnotationService.EXPIRE_NEVER);
			},
			
			unsubscribe : function (url) {
				var anno = Components.classes["@mozilla.org/browser/annotation-service;1"].getService(Components.interfaces.nsIAnnotationService);
				var livemarkIds = anno.getItemsWithAnnotation("livemark/feedURI", {});
				var livemarkService = Components.classes["@mozilla.org/browser/livemark-service;2"].getService(Components.interfaces.nsILivemarkService);

				for (var i = 0; i < livemarkIds.length; i++){
					var feedURL = livemarkService.getFeedURI(livemarkIds[i]).spec;
					
					if (feedURL == url) {
						var bookmarkService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Components.interfaces.nsINavBookmarksService);
						bookmarkService.removeFolder(livemarkIds[i]);
					}
				}
			},
			
			unload : function () {
				var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
				var browserPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("browser.preferences.");
				
				if (!browserPrefs.getBoolPref("instantApply")) {
					var prefs = document.getElementById("all_prefs").getElementsByTagName("preference");
					
					for (var i = 0; i < prefs.length; i++) {
						switch (prefs[i].getAttribute("type")) {
							case 'bool':
								prefService.setBoolPref(prefs[i].getAttribute("name"), prefs[i].value);
							break;
							case 'int':
								prefService.setIntPref(prefs[i].getAttribute("name"), prefs[i].value);
							break;
						}
					}
				}

				if (document.getElementById("show-all-radio").selected) {
					prefService.setIntPref("extensions.feedbar.displayPeriod", 0);
				}
				else {
					prefService.setIntPref("extensions.feedbar.displayPeriod", parseInt(document.getElementById("display-days").value,10));
				}
			},

			showAll : function () {
				document.getElementById("display-days").disabled = true;
				
				if (this.browserPrefs.getBoolPref("instantApply")) {
					this.prefs.setIntPref("displayPeriod", 0);
				}
			},
			
			showOnly : function (days) {
				document.getElementById("display-days").disabled = false;
				
				if (!days) {
					days = 1;
				}
				
				document.getElementById("display-days").value = days;
				
				if (this.browserPrefs.getBoolPref("instantApply")) {
					this.prefs.setIntPref("displayPeriod", parseInt(days, 10));
				}
			}
		};
		
		Components.utils.import("resource://feedbar-modules/feed-getter.js");
		
		addEventListener("load", FEED_GETTER.load, false);
		addEventListener("unload", FEED_GETTER.unload, false);
		
		]]>
	</script>
	<stringbundleset id="stringbundleset">
		<stringbundle id="feedbar-backup-string-bundle" src="chrome://feedbar-default-locale/content/locale.properties" />
		<stringbundle id="feedbar-string-bundle" src="chrome://feedbar/locale/locale.properties" />
	</stringbundleset>
	<prefpane id="general-pane" label="&feedbar.options.general;" image="chrome://feedbar/content/skin-common/emblem-system.png">
		<preferences id="all_prefs">
			<preference id="p_hide_read" name="extensions.feedbar.hideReadItems" type="bool" />
			<preference id="p_update_freq" name="extensions.feedbar.updateFrequency" type="int" />
			<preference id="p_show_days" name="extensions.feedbar.displayPeriod" type="int" />
			<preference id="p_autoClose" name="extensions.feedbar.autoClose" type="bool" />
			<preference id="p_notify" name="extensions.feedbar.notify" type="bool" />
			<preference id="p_sidebar" name="extensions.feedbar.runInSidebar" type="bool" />
			<preference id="p_fullPreview" name="extensions.feedbar.showFullPreview" type="bool" />
			<preference id="p_new_tab" name="extensions.feedbar.openInNewTab" type="bool" />
			<preference id="p_featured" name="extensions.feedbar.featuredFeeds.notify" type="bool" />
		</preferences>
		<groupbox>
			<hbox>
				<label value="&feedbar.updateEvery1;" style="padding-top: 4px;" control="updateFreq"/>
				<textbox id="updateFreq" preference="p_update_freq" size="2" aria-labelledby="updateFreq updateFreqUnit"/>
				<label id="updateFreqUnit" value="&feedbar.updateEvery2;" style="padding-top: 4px;"  />
			</hbox>
		</groupbox>
		<groupbox>
			<radiogroup id="display-period" style="padding-left: 2ex;">
				<radio label="&feedbar.showAllItemsFull;" id="show-all-radio" oncommand="FEEDBAR_PREFS.showAll();" />
				<hbox align="center">
					<radio id="showOnlyRadio" label="&feedbar.onlyShow1;" oncommand="FEEDBAR_PREFS.showOnly(document.getElementById('display-days').value);" />
					<textbox onblur="FEEDBAR_PREFS.showOnly(this.value);" id="display-days" size="2" aria-labelledby="showOnlyRadio display-days onlyShowUnit"/>
					<label id="onlyShowUnit" value="&feedbar.onlyShow2;" />
				</hbox>
			</radiogroup>
			<checkbox label="&feedbar.hideReadItems;" preference="p_hide_read" />
		</groupbox>
		<groupbox>
			<checkbox label="&feedbar.autoClose;" preference="p_autoClose" />
			<checkbox id="growl" label="&feedbar.notifications;" preference="p_notify" />
			<checkbox label="&feedbar.runInSidebar;" preference="p_sidebar" />
		</groupbox>
		<groupbox>
			<label value="&feedbar.previewQuestion;" control="fullPreview"/>
			<radiogroup id="fullPreview" preference="p_fullPreview" style="padding-left: 2ex;">
				<radio label="&feedbar.previewAnswer1;" value="false" />
				<radio label="&feedbar.previewAnswer2;" value="true" />
			</radiogroup>
			<checkbox label="&feedbar.alwaysOpenInNewTab;" preference="p_new_tab" />
		</groupbox>
	</prefpane>
	<prefpane id="featured-pane" label="&feedbar.featured.panelTitle;" image="chrome://feedbar/content/skin-common/feed-icon-32.png">
		<vbox style="max-width: 460px;">
			<description>&feedbar.featured.description;</description>
			<description style="font-size: 8pt;">&feedbar.featured.contact;</description>
			<checkbox preference="p_featured" label="&feedbar.featured.notifyMe;" />
			<vbox id="featured-feeds" style="min-height: 400px;">
			</vbox>
		</vbox>
	</prefpane>
</prefwindow>