<?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="profile">
        <content>
            <xul:toolbarbutton action="open-page" anonid="button" class="chromeclass-toolbar-additional toolbarbutton-1 yb-vkontakte-button" has-updates="false" id="yb-vkontakte-profile" label="" page="profile" type="menu-button" vk-type="profile">
                <xul:menupopup anonid="menu" class="yb-vkontakte-profile-menu">
                    <xul:menuitem action="update-all-manual" label="&vkontakte.profile.menu.update;"/>
                    <xul:menuseparator/>

                    <xul:menuitem action="open-page" class="menuitem-iconic yb-vkontakte-profile-page" label="&vkontakte.profile.menu.profile-page;" page="profile"/>
                    <xul:menuseparator/>

                    <xul:menuitem action="open-page" active="false" anonid="messages" class="menuitem-iconic yb-vkontakte-profile-messages yb-vkontakte-optional" label="&vkontakte.profile.menu.messages;" optional="true" page="messages"/>
                    <xul:menuitem action="open-page" active="false" anonid="friends" class="menuitem-iconic yb-vkontakte-profile-friends yb-vkontakte-optional" label="&vkontakte.profile.menu.friends;" optional="true" page="friends"/>
                    <xul:menuitem action="open-page" active="false" anonid="events" class="menuitem-iconic yb-vkontakte-profile-events yb-vkontakte-optional" label="&vkontakte.profile.menu.events;" optional="true" page="events"/>
                    <xul:menuitem action="open-page" active="false" anonid="groups" class="menuitem-iconic yb-vkontakte-profile-groups yb-vkontakte-optional" label="&vkontakte.profile.menu.groups;" optional="true" page="groups"/>
                    <xul:menuitem action="open-page" active="false" anonid="photos" class="menuitem-iconic yb-vkontakte-profile-photos yb-vkontakte-optional" label="&vkontakte.profile.menu.photos;" optional="true" page="photos"/>
                    <xul:menuitem action="open-page" active="false" anonid="video" class="menuitem-iconic yb-vkontakte-profile-video yb-vkontakte-optional" label="&vkontakte.profile.menu.video;" optional="true" page="video"/>
                    <xul:menuitem action="open-page" active="false" anonid="notes" class="menuitem-iconic yb-vkontakte-profile-notes yb-vkontakte-optional" label="&vkontakte.profile.menu.notes;" optional="true" page="notes"/>
                    <xul:menuseparator anonid="optional-buttons-separator" class="yb-vkontakte-optional"/>

                    <xul:menuitem action="open-page" class="menuitem-iconic yb-vkontakte-profile-audio" label="&vkontakte.profile.menu.audio;" page="audio"/>
                    <xul:menuitem action="open-page" class="menuitem-iconic yb-vkontakte-profile-news" label="&vkontakte.profile.menu.news;" page="news"/>
                    <xul:menuitem action="open-page" class="menuitem-iconic yb-vkontakte-profile-bookmarks" label="&vkontakte.profile.menu.bookmarks;" page="bookmarks"/>
                    <xul:menuseparator/>

                    
                    
                    <xul:menuitem action="open-page" class="menuitem-iconic yb-vkontakte-profile-applications" label="&vkontakte.profile.menu.applications;" page="applications"/>
                    
                    <xul:menuseparator/>

                    <xul:menuitem action="open-settings" label="&vkontakte.profile.menu.settings;"/>
                    <xul:menuseparator/>

                    <xul:menuitem action="logout" label="&vkontakte.profile.menu.logout;"/>

                </xul:menupopup>
            </xul:toolbarbutton>
        </content>

		<implementation>
            <field name="BUTTON_NAME" readonly="true">'profile'</field>

            <field name="optionalButtonsSeparator" readonly="true">
                document.getAnonymousElementByAttribute(this, "anonid", "optional-buttons-separator")
            </field>

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

            <filed name="_optionalMenuItems">null</filed>
            <property name="optionalMenuItems">
                <getter><![CDATA[if (null == this._optionalMenuItems)
{
let items = [];
for (let i = this.menu.childNodes.length - 1;i > 0;--i) {
let item = this.menu.childNodes[i];
if (Node.ELEMENT_NODE == item.nodeType && item.hasAttribute("optional"))
{
items.push(item);
}

}

this._optionalMenuItems = items;
}

return this._optionalMenuItems;]]></getter>
            </property>

            <field name="tooltips" readonly="true">
                ({
                    'need_auth': 'vkontakte.profile.tooltip.need_auth',
                    'normal': 'vkontakte.profile.tooltip.normal'
                })
            </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.type = "menu-button";
