<?xml version="1.0"?>
<!DOCTYPE window SYSTEM "chrome://livemargins/locale/appcenter.dtd">
<window id="appcenter_window"
		xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
		xmlns:html="http://www.w3.org/1999/xhtml"
		title="&appcenterTitle;">
	<html:link rel="icon" 
			   href="chrome://livemargins/skin/appcenter-16.png" 
			   style="display:none"/>	
	<browser id="appcenter_content" 
			 type="content" 
			 autoscroll="false" 
			 flex="1"
			 onload="appCenter.init(event);"
			 disablehistory="true"
			 src="resource://livemargins/appcenter.xhtml"></browser>
	<script type="application/x-javascript" src="chrome://livemargins/content/appContentAPI.js"></script>
	<script type="application/x-javascript">
	<![CDATA[
		(function () {
			var jsm = {};
			Components.utils.import("resource://livemargins/utils.jsm", jsm);
			
			if (!jsm.utils.isFFVersionGET4()) {
				return;
			}
			
			function getChromeWindow() {
				return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
							 .getInterface(Components.interfaces.nsIWebNavigation)
							 .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
							 .rootTreeItem
							 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
							 .getInterface(Components.interfaces.nsIDOMWindow);
			}
			
			function getMyTab() {
				var browserIdx = getChromeWindow().gBrowser.getBrowserIndexForDocument(document);
				return getChromeWindow().gBrowser.tabContainer.getItemAtIndex(browserIdx);	
			}
			
			var gBrowser = getChromeWindow().gBrowser;
			if (!gBrowser) {
				return;
			}
			
			Components.utils.import("resource://gre/modules/AddonManager.jsm",jsm);
			
			// remove all Appcenter tab pages.
			function _onAppcenterDisabling(addon) {
				if(addon.id == "livemargins@mozillaonline.com") {
					gBrowser.removeTab(getMyTab());
				}
			}
			
			var listener = {
				onUninstalling : _onAppcenterDisabling,
				onDisabling : _onAppcenterDisabling
			};
			
			window.addEventListener("load", function(){
				jsm.AddonManager.addAddonListener(listener);
			}, false);
			
			window.addEventListener("unload", function() {
				jsm.AddonManager.removeAddonListener(listener);
			}, false);
			
			if (!jsm.utils.getPref("pinfirsttab")) {
				return;
			}
			
			// check if there is already pinned tab
			for (let i = 0; i < gBrowser.tabs.length; i++) {
				if (gBrowser.tabs[i].pinned) {
					if (gBrowser.getBrowserAtIndex(i).contentWindow.document.location == "about:appcenter") {
						return;
					}
				}
			}
			
			gBrowser.pinTab(getMyTab());
			
			function onUnpinTab(event) {
				if (getMyTab() == event.target) {
					jsm.utils.setPref("pinfirsttab", false);
				}
			}
			
			// add event listener
			gBrowser.tabContainer.addEventListener("TabUnpinned", onUnpinTab, false);
			
			window.addEventListener("unload", function() {
				gBrowser.tabContainer.removeEventListener("TabUnpinned", onUnpinTab, false);
			}, false);
		})();
		]]>
	</script>
</window>