<?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.counter.xbl#button" id="friends">
        <content>
            <xul:toolbarbutton action="open-page" anonid="button" class="chromeclass-toolbar-additional toolbarbutton-1 yb-vkontakte-button" id="yb-vkontakte-friends" label="" page="friends" type="menu-button" vk-type="friends">
                <xul:menupopup anonid="menu">
                    <xul:menuitem action="update" label="&vkontakte.friends.menu.update;" update="friends"/>
                    
                    <xul:menuseparator anonid="separator"/>
                    <xul:menuitem action="open-page" active="false" anonid="new-friends" class="yb-vkontakte-optional" label="&vkontakte.friends.menu.new-friends;" page="friends"/>
                    <xul:menuitem action="open-page" label="&vkontakte.friends.menu.friends;" page="friends_all"/>
                </xul:menupopup>
            </xul:toolbarbutton>
        </content>

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

            <field name="tooltips" readonly="true">
                ({
                    'need_auth': 'vkontakte.friends.tooltip.need_auth',
                    'normal': 'vkontakte.friends.tooltip.normal',
                    'counter': {
                        'form_1': 'vkontakte.friends.tooltip.new.form_1',
                        'form_2': 'vkontakte.friends.tooltip.new.form_2',
                        'form_5': 'vkontakte.friends.tooltip.new.form_5'
                    }
                })
            </field>

            <field name="newFriendsItem">document.getAnonymousElementByAttribute(this, "anonid", "new-friends")</field>

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

            <method name="_customConstructor">
                <body><![CDATA[var $ = this;
$._counterButtonConstructor();
var mailman = this.app.mailman;
mailman.addSubscriber({
subject: "new-friends",
group: $.BUTTON_NAME,
subscriber: {
receive: function ({body: users}) {
$.waiting = false;
if ($.updateFriendsRequests)
{
$.updateFriendsRequests(users);
$.updateTooltip();
}

}
}});
mailman.send({
subject: "please-repeat",
body: "new-friends"});]]></body>
            </method>

            <method name="updateFriendsRequests">
                <parameter name="users"/>
                <body><![CDATA[this.clearFriendsRequests();
if (0 === users.total)
{
this.hasNewFriendsRequests = false;
}
else
{
this.hasNewFriendsRequests = true;
this.showRequestsFrom(users.data);
}
]]></body>
            </method>

            <method name="clearFriendsRequests">
                <body><![CDATA[var $ = this;
for (let i = $.menu.childNodes.length - 1;i > 0;--i) {
let item = $.menu.childNodes[i];
if (Node.ELEMENT_NODE == item.nodeType && item.hasAttribute("isRequest"))
{
$.menu.removeChild(item);
}

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

            <method name="showRequestsFrom">
                <parameter name="users"/>
                <body><![CDATA[var $ = this;
var separator = document.createElement("menuseparator");
separator.setAttribute("isRequest","true");
$.menu.insertBefore(separator,$.separator);
users.forEach(function (user) {
var item = $.createRequestItem(user);
$.menu.insertBefore(item,$.separator);
}
);]]></body>
            </method>

            <method name="createRequestItem">
                <parameter name="userData"/>
                <body><![CDATA[var {uid: uid, _profile: profile} = userData;
var username = profile.first_name + " " + profile.last_name;
var item = document.createElement("menuitem");
item.setAttribute("label",username);
item.setAttribute("action","open-url");
item.setAttribute("url",this.getUserLinkById(uid));
item.setAttribute("isRequest","true");
return item;]]></body>
            </method>


            <method name="getUserLinkById">
                <parameter name="id"/>
                <body><![CDATA[return "http://vk.com/id" + id;]]></body>
            </method>

		</implementation>
    </binding>

</bindings>
