<?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="//toolkit/native/bindings/widget.xml#widget" id="toolbarbutton">
        <content>
            <xul:toolbarbutton anonid="button" class="toolbarbutton-1 chromeclass-toolbar-additional" id="ybar-button-odnoklassniki" oncommand="OnCommand(event)" style="padding: 0  !important" tooltip="ok-tooltip-off" type="menu-button">
                <xul:menupopup anonid="menupopup">
                    <xul:menuitem anonid="menu-update" label="&widget.menuitem.update;"/>
                    <xul:menuseparator/>
                    <xul:menuitem anonid="menu-home" label="&widget.menuitem.home;"/>
                    <xul:menuseparator/>
                    <xul:menuitem anonid="menu-messages" class="menuitem-iconic" label="&widget.menuitem.messages;" link="messages"/>
                    <xul:menuitem anonid="menu-notifications" class="menuitem-iconic" label="&widget.menuitem.notifications;" link="notifications"/>
                    <xul:menuitem anonid="menu-guests" class="menuitem-iconic" label="&widget.menuitem.guests;" link="guests"/>
                    <xul:menuitem anonid="menu-marks" class="menuitem-iconic" label="&widget.menuitem.marks;" link="marks"/>
                    <xul:menuitem anonid="menu-bd2" class="menuitem-iconic" label="&widget.menuitem.bd2;" link="bd2"/>
                    <xul:menuitem anonid="menu-bd" class="menuitem-iconic" label="&widget.menuitem.bd;" link="bd"/>
                    <xul:menuseparator/>
                    <xul:menuitem anonid="menu-settings" label="&widget.menuitem.settings;"/>
                    <xul:menuseparator/>
                    <xul:menuitem anonid="menu-quit" label="&widget.menuitem.quit;"/>
                </xul:menupopup>
            </xul:toolbarbutton>
            
            <xul:toolbarbutton anonid="button-messages" class="toolbarbutton-1 button-messages auth-button-odnoklassniki"/>
            <xul:toolbarbutton anonid="button-notifications" class="toolbarbutton-1 button-notifications auth-button-odnoklassniki"/>
            <xul:toolbarbutton anonid="button-guests" class="toolbarbutton-1 button-guests auth-button-odnoklassniki"/>
            <xul:toolbarbutton anonid="button-marks" class="toolbarbutton-1 button-marks auth-button-odnoklassniki"/>
            <xul:toolbarbutton anonid="button-bd" class="toolbarbutton-1 button-bd auth-button-odnoklassniki" id="ybar-ok-button-hbd"/>
            
            <xul:tooltip class="ybar-ok-default-tooltip" id="ok-tooltip-on">
                <xul:label class="text-bold" show="true" value="&widget.name;:"/>
                <xul:grid>
                    <xul:columns>
                        <xul:column flex="1"/>
                        <xul:column flex="2"/>
                    </xul:columns>
                    <xul:rows>
                        <xul:row>
                            <xul:cell><xul:label anonid="tooltip-messages"/></xul:cell>
                            <xul:cell><xul:label anonid="tooltip-messages-count"/></xul:cell>
                        </xul:row>
                        <xul:row>
                            <xul:cell><xul:label anonid="tooltip-notifications"/></xul:cell>
                            <xul:cell><xul:label anonid="tooltip-notifications-count"/></xul:cell>
                        </xul:row>
                        <xul:row>
                            <xul:cell><xul:label anonid="tooltip-guests"/></xul:cell>
                            <xul:cell><xul:label anonid="tooltip-guests-count"/></xul:cell>
                        </xul:row>
                        <xul:row>
                            <xul:cell><xul:label anonid="tooltip-bd"/></xul:cell>
                            <xul:cell><xul:label anonid="tooltip-bd-count"/></xul:cell>
                        </xul:row>
                    </xul:rows>
                </xul:grid>
            </xul:tooltip>
            
            <xul:tooltip class="ybar-ok-default-tooltip" id="ok-tooltip-off">
                <xul:label value="&widget.tooltip.no-auth1;"/>
                <xul:label value="&widget.tooltip.no-auth2;"/>
            </xul:tooltip>
            
            <xul:tooltip class="ybar-ok-default-tooltip" id="ok-tooltip-empty">
                <xul:label value="&widget.menuitem.home;"/>
            </xul:tooltip>
        </content>
        <implementation implements="nsISupports, nsIDOMEventListener">
            <field name="api">this.app.api</field>
            <field name="odnoklassniki">this.app.odnoklassniki</field>
            <field name="button">document.getAnonymousElementByAttribute(this, "anonid", "button")</field>
            <field name="button_messages">document.getAnonymousElementByAttribute(this, "anonid", "button-messages")</field>
            <field name="button_notifications">document.getAnonymousElementByAttribute(this, "anonid", "button-notifications")</field>
            <field name="button_guests">document.getAnonymousElementByAttribute(this, "anonid", "button-guests")</field>
            <field name="button_marks">document.getAnonymousElementByAttribute(this, "anonid", "button-marks")</field>
            <field name="button_bd">document.getAnonymousElementByAttribute(this, "anonid", "button-bd")</field>
            <field name="menu_update">document.getAnonymousElementByAttribute(this, "anonid", "menu-update")</field>
            <field name="menu_home">document.getAnonymousElementByAttribute(this, "anonid", "menu-home")</field>
            <field name="menu_messages">document.getAnonymousElementByAttribute(this, "anonid", "menu-messages")</field>
            <field name="menu_notifications">document.getAnonymousElementByAttribute(this, "anonid", "menu-notifications")</field>
            <field name="menu_guests">document.getAnonymousElementByAttribute(this, "anonid", "menu-guests")</field>
            <field name="menu_marks">document.getAnonymousElementByAttribute(this, "anonid", "menu-marks")</field>
            <field name="menu_bd2">document.getAnonymousElementByAttribute(this, "anonid", "menu-bd2")</field>
            <field name="menu_bd">document.getAnonymousElementByAttribute(this, "anonid", "menu-bd")</field>
            <field name="menu_bdhd">document.getAnonymousElementByAttribute(this, "anonid", "menu-bd-hd")</field>
            <field name="menu_settings">document.getAnonymousElementByAttribute(this, "anonid", "menu-settings")</field>
            <field name="menu_quit">document.getAnonymousElementByAttribute(this, "anonid", "menu-quit")</field>
            <field name="tt_messages">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-messages")</field>
            <field name="tt_notifications">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-notifications")</field>
            <field name="tt_guests">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-guests")</field>
            <field name="tt_bd">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-bd")</field>
            <field name="tc_messages">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-messages-count")</field>
            <field name="tc_notifications">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-notifications-count")</field>
            <field name="tc_guests">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-guests-count")</field>
            <field name="tc_bd">document.getAnonymousElementByAttribute(this, "anonid", "tooltip-bd-count")</field>
            <field name="observerService">Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);</field>
            <field name="widget_settings">["show-messages", "show-notifications", "show-guests", "show-marks", "show-bd"]</field>
            
            <constructor><![CDATA[var $ = this;
this.odnoklassniki.widgetInstaces++;
var settingsListener = {
onSettingChange: function onSettingChange(name, value) {
$.OnSettingChange(name.replace("show-",""),value);
}
};
this.OnUpdateListener = {
observe: function (subj, topic, data) {
if (topic == "odnoklassniki-counters-updated")
{
try {
$.UpdateToolbar();
}
catch (e) {

}

}
else
if (topic == "odnoklassniki-update-failed")
{
$.button.setAttribute("throbber",false);
["messages", "notifications", "guests", "marks", "bd"].forEach(function (name) {
var buttonName = "button_" + name;
var button = $[buttonName];
if ("undefined" === typeof button)
{
return;
}

if (button.button)
{
button.button.setAttribute("throbber",false);
}

}
);
}
else
if (topic == "odnoklassniki-throbber")
{
$.ShowThrobber(data);
}

}
};
this.api.Settings.observeChanges(settingsListener);
try {
for(var i in this.widget_settings) {
this.OnSettingChange(this.widget_settings[i].replace("show-",""),this.api.Settings.getValue(this.widget_settings[i]));
}

}
catch (e) {

}

this.observerService.addObserver(this.OnUpdateListener,"odnoklassniki-throbber",false);
this.observerService.addObserver(this.OnUpdateListener,"odnoklassniki-counters-updated",false);
this.observerService.addObserver(this.OnUpdateListener,"odnoklassniki-update-failed",false);
this.UpdateToolbar();]]></constructor>
            
            <method name="ExDestructor">
                <body><![CDATA[this.odnoklassniki.widgetInstaces--;
if (this.odnoklassniki.widgetInstaces == 0)
{

}

try {
this.api.Settings.ignoreChanges();
this.observerService.removeObserver(this.OnUpdateListener,"odnoklassniki-throbber");
this.observerService.removeObserver(this.OnUpdateListener,"odnoklassniki-counters-updated");
this.observerService.removeObserver(this.OnUpdateListener,"odnoklassniki-update-failed");
}
catch (e) {

}
]]></body>
            </method>
			
			<method name="ShowThrobber">
                <parameter name="button"/>
                <body><![CDATA[if (button == "main")
{
button = this.button;
}
else
{
button = this["button_" + button];
}

button.setAttribute("throbber",true);
if (button.button)
{
button.button.setAttribute("throbber",true);
}
]]></body>
            </method>
            
            <property name="isAuth">
                <getter><![CDATA[return this.odnoklassniki.isAuth;]]></getter>
            </property>
            
            <method name="OnSettingChange">
                <parameter name="name"/>
                <parameter name="value"/>
                <body><![CDATA[if (name != "update-interval" && name != "login")
{
this["button_" + name].setAttribute("auth",value && this.odnoklassniki.isAuth);
}
]]></body>
            </method>
            
            <method name="OnCommand">
                <parameter name="event"/>
                <body><![CDATA[switch (event.target) {
case this.button:

case this.menu_home:
if (this.isAuth)
{
this.odnoklassniki.Navigate("home");
}
else
{
this.odnoklassniki.Auth();
}

break;
case this.menu_update:
this.odnoklassniki.ShowThrobber();
this.odnoklassniki.Update();
break;
case this.menu_messages:

case this.menu_notifications:

case this.menu_guests:

case this.menu_marks:

case this.menu_bdhd:

case this.menu_bd2:

case this.menu_bd:
this.odnoklassniki.Navigate(event.target.getAttribute("link",""));
break;
case this.menu_settings:
this.api.Controls.openSettingsDialog(window,this.app.WIID);
break;
case this.menu_quit:
this.odnoklassniki.RemoveCookies();
break;
}
]]></body>
            </method>
            
            <method name="UpdateToolbar">
                <body><![CDATA[
                    var $ = this;
                    var counters = this.app.settings.counters;
                    var newEvents = false;
                    var allNewEvents = false;
                    var event_types = {'messages':"]]>&widget.menuitem.messages;<![CDATA[",
                                       'notifications':"]]>&widget.menuitem.notifications;<![CDATA[",
                                       'guests':"]]>&widget.menuitem.guests;<![CDATA[",
                                       'marks':"]]>&widget.menuitem.marks;<![CDATA[",
                                       'bd2':"]]>&widget.menuitem.bd2;<![CDATA[",
                                       'bd':"]]>&widget.menuitem.bd;<![CDATA["};
                
                    this.button.setAttribute('auth', this.isAuth);
                    this.button.type= this.isAuth ? "menu-button" : "";
                    
                    for(var i in this.widget_settings) {
                        this.OnSettingChange(this.widget_settings[i].replace("show-",''), this.api.Settings.getValue(this.widget_settings[i]));
                    }
                    
                    for(var j in counters) {
                        var oButton = this['button_'+j];
                        var hasNew = Number(counters[j]) > 0;
                        
                        if(hasNew) {
                            allNewEvents = true;
                        }
                        
                        if(hasNew && j != 'bd') {
                            newEvents = true;
                        }
                        
                        var tt = this['tt_'+j];
                        var tc = this['tc_'+j];
                        var key = (j == 'bd' ? 'bd2' : j);
                        
                        if(tt && tc) {
                            tt.setAttribute("show", hasNew);
                            tc.setAttribute("show", hasNew);
                            tt.value = hasNew ? event_types[key] : '';
                            tc.value = hasNew ? counters[j] : '';
                        }
                        
                        if(oButton) {
                            if(oButton == this.button_bd) {
                                this.button_bd.setAttribute('hd', hasNew);
                            }
                            
                            if(this.updateTimer) {
                                try {
                                    this.updateTimer.cancel();
                                } catch(e) {}
                            }
                            
                            if(oButton.SetCounter) {
                                oButton.SetCounter(counters[j]);
                            } else {
                                this.updateTimer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
                                this.updateTimer.initWithCallback(function() { $.UpdateToolbar($); }, 1000 , Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
                            }
                        }
                        
                        var mi = this['menu_'+key];
                        if(mi) {
                            mi.setAttribute("acceltext", counters[j] ? counters[j] : '');
                            if(mi == this.menu_bd2) {
                                mi.label = hasNew ? "]]>&widget.menuitem.bd2;<![CDATA[" : "]]>&widget.menuitem.bd-hd;<![CDATA[";
                            }
                        }
                    }
                    
                    this.button.tooltip = this.isAuth ? (allNewEvents ? "ok-tooltip-on" : "ok-tooltip-empty") : "ok-tooltip-off";
                    this.button.setAttribute("newEvents", newEvents);
                    this.button.setAttribute("throbber", false);
                ]]></body>
            </method>
            
        </implementation>
    </binding>
    
    <binding id="authform">
        <content>
            <xul:vbox flex="1">
                <xul:browser anonid="browser" flex="1" type="content"/>
            </xul:vbox>
        </content>
        <implementation>
            <field name="browser">document.getAnonymousElementByAttribute(this, "anonid", "browser")</field>
            <constructor><![CDATA[var args = window.arguments[0];
var browser0 = this.browser;
if (args.init)
{
args.init.call(this,browser);
}

if (args.onload)
{
window.addEventListener("load",args.onload,false);
}

if (args.onclose)
{
window.addEventListener("close",args.onclose,false);
}

if (args.progressListener)
{
browser0.addProgressListener(args.progressListener,args.notifyMask);
}

var doRemoveDiv = true;
var observer = {
observe: function (subj, topic, data) {
browser0.loadURI(args.url);
browser0.addEventListener("DOMContentLoaded",function () {
if (doRemoveDiv)
{
var doc = browser0.contentDocument;
if (doc)
{
var div = doc.getElementById("smallTopNotificationPanel");
if (div)
{
div.style.display = "none";
doRemoveDiv = false;
}

}

}

}
,false);
window.focus();
}
};
var timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
timer.init(observer,1,timer.TYPE_ONE_SHOT);]]></constructor>
            <destructor><![CDATA[var args = window.arguments[0];
if (args.onclose)
{
try {
window.removeEventListener("close",args.onclose,false);
}
catch (e) {

}

}

if (args.onload)
{
try {
window.removeEventListener("load",args.onload,false);
}
catch (e) {

}

}
]]></destructor>
        </implementation>
    </binding>
</bindings>

