<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE bindings SYSTEM "/vkontakte.dtd"><?xml-stylesheet type="text/css" href="/xb-skin/default/style.css"?>

<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <binding extends="_vk.button.simple.xbl#button" id="button">
        <content>
            <xul:toolbarbutton anonid="menu-button" id="yb-vkontakte-button-counter" label="" type="button">
                <xul:menupopup anonid="menu">
                    <xul:menuseparator anonid="separator"/>
                </xul:menupopup>
            </xul:toolbarbutton>
        </content>

		<implementation>
            <field name="menu">document.getAnonymousElementByAttribute(this, "anonid", "menu")</field>
            <field name="separator">document.getAnonymousElementByAttribute(this, "anonid", "separator")</field>

            <field name="vkType" readonly="true">this.button.getAttribute('vk-type')</field>

            <method name="_counterButtonConstructor">
                <body><![CDATA[var $ = this;
var counters = this.app.counters;
$.counter = counters.get($.vkType);
var mailman = this.app.mailman;
mailman.addSubscriber({
subject: "please-update",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: target}) {
if ($.vkType == target)
{
$.waiting = true;
}

}
}});
mailman.addSubscriber({
subject: "request-failed",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: name}) {
if ($.vkType == name)
{
$.waiting = false;
}

}
}});
mailman.addSubscriber({
subject: "counter-update",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: counter}) {
if ($.vkType == counter.id)
{
$.counter = counter.value;
}

}
}});]]></body>
            </method>

            <field name="_counter">0</field>
            <property name="counter">
                <getter><![CDATA[return this._counter;]]></getter>
                <setter><![CDATA[var newLabel = "";
if ("number" == typeof val && val > 0)
{
newLabel = String(val);
}

this.button.setAttribute("label",newLabel);
this._counter = val;]]></setter>
            </property>

            <property name="authorized">
                <getter><![CDATA[var value = this.button.getAttribute("authorized");
return "true" === value;]]></getter>

                <setter><![CDATA[this.log("Set authorized to \"" + val + "\".");
if (true === val)
{
this.button.type = "menu-button";
this.button.setAttribute("authorized","true");
}
else
if (false === val)
{
this.button.type = "button";
this.button.setAttribute("authorized","false");
this.counter = 0;
}

this.updateTooltip();]]></setter>
            </property>

            <property name="waiting">
                <setter><![CDATA[if (true === val)
{
this.button.setAttribute("waiting","true");
}
else
if (false === val)
{
this.button.setAttribute("waiting","false");
}
]]></setter>
            </property>

            
            <field name="tooltips" readonly="true">
                ({
                    'need_auth': '',
                    'normal': '',
                    'counter': ''
                })
            </field>
            <method name="updateTooltip">
                <body><![CDATA[var templater = this.app.templater;
var tooltipType = "";
var data = null;
if (! this.authorized)
{
tooltipType = "need_auth";
}
else
{
if (this.counter > 0)
{
tooltipType = "counter";
data = {
number: this.counter};
}
else
{
tooltipType = "normal";
}

}

this.log("Set tooltip type to \"" + tooltipType + "\".");
var tooltip = "";
if ("counter" == tooltipType)
{
let forms = this.tooltips[tooltipType];
tooltip = this.selectForm(this.counter,forms.form_1,forms.form_2,forms.form_5);
}
else
{
tooltip = this.tooltips[tooltipType];
}

var newTooltip = templater.getString(tooltip,data);
this.log("New tooltip is \"" + newTooltip + "\".");
this.button.setAttribute("tooltiptext",newTooltip);]]></body>
            </method>

            <method name="selectForm">
                <parameter name="n"/>
                <parameter name="form_1"/>
                <parameter name="form_2"/>
                <parameter name="form_5"/>
                <body><![CDATA[return this.app.getPluralForm(n,[form_1, form_2, form_5]);]]></body>
            </method>

		</implementation>
    </binding>

</bindings>
