<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bindings SYSTEM "../entities.dtd">

<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="toolbarbutton" extends="/-common/content/basewidget.xbl#toolbarbuttonbase">
		<resources>
			<stylesheet src="main.css" />
		</resources>
		<content>
			<xul:toolbarbutton class="toolbarbutton-1 widget-button" anonid="mailru-button-auth" label="&mailru.main-button.text;" item-command="auth" item-href="inbox" />
			<xul:toolbarbutton class="toolbarbutton-1 widget-button" anonid="mailru-button-main" type="menu-button" label="&mailru.main-button.text;" item-href="inbox">
				<xul:menupopup>
					<xul:menuitem class="menuitem-iconic menuitem-with-favicon" label="&mailru.main-button.menu.check_mail;" item-command="update" />
					<xul:menuseparator />
					<xul:menuitem anonid="mailbox" class="menuitem-iconic menuitem-with-favicon" disabled="true" label="" />
					<xul:menuitem class="menuitem-iconic menuitem-with-favicon" label="&mailru.main-button.menu.new_message;" item-href="compose" />
					<xul:menuitem class="menuitem-iconic menuitem-with-favicon" label="&mailru.main-button.menu.read_mail;" item-href="inbox" />
					<xul:menuitem class="menuitem-iconic menuitem-with-favicon" label="&mailru.main-button.menu.open_address_book;" item-href="addressbook" />
					<xul:menuseparator />
					<xul:menuitem class="menuitem-iconic menuitem-with-favicon" label="&mailru.main-button.menu.show_settings;" item-command="settings" />
					<xul:menuseparator />
					<xul:menuitem class="menuitem-iconic menuitem-with-favicon" label="&mailru.main-button.menu.logout;" item-command="logout" />
				</xul:menupopup>
			</xul:toolbarbutton>
			<xul:toolbarbutton class="toolbarbutton-1 widget-button" anonid="mailru-button-compose" label="&mailru.new-message.text;" tooltiptext="&mailru.new-message.tooltiptext.auth_ok;" item-href="compose" />
		</content>
		<implementation implements="nsISupports, nsIDOMEventListener">
			<field name="authButton">document.getAnonymousElementByAttribute(this, "anonid", "mailru-button-auth")</field>
			<field name="mainButton">document.getAnonymousElementByAttribute(this, "anonid", "mailru-button-main")</field>
			<field name="composeButton">document.getAnonymousElementByAttribute(this, "anonid", "mailru-button-compose")</field>
			<field name="mailboxMI">document.getAnonymousElementByAttribute(this, "anonid", "mailbox")</field>
			<field name="observers"><![CDATA[
			({
				"showitems": function() {
					this.showItems();
				},
				"throbber": function(topic, data) {
					this.mainButton.setAttribute('throbber', !!data);
				},
				"display": function(topic) {
					this.updateView();
				}
			})
			]]></field>
			<field name="commands"><![CDATA[
			({})
			]]></field>
			<constructor><![CDATA[
				this.log('constructor');
                var ent = this.application.entities;
                this.authButton.setAttribute('tooltiptext', ent.text("mailru.main-button.tooltiptext.auth_none"));
                this.showItems();
				this.updateView();
			]]></constructor>
			<method name="showItems">
				<body><![CDATA[
					var shMainLabel = this.application.api.Settings.getValue('show-main-button-title');
					var shCLabel = this.application.api.Settings.getValue('show-compose-button-title');
					var shCButton = this.application.api.Settings.getValue('show-compose-button');
					this.authButton.setAttribute('show-label', shMainLabel);
					this.mainButton.setAttribute('show-label', shMainLabel);
					this.composeButton.setAttribute('show-label', shCLabel);
					this.composeButton.setAttribute('hidden', !shCButton);
				]]></body>
			</method>
			<method name="updateView">
				<body><![CDATA[
                    var data = this.application.getUserData();
					this.setAttribute("auth", !!data);
					if (data) {
                        this.mailboxMI.setAttribute('label', data.mail);
                        this.mainButton.setAttribute('counter', data.count || '');
                        var tipText, ent = this.application.entities;
                        if (data.error) {
                            tipText = ent.text("mailru.main-button.tooltiptext.update_failed");
                        } else {
                            var tip = ent.text("mailru.main-button.tooltiptext.ok_template");
                            if (!data.mail) {
                                tip.replace('\n{$address}', '');
                            }
                            var pforms = ent.text("mailru.main-button.tooltiptext.ok_template.plural_forms");
                            var countStr = (data.count > 0) 
                                ? this.common.strUtils.plural(data.count, pforms).replace("{N}", data.count)
                                : ent.text("mailru.main-button.tooltiptext.ok_template.no_messages");
                            tipText = tip.replace("{$address}", data.mail).replace("{$new_msg_wordform}", countStr);
                        }
                        this.mainButton.setAttribute('tooltiptext', tipText);
					}
				]]></body>
			</method>
		</implementation>
	</binding>
</bindings>