<?xml version='1.0'?>
<!DOCTYPE bindings [
<!ENTITY % logo SYSTEM "/native/fx/logo.dtd">
%logo;
]>

<bindings 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="logo">
        <content>
            <children/>
            <xul:hbox anonid="logo-container"/>
        </content>

        <implementation implements="nsIObserver, nsIDOMEventListener">
            <field name="mAppName" readonly="true"><![CDATA[this.module.API.Environment.barPlatform.name;]]></field>

            <field name="mLogoContainer" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","logo-container");]]></field>

            <field name="mObserverService" readonly="true"><![CDATA[Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);]]></field>

            <field name="mToolbox" readonly="true"><![CDATA[document.getElementById("navigator-toolbox");]]></field>

            <field name="isDefaultTheme" readonly="true"><![CDATA[this.mToolbox.getAttribute("cb-default-theme") === "true";]]></field>

            <method name="logoConstructor">
                <body><![CDATA[this.mLogoContainer.module = this.module;
setTimeout(function (that) that._checkPosition(),0,this);
this.mObserverService.addObserver(this,"lightweight-theme-changed",false);
this.mToolbox.addEventListener(this.mAppName + "-beforecustomization",this,false);
this.mToolbox.addEventListener(this.mAppName + "-aftercustomization",this,false);]]></body>
            </method>

            <method name="logoDestructor">
                <body><![CDATA[this._removeElement();
this.mObserverService.removeObserver(this,"lightweight-theme-changed",false);
this.mToolbox.removeEventListener(this.mAppName + "-beforecustomization",this,false);
this.mToolbox.removeEventListener(this.mAppName + "-aftercustomization",this,false);]]></body>
            </method>

            <method name="observe">
                <parameter name="aSubject"/>
                <parameter name="aTopic"/>
                <parameter name="aData"/>
                <body><![CDATA[switch (aTopic) {
case "lightweight-theme-changed":
this._setStyles(this.container);
break;
default:
break;
}
]]></body>
            </method>

            <method name="handleEvent">
                <parameter name="aEvent"/>
                <body><![CDATA[switch (aEvent.type) {
case this.mAppName + "-beforecustomization":
this.mToolbox.setAttribute("cb-customizing","true");
this.removeAttribute("cb-empty");
this._removeElement();
break;
case this.mAppName + "-aftercustomization":
this.mToolbox.removeAttribute("cb-customizing");
this._checkPosition();
break;
default:
break;
}
]]></body>
            </method>

            <method name="_checkPosition">
                <body><![CDATA[if (this.mToolbox.getAttribute("cb-customizing") === "true")
return;
var urlbarContainer = document.getElementById("urlbar-container");
if (! urlbarContainer)
return;
if (this.nextSibling === urlbarContainer)
{
this.setAttribute("cb-empty",true);
this._createElement();
}
else
{
this.removeAttribute("cb-empty");
this._removeElement();
}
]]></body>
            </method>

            <method name="_createElement">
                <body><![CDATA[var urlbar = document.getElementById("urlbar");
if (! urlbar)
return;
if (this.container)
return;
var logoBox = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul","box");
var firstChild = urlbar.firstChild;
firstChild ? urlbar.insertBefore(logoBox,firstChild) : urlbar.appendChild(logoBox);
this.container = logoBox;
this.container.module = this.module;
this._setStyles(logoBox);
logoBox.setAttribute("anonid","logo-container");]]></body>
            </method>

            <method name="_setStyles">
                <parameter name="hbox"/>
                <body><![CDATA[if (! hbox)
return;
var hboxStyle = hbox.style;
var computedStyle = window.getComputedStyle(document.documentElement);
if (computedStyle)
{
hboxStyle.setProperty("background-color",computedStyle.backgroundColor,"important");
hboxStyle.setProperty("background-image",computedStyle.backgroundImage,"important");
hboxStyle.setProperty("background-repeat",computedStyle.backgroundRepeat,"important");
hboxStyle.setProperty("background-position","50% 0","important");
}

if (this.isDefaultTheme)
{
let urlbar = document.getElementById("urlbar");
let urlbarComputedStyle = window.getComputedStyle(urlbar);
hboxStyle.setProperty("border-right-style",urlbarComputedStyle.borderLeftStyle,"");
hboxStyle.setProperty("border-right-color",urlbarComputedStyle.borderLeftColor,"");
hboxStyle.setProperty("border-right-width",urlbarComputedStyle.borderLeftWidth,"");
}
]]></body>
            </method>

            <method name="_removeElement">
                <body><![CDATA[if (! this.container)
return;
var urlbar = document.getElementById("urlbar");
if (! urlbar)
return;
urlbar.removeChild(this.container);
this.container = null;]]></body>
            </method>

            <constructor><![CDATA[this.logoConstructor();]]></constructor>

            <destructor><![CDATA[this.logoDestructor();]]></destructor>

        </implementation>
    </binding>

    <binding id="widget">
        <resources>
            <stylesheet src="button.css"/>
        </resources>

        <content>
            <xul:toolbarbutton anonid="logo-button" class="chromeclass-toolbar-additional toolbarbutton-1" hidden="true" onclick="if (event.target == this &amp;&amp; event.button == 1) logoClicked(event);" oncommand="if (event.target == this) logoClicked(event);">
            </xul:toolbarbutton>
        </content>

        <implementation implements="nsIDOMEventListener">
            <field name="mAppName" readonly="true"><![CDATA[this.module.API.Environment.barPlatform.name;]]></field>

            <field name="mButton" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","logo-button");]]></field>

            <field name="mPopupMenu" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","logo-menupopup");]]></field>

            <field name="mMenuLoaded">false</field>

            <method name="wdgtxConstructor">
                <body><![CDATA[this.mButton.style.setProperty("list-style-image","url('" + this.module.logoImagePath + "')","important");
this.mButton.setAttribute("tooltiptext",this.module.logoTooltip);
this.mButton.setAttribute("label",this.module.vendorName);
this.mButton.removeAttribute("hidden");]]></body>
            </method>

            <method name="wdgtxDestructor">
                <body><![CDATA[this.mMenuLoaded = false;
this.mButton.setAttribute("hidden",true);
this.mButton.removeAttribute("tooltiptext");]]></body>
            </method>

            <constructor><![CDATA[setTimeout(function (me) {
if (me && "wdgtxConstructor" in me)
me.wdgtxConstructor();
}
,0,this);]]></constructor>

            <destructor><![CDATA[this.wdgtxDestructor();]]></destructor>

            <method name="logoClicked">
                <parameter name="aEvent"/>
                <body><![CDATA[this.module.navigateMainPage(aEvent);]]></body>
            </method>

            <method name="openLink">
                <parameter name="event"/>
                <body><![CDATA[this.module.handleEvent(event);]]></body>
            </method>

            <method name="reload">
                <body><![CDATA[this.wdgtxDestructor();
this.wdgtxConstructor();]]></body>
            </method>

        </implementation>

        <handlers>
            <handler action="openLink(event);" event="command"/>
        </handlers>
    </binding>
</bindings>