this.button.setAttribute("authorized","true");
this.displayUsername = this.app.api.Settings.getValue("display-username",this.app.wiid);
this.updateTooltip();
}
else
if (false === val)
{
this.button.type = "button";
this.button.setAttribute("authorized","false");
this.button.setAttribute("has-updates","false");
this.updateTooltip();
this.displayUsername = false;
this._username = "";
this._userId = 0;
}
]]></setter>
            </property>

            <field name="_username">''</field>
            <property name="displayUsername">
                <setter><![CDATA[if (false === val)
{
this.button.label = "";
return;
}

if ("" !== this._username)
{
this.button.label = this._username;
}
else
{
let mailman = this.app.mailman;
var button = this;
var uid = this.userId;
if (0 === uid)
{
return;
}

var subscriber = {
receive: function ({body: data}) {
var userdata = data[0];
if (uid !== userdata.uid)
return;
mailman.removeSubscriber({
subject: "profiles",
subscriber: subscriber});
var username = userdata.first_name + " " + userdata.last_name;
button._username = username;
button.button.label = username;
}
};
mailman.addSubscriber({
subject: "profiles",
group: this.BUTTON_NAME,
subscriber: subscriber});
mailman.send({
subject: "please-get-profiles",
body: uid});
}
]]></setter>
            </property>

            <field name="counters">
                ({
                    messages: {value: 0, use: false},
                    friends: {value: 0, use: false}
                })
            </field>

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

            <method name="_customConstructor">
                <body><![CDATA[var $ = this;
const KNOWN_BUTTONS = $.container.KNOWN_BUTTONS;
var counters = this.app.counters;
for(var counterId in this.counters) {
$.updateCounter(counterId,counters.get(counterId));
}

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

}
}});
var wiid = this.app.wiid;
KNOWN_BUTTONS.forEach(function (id) {
var settingName = "display-button-" + id;
var isButtonActive = $.app.api.Settings.getValue(settingName,wiid);
$.switchMenuitem(id,! isButtonActive);
}
);
mailman.addSubscriber({
subject: "button-state-update",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: data}) {
if ($.switchMenuitem)
{
$.switchMenuitem(data.name,! data.isActive);
}

}
}});
mailman.addSubscriber({
subject: "setting-changed",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: setting}) {
if ("display-username" == setting.name)
{
$.displayUsername = setting.value;
}

}
}});
var stopWaiting = {
_counters: [],
receive: function ({subject: subject, body: data}) {
var id = data.id || data;
if (this._counters.indexOf(id) == - 1)
{
this._counters.push(id);
}

const COUNTERS_NUMBER = 2;
if (COUNTERS_NUMBER === this._counters.length)
{
$.waiting = false;
this._counters = [];
mailman.removeSubscriber({
subject: "counter-update",
subscriber: stopWaiting});
mailman.removeSubscriber({
subject: "request-failed",
subscriber: stopWaiting});
}

}
};
mailman.addSubscriber({
subject: "please-update-all-manual",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: target}) {
$.waiting = true;
var mailman = $.app ? $.app.mailman : null;
if (mailman)
{
mailman.addSubscriber({
subject: "counter-update",
group: $.BUTTON_NAME,
subscriber: stopWaiting});
mailman.addSubscriber({
subject: "request-failed",
group: $.BUTTON_NAME,
subscriber: stopWaiting});
mailman.send({
subject: "please-update",
body: "all"});
}

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

            <field name="_items">({})</field>
            <method name="getItem">
                <parameter name="itemName"/>
                <body><![CDATA[if (! this._items[itemName])
{
this._items[itemName] = document.getAnonymousElementByAttribute(this,"anonid",itemName);
}

return this._items[itemName];]]></body>
            </method>


            <method name="updateCounter">
                <parameter name="counterId"/>
                <parameter name="value"/>
                <body><![CDATA[var newValue = "";
if (value > 0)
{
newValue = value;
}

var item = this.getItem(counterId);
item.setAttribute("acceltext",newValue);
var counter = this.counters[counterId];
counter.value = value;
this.updateMainButtonState();]]></body>
            </method>

            <method name="updateMainButtonState">
                <body><![CDATA[this.button.setAttribute("has-updates","false");
[counter for each (counter in this.counters) if (counter.value > 0 && true === counter.use)].some(function () {
this.button.setAttribute("has-updates","true");
return true;
}
,this);]]></body>
            </method>

            <method name="switchMenuitem">
                <parameter name="anonid"/>
                <parameter name="isActive"/>
                <body><![CDATA[var menuItem = this.getItem(anonid);
if (null !== menuItem)
{
menuItem.setAttribute("active",isActive ? "true" : "false");
this.updateSeparatorState();
let counterId = anonid;
let counter = this.counters[counterId];
if ("undefined" != typeof counter)
{
counter.use = isActive;
this.updateMainButtonState();
}

}
]]></body>
            </method>

            <method name="updateSeparatorState">
                <body><![CDATA[var separatorIsActive = "false";
var someMenuItemIsActive = false;
var optionalMenuItemsLength = this.optionalMenuItems.length;
for (let i = 0, len = optionalMenuItemsLength;i < len;i++) {
let item = this.optionalMenuItems[i];
let isActive = ! item.hasAttribute("active") || "true" === item.getAttribute("active");
if (isActive)
{
someMenuItemIsActive = true;
break;
}

}

if (someMenuItemIsActive)
{
separatorIsActive = "true";
}

this.optionalButtonsSeparator.setAttribute("active",separatorIsActive);]]></body>
            </method>

		</implementation>
    </binding>

</bindings>
