<?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 id="button">
        <content>
            <xul:toolbarbutton anonid="button" id="yb-vkontakte-button-simple" label="" type="button"/>
        </content>

        <handlers>
            <handler event="command"><![CDATA[var mailman = this.app.mailman;
if (! this.authorized)
{
mailman.send("please-start-auth");
return;
}

var target = event.originalTarget;
if ("toolbarbutton" == target.localName)
{
target = this.button;
}

var action = null;
var data = null;
if (target.hasAttribute("action"))
{
action = target.getAttribute("action");
if ("update" === action && target.hasAttribute("update"))
{
data = target.getAttribute("update");
}
else
if ("open-page" === action && target.hasAttribute("page"))
{
data = target.getAttribute("page");
}
else
if ("open-url" === action && target.hasAttribute("url"))
{
data = target.getAttribute("url");
}
else
if (target.hasAttribute("data"))
{
data = target.getAttribute("data");
}

mailman.send({
subject: "please-" + action,
body: data});
}
]]></handler>
        </handlers>

		<implementation>
            <field name="container" readonly="true">this.parentNode</field>
            <field name="app" readonly="true">this.parentNode.application</field>
            <field name="api" readonly="true">this.app.api</field>

            <field name="BUTTON_NAME" readonly="true">'_virtual'</field>

            <field name="button" readonly="true">document.getAnonymousElementByAttribute(this, "anonid", "button")</field>

            
            <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.setAttribute("authorized","true");
this.updateTooltip();
}
else
if (false === val)
{
this.button.setAttribute("authorized","false");
this.updateTooltip();
this.counter = 0;
}
]]></setter>
            </property>

            <field name="_userId">0</field>
            <property name="userId">
                <getter><![CDATA[if (0 !== this._userId)
{
return this._userId;
}
else
{
let mailman = this.app.mailman;
mailman.send("please-get-user-id");
return this._userId;
}
]]></getter>
                <setter><![CDATA[this._userId = val;]]></setter>
            </property>

            <field name="__constructed">false</field>
			<constructor><![CDATA[var $ = this;
if (true === $.__constructed)
{
return;
}

this.api.logger.debug("[Vkontakte Button][" + this.button.id + "] constructor ");
$._defaultConstructor();
$._customConstructor();
$.__constructed = true;]]></constructor>


            <method name="_defaultConstructor">
                <body><![CDATA[var $ = this;
$.authorized = false;
var mailman = this.app.mailman;
mailman.addSubscriber({
subject: "user-id",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: uid}) {
$.userId = uid;
}
}});
mailman.send({
subject: "please-repeat",
body: "user-id"});
mailman.addSubscriber({
subject: "auth-granted",
group: $.BUTTON_NAME,
subscriber: {
receive: function () {
$.authorized = true;
}
}});
mailman.addSubscriber({
subject: "auth-lost",
group: $.BUTTON_NAME,
subscriber: {
receive: function () {
$.authorized = false;
}
}});
$.authorized = $.container.authorized;
mailman.addSubscriber({
subject: "please-destroy-button",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: name}) {
if (name === $.BUTTON_NAME)
{
mailman.removeSubscribersGroup($.BUTTON_NAME);
}

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


            <method name="_customConstructor">
                <body><![CDATA[]]></body>
            </method>


            <method name="log">
                <parameter name="message"/>
                <body><![CDATA[]]></body>
            </method>

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

var tooltip = this.tooltips[tooltipType];
this.button.setAttribute("tooltiptext",templater.getString(tooltip));]]></body>
            </method>

		</implementation>
    </binding>

</bindings>
