<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<bindings
    xmlns="http://www.mozilla.org/xbl"
    xmlns:xbl="http://www.mozilla.org/xbl"
    xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="dialog">
    <content xbl:inherits="orient, closebutton" flex="1">
      <children/>
    </content>

    <implementation implements="nsIDOMEventListener">
      <field name="arguments"/>
      <field name="parent"/>
      <property name="_scrollbox" readonly="true" onget="return this.getElementsByTagName('scrollbox')[0];"/>

      <constructor><![CDATA[
        this._closed = false;
        if (this.hasAttribute("script")) {
          try {
            Services.scriptloader.loadSubScript(this.getAttribute("script"), this);
          } catch(e) {
            throw("Dialog : Unable to load script : " + this.getAttribute("script") + "\n");
          }
        }
        window.addEventListener("unload", this, true);

        let scrollbox = this._scrollbox;
        if (scrollbox) {
          window.addEventListener("resize", this, true);
          scrollbox.addEventListener("overflow", this, true);
        }
        setTimeout(this.load.bind(this), 0);
      ]]></constructor>

      <method name="handleEvent">
        <parameter name="aEvent"/>
        <body><![CDATA[
          switch(aEvent.type) {
            case "unload":
              if (aEvent.originalTarget == document)
                this._removeDialog();
              break;

            case "resize":
            case "overflow":
              let scrollbox = this._scrollbox;
              let style = document.defaultView.getComputedStyle(scrollbox, null);
              let newHeight =  Math.ceil(scrollbox.firstChild.getBoundingClientRect().height) + 
                               parseInt(style.marginTop) +
                               parseInt(style.marginBottom);
              scrollbox.style.minHeight = Math.min(window.innerHeight / 2, newHeight) + "px";
              break;
          }
        ]]></body>
      </method>

      <method name="load">
        <body><![CDATA[
          if (this.hasAttribute("onload")) {
            let func = new Function(this.getAttribute("onload"));
            func.call(this);
          }
        ]]></body>
      </method>

      <method name="close">
        <body><![CDATA[
          if (this.hasAttribute("onclose")) {
            let func = new Function(this.getAttribute("onclose"));
            func.call(this);
          }
          this._removeDialog();
        ]]></body>
      </method>

      <method name="_removeDialog">
        <body><![CDATA[
          window.removeEventListener("unload", this, true);
          let scrollbox = this._scrollbox;
          if (scrollbox) {
            window.removeEventListener("resize", this, true);
            scrollbox.removeEventListener("overflow", this, true);
          }

          this.parentNode.parentNode.removeChild(this.parentNode);
          this._closed = true;

          // emit DOMModalDialogClosed event
          let event = document.createEvent("Events");
          event.initEvent("DOMModalDialogClosed", true, false);
          let dispatcher = this.parent || getBrowser();
          dispatcher.dispatchEvent(event);
        ]]></body>
      </method>
      
      <method name="waitForClose">
        <body><![CDATA[
          while (!this._closed)
            Services.tm.currentThread.processNextEvent(true);
        ]]></body>
      </method>
    </implementation>
  </binding>
</bindings>
