<?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="//toolkit/native/bindings/widget.xml#widget" id="vkontakte">
        <content>
            <xul:box class="yb-vkontakte-placeholder yb-vkontakte-optional" id="yb-vkontakte-profile-placeholder"/>
            <xul:box active="false" anonid="messages" class="yb-vkontakte-placeholder yb-vkontakte-optional" id="yb-vkontakte-messages-placeholder"/>
            <xul:box active="false" anonid="friends" class="yb-vkontakte-placeholder yb-vkontakte-optional" id="yb-vkontakte-friends-placeholder"/>
            <xul:box active="false" anonid="photos" class="yb-vkontakte-placeholder yb-vkontakte-optional" id="yb-vkontakte-photos-placeholder"/>
            <xul:box active="false" anonid="video" class="yb-vkontakte-placeholder yb-vkontakte-optional" id="yb-vkontakte-video-placeholder"/>
            <xul:box active="false" anonid="groups" class="yb-vkontakte-placeholder yb-vkontakte-optional" id="yb-vkontakte-groups-placeholder"/>
            <xul:box active="false" anonid="notes" class="yb-vkontakte-placeholder yb-vkontakte-optional" id="yb-vkontakte-notes-placeholder"/>
        </content>

        <implementation>
            <field name="__constructed">false</field>
            <field name="__destructed">false</field>
            <field name="authorized">false</field>
            <field name="KNOWN_BUTTONS" readonly="true">[
                'messages',
                'friends',
                'photos',
                'video',
                'groups',
                'notes'
            ]</field>

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

$.__constructor();
$.__constructed = true;]]></constructor>

			<method name="__finalizeMailman">
				<parameter name="fromApp"/>
                <body><![CDATA[var $ = this;
if ($.__destructed)
{
return;
}

if ($.application)
{
let mailman = $.application.mailman;
$.KNOWN_BUTTONS.concat(["profile"]).forEach(function (buttonName) {
mailman.send({
subject: "please-destroy-button",
body: buttonName});
}
);
}

$.__destructed = true;]]></body>
            </method>

	        <destructor><![CDATA[this.__finalizeMailman && this.__finalizeMailman(false);]]></destructor>


            <method name="__constructor">
                <body><![CDATA[var $ = this;
var wiid = this.application.wiid;
$.KNOWN_BUTTONS.forEach(function (buttonName) {
var settingName = "display-button-" + buttonName;
var isActive = $.application.api.Settings.getValue(settingName,wiid);
$.updateButtonState(buttonName,isActive);
}
);
var mailman = this.application.mailman;
mailman.addSubscriber({
subject: "setting-changed",
subscriber: {
receive: function ({body: data}) {
var {name: settingName, value: isActive} = data;
if (0 !== settingName.indexOf("display-button-"))
{
return;
}

var name = settingName.replace("display-button-","");
$.updateButtonState(name,isActive);
}
}});
mailman.addSubscriber({
subject: "auth-granted",
subscriber: {
receive: function () {
$.authorized = true;
}
}});
mailman.addSubscriber({
subject: "auth-lost",
subscriber: {
receive: function () {
$.authorized = false;
}
}});
mailman.send("please-check-auth");]]></body>
            </method>

            <method name="updateButtonState">
                <parameter name="name"/>
                <parameter name="isActive"/>
                <body><![CDATA[var button = document.getAnonymousElementByAttribute(this,"anonid",name);
var mailman = this.application.mailman;
if (false === isActive)
{
mailman.send({
subject: "please-destroy-button",
body: name});
}

button.setAttribute("active",isActive ? "true" : "false");
mailman.send({
subject: "button-state-update",
body: {
name: name,
isActive: isActive}});]]></body>
            </method>
        </implementation>

    </binding>
</bindings>
