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

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

    <binding extends="chrome://global/content/bindings/button.xml#button-base" id="button">
        <content>
            <xul:toolbarbutton anonid="button" class="ybar-button-text-counter" id="ybar-button-odnoklassniki-bd" oncommand="OnCommand(event)" orient="horizontal" tooltiptext="&widget.tooltip.bd;" type="menu-button">
                <xul:menupopup anonid="menu">
                    <xul:menuitem anonid="update" label="&widget.menuitem.update;"/>
                    <xul:menuseparator/>
                    <xul:menuitem anonid="menu-bd" class="menuitem-iconic" label="&widget.menuitem.bd-follow;" link="bd"/>
                    <xul:menuitem anonid="menu-bd-hd" class="menuitem-iconic" label="&widget.menuitem.bd-hd-follow;" link="bd-hd"/>
                    <xul:menuseparator anonid="sep" hidden="true"/>
                </xul:menupopup>
            </xul:toolbarbutton>
        </content>
        <implementation implements="nsISupports, nsIDOMEventListener">
            <field name="odnoklassniki">this.parentNode.app.odnoklassniki</field>
            <field name="settings">this.parentNode.app.settings</field>
            <field name="dynamicNodes">[]</field>
            <field name="button">document.getAnonymousElementByAttribute(this, "anonid", "button")</field>
            <field name="sep">document.getAnonymousElementByAttribute(this, "anonid", "sep")</field>
            <field name="menu">document.getAnonymousElementByAttribute(this, "anonid", "menu")</field>
            <field name="menu_bd">document.getAnonymousElementByAttribute(this, "anonid", "menu-bd")</field>
            <field name="menu_bd_hd">document.getAnonymousElementByAttribute(this, "anonid", "menu-bd-hd")</field>
            <field name="observerService">Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);</field>
            
            <constructor><![CDATA[var $ = this;
if (this.settings.counters)
{
this.SetCounter(this.settings.counters.bd);
}

this.EventListener = {
observe: function (subj, topic, data) {
if (topic == "odnoklassniki-bd-updated")
{
$.UpdateMenu();
}

}
};
this.observerService.addObserver(this.EventListener,"odnoklassniki-bd-updated",false);
this.UpdateMenu();]]></constructor>
            
            <destructor><![CDATA[try {
this.observerService.removeObserver(this.EventListener,"odnoklassniki-bd-updated");
}
catch (e) {

}
]]></destructor>
            
            <method name="OnCommand">
                <parameter name="event"/>
                <body><![CDATA[var anonid = event.target.getAttribute("anonid","");
var link = event.target.getAttribute("link","");
switch (anonid) {
case "button":

case "menu-bd":

case "menu-bd-hd":
this.odnoklassniki.Navigate(link);
break;
case "update":
this.odnoklassniki.ShowThrobber("bd");
this.odnoklassniki.Update();
break;
case "menuitem":
this.odnoklassniki.Navigate(link,true);
break;
}
]]></body>
            </method>
            
            <method name="SetCounter">
                <parameter name="counter"/>
                <body><![CDATA[this.button.label = counter ? counter : "";
this.button.setAttribute("throbber",false);]]></body>
            </method>
            
            <method name="GetHumanDate">
                <parameter name="date"/>
                <body><![CDATA[
                    var decls = ("]]>&widget.info.age;<![CDATA[").split(',');
                    var months = ("]]>&widget.info.months;<![CDATA[").split(',');
                    var x = Math.abs(date.age);
                    var intX = parseInt(x);
                    if (intX != x) {
                        x = 2;
                    }
                    var age = x + ' ' + decls[(x % 100 > 4 && x % 100 < 20) ? 2 : [2, 0, 1, 1, 1, 2][Math.min(x % 10, 5)]];
                    return date.day + ' ' + months[date.month - 1] + ' (' + age + ')';
                ]]></body>
            </method>
            
            <method name="UpdateMenu">
                <body><![CDATA[
                    var bd = this.settings.bd;
                    var hasHD;
                    try {
                        hasHD = this.settings.counters.bd > 0;
                    } catch(e) {
                        hasHD = false;
                    }
                    
                    this.menu_bd_hd.setAttribute('hidden', !hasHD);
                    this.button.setAttribute('link', hasHD ? 'bd-hd' : 'bd');
                    this.button.setAttribute('tooltiptext', hasHD ? "]]>&widget.tooltip.bd-hd;<![CDATA[" : "]]>&widget.tooltip.bd;<![CDATA[");
                    
                    
                    
                    var show = bd.length > 0;
                    this.sep.setAttribute('hidden', !show);
                    
                    for(var i in this.dynamicNodes) {
                        var node = this.dynamicNodes[i];
                        this.menu.removeChild(node);
                    }
                    
                    this.dynamicNodes = [];
                    
                    if(!bd) {
                        return;
                    }
                    
                    for(var i in bd) {
                        var msg = bd[i];
                        if(msg && msg.name && msg.url && msg.date) {
                            var node = document.createElement("menuitem");
                            node.setAttribute('label', msg.name);
                            node.setAttribute('link', msg.url);
                            node.setAttribute('acceltext', '  ' + this.GetHumanDate(msg.date));
                            node.setAttribute('anonid', "menuitem");
                            node.setAttribute('oncomand', "OnCommand(event)");
                            node.setAttribute('hd', msg.hd);
                            node.className = "menuitem-iconic";
                            this.menu.appendChild(node);
                            this.dynamicNodes.push(node);
                        }
                    }
                ]]></body>
            </method>
            
        </implementation>
    </binding>
</bindings>