<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl">
  <binding id="abbr">
    <implementation>
      <constructor type="application/x-javascript" xml:space="preserve">
        <![CDATA[
          const abns = 'http://suika.fam.cx/~wakaba/archive/2004/12/6/ab#';
          if (this.hasAttributeNS (null, 'title')) {
            var abbrBody = this.textContent;
            var abbrTitle = this.getAttributeNS (null, 'title');

            var abbrContent = abbrBody + abbrTitle;

            var abn = document.getElementsByTagNameNS (abns, 'note');
            var chk = 1;
            for (var j = 0; j < abn.length; j++) {
              if (abn[j].textContent == abbrContent) {
                chk = 0;
                break;
              }
            }

            if (chk) {
              var n = document.createElementNS (abns, 'note');
                var nc = document.createElementNS (abns, 'caption');
                nc.appendChild (document.createTextNode (abbrBody));
              n.appendChild (nc);
                var nd = document.createElementNS (abns, 'desc');
                nd.appendChild (document.createTextNode (abbrTitle));
              n.appendChild (nd);
              this.parentNode.insertBefore (n, this);
            }
          }
        ]]>
      </constructor>
    </implementation>
  </binding>
  <binding id="footnote">
    <implementation>
      <constructor type="application/x-javascript" xml:space="preserve">
        <![CDATA[
          const abns = 'http://suika.fam.cx/~wakaba/archive/2004/12/6/ab#';
          var n = document.documentElement.getAttributeNS
                                                (abns, 'last-fnref') - 0 + 1;
          var fnref = document.createElementNS (abns, 'fnref');
            fnref.appendChild (document.createTextNode (n));
            this.parentNode.insertBefore (fnref, this);
            this.setAttributeNS (abns, 'fnnum', n);
          document.documentElement.setAttributeNS (abns, 'last-fnref', n);
        ]]>
      </constructor>
    </implementation>
  </binding>
  <binding id="bottom-by-previous-sibling-offset">
    <implementation>
      <constructor>
        var thisEl = this;
        thisEl.setAttributeNS ('http://suika.fam.cx/www/style/ui/sidenote#',
                               'sidenote:auto-positioning', 'true');
        var setPositionEv = function () {
          var prevSibling = thisEl.SideNotePreviousSibling;
          if (prevSibling) {
            thisEl.SideNoteSetNewPosition (prevSibling);
          }
        };
        window.addEventListener ('resize', setPositionEv, false);
        var setPositionOnLoad = function () {
          setPositionEv ();
          document.removeEventListener ('mousemove', setPositionOnLoad, false);
        };
        document.addEventListener ('mousemove', setPositionOnLoad, false);
        var setPositionOnLoad2 = function () {
          setPositionEv ();
          document.removeEventListener ('mousemove', setPositionOnLoad2, false);
        };
        document.addEventListener ('mousemove', setPositionOnLoad2, false);
          /* NOTE: Calls two times so that heights are correct. */
        this.addEventListener ('mouseover', setPositionEv, false);
      </constructor>
      <property name="SideNotePreviousSibling" readonly="true">
        <getter><![CDATA[
          var prevSibling = this.previousSibling;
          while (prevSibling &&
                 (prevSibling.nodeType != prevSibling.ELEMENT_NODE ||
                  prevSibling.getAttributeNS
                               ('http://suika.fam.cx/www/style/ui/sidenote#',
                                'auto-positioning') == 'true')) {
            prevSibling = prevSibling.previousSibling;
          }
          return prevSibling;
        ]]></getter>
      </property>
      <property name="SideNotePreviousSideNote" readonly="true">
        <getter><![CDATA[
          var prevSibling = this.previousSibling;
          while (prevSibling &&
                 (prevSibling.nodeType != prevSibling.ELEMENT_NODE ||
                  prevSibling.getAttributeNS
                               ('http://suika.fam.cx/www/style/ui/sidenote#',
                                'auto-positioning') != 'true')) {
            prevSibling = prevSibling.previousSibling;
          }
          return prevSibling;
        ]]></getter>
      </property>
      <method name="SideNoteSetNewPosition">
        <parameter name="prevSibling"/>
        <body>
          var prev = this.SideNotePreviousSideNote;
          var prevVal = 0;
          if (prev) {
            prevVal = prev.offsetTop + prev.offsetHeight;
          }
          var bottomVal = prevSibling.offsetTop + prevSibling.offsetHeight;
          var topVal = bottomVal - this.offsetHeight;
          if (prevVal > topVal) {
            topVal = prevVal;
            bottomVal = topVal + this.offsetHeight;
          }
          this.style.top = topVal + 'px';
          this.style.bottom = 'auto';
        </body>
      </method>
    </implementation>
  </binding>
  <binding id="top-by-previous-sibling-offset"
           extends="#bottom-by-previous-sibling-offset">
    <implementation>
      <method name="SideNoteSetNewPosition">
        <parameter name="prevSibling"/>
        <body>
          var prev = this.SideNotePreviousSideNote;
          var prevVal = 0;
          if (prev) {
            prevVal = prev.offsetTop + prev.offsetHeight;
          }
          var topVal = prevSibling.offsetTop + prevSibling.offsetHeight;
          if (prevVal > topVal) {
            topVal = prevVal;
          }
          this.style.top = topVal + 'px';
          this.style.bottom = 'auto';
        </body>
      </method>
    </implementation>
  </binding>
</bindings>
<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (the "License"); you may not use this file except in compliance with
   - the License. You may obtain a copy of the License at
   - <http://www.mozilla.org/MPL/>
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is sidenote.xbl code.
   -
   - The Initial Developer of the Original Code is Wakaba.
   - Portions created by the Initial Developer are Copyright (C) 2003
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -   Wakaba <w@suika.fam.cx>
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the LGPL or the GPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   -
   - ***** END LICENSE BLOCK ***** -->
