<?xml version="1.0"?>

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

  <binding id="floatingpanel"
           extends="chrome://global/content/bindings/popup.xml#panel">

    <resources>
      <stylesheet src="chrome://bluegriffon/skin/panels/floatingpanel.css"/>
    </resources>

    <content noautohide="true" level="floating">
      <xul:hbox align="center" class="titleholder">
        <xul:image class="floatingpanel-close"
                   onclick="this.parentNode.parentNode.closePanel()"/>
        <xul:titlebar flex="1">
          <xul:label xbl:inherits="value=label"/>
        </xul:titlebar>
        <xul:image class="floatinpanel-align"
                   onclick="AlignAllPanels()"/>
      </xul:hbox>
      <children/>
      <xul:hbox style="width: 25px;" align="center">
        <xul:spacer flex="1"/>
        <xul:box anonid="resizer"
                 onmousedown="captureMouse(event, this)"
                 onmouseup="releaseMouse(event, this)"
                 onmousemove="resizePanel(event, this)"/>
      </xul:hbox>
    </content>

    <implementation>
      <field name="_captured">false</field>
      <field name="_captureX">0</field>
      <field name="_initialW">0</field>
      <field name="_initialH">0</field>

      <constructor><![CDATA[
        if (this.getAttribute("open") == "true") {
          var _self = this;
          setTimeout(function() {
              _self.openPanel(null, true);
              NotifierUtils.notify("redrawPanel", _self.id);
            }, 500);
        }
      ]]></constructor>
      
      <method name="openPanel">
        <parameter name="aAnchorElement"/>
        <parameter name="aDoResize"/>
        <body>
        <![CDATA[
          try {
            if (aAnchorElement)
              this.openPopup(aAnchorElement, "after_start", 0, 0,
                             false, true);
            else
              this.openPopup(document.documentElement, "start_before", 10, 10,
                             false, true);
            this.setAttribute("open", "true");
            document.persist(this.id, "open");
            if (aDoResize && this.hasAttribute("width") && this.hasAttribute("height"))
              this.sizeTo(this.getAttribute("width"), this.getAttribute("height"));
            BlueGriffonVars.lastPanelRaised = this;
          } catch(e) {}
        ]]>
        </body>
      </method>

      <method name="closePanel">
        <body><![CDATA[
          this.setAttribute("style", "opacity: 0");
          var _self = this;
          setTimeout(function() {
              _self.hidePopup();
              _self.removeAttribute("style");
              _self.setAttribute("open", "false");
              document.persist(_self.id, "open");
            }, 500);
        ]]></body>
      </method>

      <method name="persistPosition">
        <body><![CDATA[
          try {
            var screenX = this.boxObject.screenX;
            var screenY = this.boxObject.screenY;
            var width   = this.boxObject.width;
            var height  = this.boxObject.height;
            this.setAttribute("left",   screenX);
            this.setAttribute("top",    screenY);
            this.setAttribute("width",  width);
            this.setAttribute("height", height);
            document.persist(this.id, "left");
            document.persist(this.id, "top");
            document.persist(this.id, "width");
            document.persist(this.id, "height");
          } catch (e) {
          }
        ]]></body>
      </method>

      <method name="captureMouse">
        <parameter name="aEvent"/>
        <parameter name="aElt"/>
        <body><![CDATA[
          var panel = aElt.parentNode.parentNode;
          var screenX = panel.boxObject.screenX;
          var screenY = panel.boxObject.screenY;
          panel.moveTo(screenX, screenY);
          if (!panel._captured)
          {
            panel._captured = true;
            panel._captureX = aEvent.clientX;
            panel._captureY = aEvent.clientY;
            panel._initialW = panel.boxObject.width;
            panel._initialH = panel.boxObject.height;
            aElt.setCapture(false);
          }
        ]]></body>
      </method>

      <method name="releaseMouse">
        <parameter name="aEvent"/>
        <parameter name="aElt"/>
        <body><![CDATA[
          var panel = aElt.parentNode.parentNode;
          if (panel._captured)
          {
            this._captured = false;
            aElt.releaseCapture();
            var dx = aEvent.clientX - panel._captureX;
            var dy = aEvent.clientY - panel._captureY;
            panel.sizeTo(panel._initialW + dx, panel._initialH + dy);
          }
        ]]></body>
      </method>

      <method name="resizePanel">
        <parameter name="aEvent"/>
        <parameter name="aElt"/>
        <body><![CDATA[
          var panel = aElt.parentNode.parentNode;
          if (panel._captured)
          {
            var dx = aEvent.clientX - panel._captureX;
            var dy = aEvent.clientY - panel._captureY;
            panel.sizeTo( (panel._initialW + dx) , (panel._initialH + dy) );
          }
        ]]></body>
      </method>

    </implementation>

    <handlers>

      <handler event="popuphiding">
        this.persistPosition();
      </handler>

      <handler event="mousedown"><![CDATA[
        if (BlueGriffonVars.lastPanelRaised != this ||
            BlueGriffonVars.lastPanelRaisedDidNotIntersect) {
          // this is ugly but there is no method to raise a panel above other existing panels...
          // XXX TODO : check if the panel to raise intersects with other visible panels
          // if not, nothing to do here after next line
          BlueGriffonVars.lastPanelRaised = this;
          var r1 = { x1: this.boxObject.screenX,
                     y1: this.boxObject.screenY,
                     x2: this.boxObject.screenX + this.boxObject.width,
                     y2: this.boxObject.screenY + this.boxObject.height };
          var panels = document.querySelectorAll('panel[floating="true"]');
          var intersecting = false;
          for (var i = 0; i < panels.length; i++) {
            var p = panels[i];
            if (p != this) {
              var r2 = { x1: p.boxObject.screenX,
                         y1: p.boxObject.screenY,
                         x2: p.boxObject.screenX + p.boxObject.width,
                         y2: p.boxObject.screenY + p.boxObject.height };
              var overlap =  (r1.x1 < r2.x2 && r1.x2 > r2.x1 &&
                                r1.y1 < r2.y2 && r1.y2 > r2.y1);
              intersecting = intersecting || overlap;
            }
          }
          if (!intersecting) { // we can take an early way out
            BlueGriffonVars.lastPanelRaisedDidNotIntersect = true;
            return;
          }
          BlueGriffonVars.lastPanelRaisedDidNotIntersect = false;
          this.hidePopup();
          this.openPanel(null, false);
        }
      ]]></handler>

    </handlers>

  </binding>

</bindings>

