<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
		  xmlns:html="http://www.w3.org/1999/xhtml">
		  
	<binding id="toolbarbuttonbase" extends="//toolkit/native/bindings/widget.xml#widget">
		<resources>
			<stylesheet src="basewidget.css" />
		</resources>
		<implementation implements="nsISupports, nsIDOMEventListener">
			
			<constructor><![CDATA[
				this.firstConstructor = !this.__XBID__;
				var tag = this, isPalette = false;
				while (tag && (tag.localName != "toolbar")) {
					if (tag.localName == "toolbarpaletteitem") {
						isPalette = true;
						break;
					}
					tag = tag.parentNode;
				}
				this.setAttribute("widget-state-palette", isPalette);
				this.statePalette = isPalette;
				if(this.firstConstructor) { 
					this.__XBID__ = "xb_"+Math.random();
					if (this.xbinit) { this.xbinit(); }
					this.__addObservers();
				}
			]]></constructor>
			
			<method name="getWindowListener"><body><![CDATA[
				return this.common.api.Browser.getWindowListener(document.defaultView);
			]]></body></method>
			
			<method name="getCurrentURL"><body><![CDATA[
				return this.getWindowListener().windowLocation;
			]]></body></method>
			
			<method name="createNode">
				<parameter name="ns"/>
				<parameter name="name"/>
				<parameter name="attrs"/>
				<parameter name="style"/>
				<body><![CDATA[
					var ni = (ns ? document.createElementNS(ns, name) : document.createElement(name));
					if(attrs) {
						for(var i in attrs) {
							if(attrs.hasOwnProperty(i)) { ni.setAttribute(i, attrs[i]); }
						}
					}
					if (style) {
						this.common.utils.copy(style, ni.style);
					}
					return ni;
				]]></body>
			</method>
            
            <method name="setMenuitemCounter">
				<parameter name="node"/>
				<parameter name="value"/>
				<body><![CDATA[
                    if (typeof node == "string") {
                        node = document.getAnonymousElementByAttribute(this, "anonid", node);
                    }
                    if (node) {
                        node.setAttribute("acceltext", value);
                    }
				]]></body>
			</method>
            <method name="getMenuitemCounter">
				<parameter name="node"/>
				<parameter name="value"/>
				<body><![CDATA[
                    if (typeof node == "string") {
                        node = document.getAnonymousElementByAttribute(this, "anonid", node);
                    }
                    return (node ? node.getAttribute("acceltext") : "");
				]]></body>
			</method>
			
			<method name="createMenuItem">
				<parameter name="text"/>
				<parameter name="attrs"/>
				<body><![CDATA[
					attrs = attrs||{};
					attrs.label = attrs.label || text;
                    if (typeof attrs.counter !== "undefined") {
                        attrs.acceltext = attrs.counter;
                        delete attrs.counter;
                    }
					return this.createNode(null, "menuitem", attrs);
				]]></body>
			</method>
			
			<method name="log">
				<parameter name="text"/>
				<parameter name="method"/>
				<body><![CDATA[
					this.application.log("[*XB*]: " + text, method);
				]]></body>
			</method>
			
			<method name="logObj">
				<parameter name="obj"/>
				<parameter name="prefix"/>
				<parameter name="method"/>
				<body><![CDATA[
					this.application.logObj(obj, "[*XB*]: " + (prefix||""), method);
				]]></body>
			</method>
			
			<method name="openSettingsDialog">
				<body><![CDATA[
					this.common.api.Controls.openSettingsDialog(window, this.WIID || this.application.WIID);
				]]></body>
			</method>
			
			
			<method name="__addObservers">
				<body><![CDATA[
					if (!this.observers || this.__addedObservers || !this.common.observerService) { return; }
					this.log("addObservers");
					var winListener = this.getWindowListener();
					this.__addedObservers = [];
					function f(th, observers, method) {
						if (!observers) { return; }
						for(var topic in observers) {
							if (observers.hasOwnProperty(topic)) {
								
								var obj = null, isWinLsr = (winListener.KNOWN_TOPICS.indexOf(topic) >= 0);
								
								if (isWinLsr) {
									obj = {topic: topic, _xbWinObserver: {
										observe: function(x, t, d) { observers[t].call(th, t, d, x); }
									}};
									winListener.addListener(topic, obj._xbWinObserver);
								} else {
									obj = th.common.observerService[method](topic, observers[topic], th);
								}
								if (obj) {
									th.__addedObservers.push(obj); 
								}
							}
						}
					}
					f(this, this.observers, "addAppObserver");
				]]></body>
			</method>
			
			<method name="__removeObservers">
				<body><![CDATA[
					if (!this.__addedObservers || !this.common.observerService) { return; }
					this.log('removeObservers');
					var winListener = this.getWindowListener();
					for(var i=0; i<this.__addedObservers.length; ++i) {
						var obj = this.__addedObservers[i];
						if (obj._xbWinObserver) {
							winListener.removeListener(obj.topic, obj._xbWinObserver);
						} else {
							this.common.observerService.removeObserver(obj);
						}
					}
					this.__addedObservers = null;
				]]></body>
			</method>
			
			<method name="__OnCommand">
				<parameter name="event"/>
				<parameter name="isMiddleBtn"/>
				<body><![CDATA[
					var target = event.originalTarget;
					if (target.hasAttribute("xbl:inherits")) { // XXX dirty
						target = target.parentNode;
					}
					var name = String(target.localName).toLowerCase();
					if (isMiddleBtn && (name == "menuitem")) { return; }
					
					if (this.application.config.useClickStatistics) {
						switch (name) {
							case "toolbarbutton":
								this.common.api.Statistics.logButtonClick();
							break;
							case "menuitem":
								this.common.api.Statistics.logMenuClick();
							break;
						}
					}
					
					var commandAttributeName = "item-command";
					var hrefAttributeName = "item-href";
					var targetAttributeName = "item-target";
                    var paramAttributeName = "item-param";
					
					function getAttr(attr) {
						var v = target.getAttribute(attr);
						var newTgt = target;
						
						if (!v) {
							newTgt = document.getBindingParent(target);
							v = newTgt.getAttribute(attr);
						}
						return {val: v, target: newTgt};
					}
					
					var command = getAttr(commandAttributeName);
					var href = getAttr(hrefAttributeName);
                    var param = getAttr(paramAttributeName);
					var hrefPrior = (href.val && (isMiddleBtn || event.ctrlKey || event.shiftKey));
					
					if (command.val && !hrefPrior) {
						var scope = this;
						var commands = this.commands;
						var eventInfo = {param: param.val, widget: this, event: event, target: command.target, isMiddleBtn: isMiddleBtn, isMenuItem: (name == 'menuitem')};
						var commandf = (commands ? commands[command.val] : null);
						if (!commandf) {
							scope = this.application;
							commands = this.application.uiCommands;
							commandf = (commands ? commands[command.val] : null);
						}
						while (commandf && (typeof commandf == "string")) { commandf = commands[commandf]; }
						if (commandf) {
							if (commandf.call(scope, command.val, eventInfo) !== false) {
                                return;
                            }
						} else {
							if (command.val == "settings") {
								this.openSettingsDialog();
								return;
							}
						}
					}
					if (href.val) {
						var obj = { url: href.val };
						var tgt = target.getAttribute(targetAttributeName);
						if (!isMiddleBtn && !event.ctrlKey && !event.shiftKey && tgt) {
							obj.target = tgt;
						} else {
							obj.eventInfo = event;
						}
						this.application.navigateBrowser(obj, param.val);
					}
				]]></body>
			</method>
			
		</implementation>
		<handlers>
			<handler event="click" button="1"><![CDATA[
				this.__OnCommand(event, true);
            ]]></handler>
			<handler event="command"><![CDATA[
				this.__OnCommand(event, false);
			]]></handler>
		</handlers>
	</binding>
</bindings>