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

    <binding extends="../../bindings/orange/dialog/dialog.xml#dialog" id="window">
        <resources>
            <stylesheet src="opinions.css"/>
        </resources>

        <implementation>
            <field name="api">window.yaWinArguments.api</field>

            <method name="move">
                <body><![CDATA[var position = this.savedPosition || {
x: screen.availLeft + screen.availWidth - window.outerWidth - 10,
y: screen.availTop + screen.availHeight - window.outerHeight - 10};
window.moveTo(position.x,position.y);]]></body>
            </method>

            <method name="sizeToContent">
                <body><![CDATA[window.sizeToContent();]]></body>
            </method>

            <method name="setPref">
                <parameter name="strPrefName"/>
                <parameter name="strPrefValue"/>
                <body><![CDATA[var prefFullName = this.api.Settings.getPackageBranchPath() + strPrefName;
var prefsModule = this.api.Settings.PrefsModule;
return prefsModule.set(prefFullName,strPrefValue);]]></body>
            </method>

            <method name="getPref">
                <parameter name="strPrefName"/>
                <parameter name="defaultValue"/>
                <body><![CDATA[var prefFullName = this.api.Settings.getPackageBranchPath() + strPrefName;
var prefsModule = this.api.Settings.PrefsModule;
return prefsModule.get(prefFullName,defaultValue);]]></body>
            </method>

            <property name="savedPosition">
                <getter><![CDATA[var coords = this.getPref("dlg.opinions.coords");
if (coords)
{
coords = coords.split(",");
return {
x: parseInt(coords[0],10),
y: parseInt(coords[1],10)};
}

return null;]]></getter>
                <setter><![CDATA[this.setPref("dlg.opinions.coords",val.x + "," + val.y);]]></setter>
            </property>
        </implementation>
    </binding>

    <binding id="custom-arrowscrollbox-clicktoscroll">
        <content>
            <xul:box anonid="buttonup" class="scrollbutton-arrow" onmousedown="_dispatchEvent(event)" onmouseup="_dispatchEvent(event)">
                <xul:image/>
            </xul:box>

            <xul:arrowscrollbox anonid="arrowscrollbox" clicktoscroll="true" flex="1" xbl:inherits="orient,align,pack,dir">
                <children/>
            </xul:arrowscrollbox>

            <xul:box anonid="buttondown" class="scrollbutton-arrow" onmousedown="_dispatchEvent(event)" onmouseup="_dispatchEvent(event)">
                <xul:image/>
            </xul:box>
        </content>

        <implementation>
            <field name="mScrollbox">document.getAnonymousElementByAttribute(this, "anonid", "arrowscrollbox")</field>
            <field name="mSelfbuttonup">document.getAnonymousElementByAttribute(this, "anonid", "buttonup")</field>
            <field name="mSelfbuttondown">document.getAnonymousElementByAttribute(this, "anonid", "buttondown")</field>
            <field name="mNativebuttonup">null</field>
            <field name="mNativebuttondown">null</field>

            <constructor><![CDATA[if (! this.mScrollbox)
return;
this.mNativebuttonup = document.getAnonymousElementByAttribute(this.mScrollbox,"anonid","scrollbutton-up");
this.mNativebuttondown = document.getAnonymousElementByAttribute(this.mScrollbox,"anonid","scrollbutton-down");
if (! this.mNativebuttonup || ! this.mNativebuttondown)
return;
if ("_updateScrollButtonsDisabledState" in this.mScrollbox)
{
let me = this;
let origUpdate = this.mScrollbox._updateScrollButtonsDisabledState;
this.mScrollbox._updateScrollButtonsDisabledState = function _updateScrollButtonsDisabledState() {
var result = origUpdate.apply(this,arguments);
me.checkButtonsDisabledState();
return result;
}
;
}

this.checkButtonsDisabledState();]]></constructor>

            <property name="scrollBoxObject" readonly="true">
                <getter><![CDATA[return this.mScrollbox.scrollBoxObject;]]></getter>
            </property>

            <method name="scrollByPixels">
                <parameter name="px"/>
                <body><![CDATA[this.mScrollbox.scrollByPixels(px);]]></body>
            </method>

            <method name="checkButtonsDisabledState">
                <body><![CDATA[["up", "down"].forEach(function _checkButtonsDisabledState(direction) {
var nativeButton = this["mNativebutton" + direction];
var disabled = nativeButton.collapsed || nativeButton.getAttribute("disabled");
this["mSelfbutton" + direction].setAttribute("disabled",disabled);
}
,this);]]></body>
            </method>

            <method name="_dispatchEvent">
                <parameter name="event"/>
                <body><![CDATA[var target = event.target;
if (target.localName == "image")
target = target.parentNode;
var eventTypeAttr = "on" + event.type;
var button = this["mNative" + target.getAttribute("anonid")];
return button && button.hasAttribute(eventTypeAttr) ? button[eventTypeAttr](event) : false;]]></body>
            </method>
        </implementation>
    </binding>
</bindings>
