<?xml version='1.0'?>
<!DOCTYPE bindings [
<!ENTITY % locale SYSTEM "/native/fx/login.dtd">
%locale;
]>
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <binding id="yb-name-container">
        <content>
            <xul:hbox anonid="yb-login">
                <xul:label anonid="login-start"/>
                <xul:label anonid="login-middle"/>
                <xul:label anonid="login-middle-3"/>
                <xul:label anonid="login-middle-2"/>
                <xul:label anonid="login-middle-1"/>
            </xul:hbox>
        </content>

        <implementation>
            <field name="_kMaxLength">12</field>

            <method name="setText">
                <parameter name="strText"/>
                <body><![CDATA[var setLabelValue = (function _setLabelValue(type, value) {
document.getAnonymousElementByAttribute(this,"anonid","login-" + type).setAttribute("value",value);
}
).bind(this);
if (strText.length > this._kMaxLength)
{
let i = 0;
setLabelValue("start",strText[i++]);
setLabelValue("middle",strText.substr(i,i += this._kMaxLength - 5));
setLabelValue("middle-3",strText.substr(++i,1));
setLabelValue("middle-2",strText.substr(++i,1));
setLabelValue("middle-1",strText.substr(++i,1));
}
else
{
setLabelValue("start",strText[0] || "");
setLabelValue("middle",strText.substr(1));
setLabelValue("middle-3","");
setLabelValue("middle-2","");
setLabelValue("middle-1","");
}
]]></body>
            </method>
        </implementation>
    </binding>

    <binding extends="../bindings/buttons.xbl#yauth-toolbarbutton" id="yauth-toolbarbutton">
        <implementation implements="nsIObserver">
            <constructor><![CDATA[this.setAttribute("yb-second-label-off",! this._authModule.authorized);
this._setLoginLabel();
this._authModule.addListener("login-state-changed",this);]]></constructor>

            <destructor><![CDATA[this.ignoreAuth();
this._authModule.removeListener("login-state-changed",this);]]></destructor>

            
            <method name="observe">
                <parameter name="subject"/>
                <parameter name="topic"/>
                <parameter name="data"/>
                <body><![CDATA[if (! this._authModule)
return;
switch (topic) {
case this._authModule.EVENTS.AUTH_STATE_CHANGED:
if (! this._ownerWidget.loginProcess)
{
this._changeState();
}

break;
case "login-state-changed":
if (data.state != this._authModule.authdefs.LOGIN_STATES.REQUEST)
{
this._changeState();
}

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

            <method name="refreshLabel">
                <body><![CDATA[this._setLoginLabel();]]></body>
            </method>

            <method name="_changeState">
                <body><![CDATA[this._authorized = this._authModule.authorized;
var menu = this._ownerWidget.module._buildMenu();
this._ownerWidget.updateMenu(menu);
this.setAttribute("yb-second-label-off",! this._authModule.authorized);
this._setLoginLabel();]]></body>
            </method>

            <method name="_setLoginLabel">
                <body><![CDATA[if (this._authModule.authorized)
this.ybSecondLabelNode.setText(this._authModule.user.displayName);]]></body>
            </method>
        </implementation>
    </binding>
</bindings>
