<?xml version='1.0'?>

<!DOCTYPE bindings [
<!ENTITY % dialog SYSTEM "/native/fx/dialog.dtd">
%dialog;
]>



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

    <binding extends="chrome://global/content/bindings/popup.xml#panel" id="alert">
        <content>
            <stack anonid="stack" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" yaHidden="true">
                <vbox anonid="contentHolder">
                    <hbox anonid="contentTextHolder">
                        <hbox align="center" anonid="alertImageBox" pack="center" valign="top">
                            <image anonid="alertImage"/>
                        </hbox>
                        <vbox anonid="alertTextBox" flex="1">
                            <hbox anonid="smallBlock" flex="1">
                                <label anonid="serviceTitle" class="plain bold" value=""/>
                                <label anonid="badgeCounter" class="plain bold" value=""/>
                                <label anonid="badgeText" class="plain bold" flex="1" value=""/>
                            </hbox>

                            <vbox anonid="bigBlock" flex="1" hidden="true">
                                <label anonid="mailFrom" class="plain bold" crop="end" value=""/>
                                <label anonid="mailSubject" class="plain" crop="end" value=""/>
                                <label anonid="mailFirstLine" class="plain" crop="end" value=""/>
                            </vbox>
                        </vbox>
                    </hbox>
                </vbox>
                <hbox anonid="title" pack="end">
                    <vbox>
                        <image anonid="close-button" onmousedown="close(); return false;" tooltiptext="&dialog.window.close;"/>
                    </vbox>
                </hbox>
            </stack>
            <children/>
        </content>

        <implementation>
            <field name="yaWinArguments"/>

            <field name="mAnimateWindowClose">true</field>
            <field name="mAnimateWindowOpacity">true</field>
            <field name="mAnimateWindowMove">true</field>

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

            <field name="_closeTimeout"/>
            <field name="_animationInterval"/>

            <method name="getElement">
                <parameter name="aAnonymousID"/>
                <body><![CDATA[return document.getAnonymousElementByAttribute(this,"anonid",aAnonymousID);]]></body>
            </method>

            <constructor><![CDATA[var params = this.yaWinArguments;
this.setAttribute("yaOS",params.api.Environment.os.name);
if ("animateWindowClose" in params)
this.mAnimateWindowClose = params.animateWindowClose;
if ("animateWindowOpacity" in params)
this.mAnimateWindowOpacity = params.animateWindowOpacity;
if ("animateWindowMove" in params)
this.mAnimateWindowMove = params.animateWindowMove;
if (params.api.Environment.os.name == "linux")
{
this.mAnimateWindowOpacity = false;
this.mAnimateWindowMove = false;
}

var imageURL = params.api.Package.resolvePath("/native/fx/dialogs/alerts/icons/" + params.type + ".png");
this.getElement("alertImage").style.setProperty("list-style-image","url('" + imageURL + "')","important");
var data = params.data;
this.getElement("serviceTitle").value = data.serviceTitle;
this.getElement("badgeCounter").value = data.badgeCounter;
this.getElement("badgeText").value = data.badgeText;
this.getElement("contentHolder").setAttribute("data-url",data.url || "");
this.getElement("contentHolder").setAttribute("data-url-action",data.urlAction || "");
if (params.type == "mail")
{
let messages = data.messages;
if (messages.length == 1)
{
let message = messages[0];
this.getElement("mailFrom").setAttribute("value",message.from || "");
this.getElement("mailSubject").setAttribute("value",message.subject || "");
this.getElement("mailFirstLine").setAttribute("value",message.firstline || "");
this.getElement("contentHolder").setAttribute("data-url",message.url || "");
this.getElement("smallBlock").setAttribute("hidden","true");
this.getElement("bigBlock").setAttribute("hidden","false");
}

}

this._closeTimeout = window.setTimeout((function () {
if ("hide" in this)
this.hide();
}
).bind(this),10000);
this.openPopupAtScreen(99999,99999);
setTimeout((function () {
if ("move" in this)
this.move(1);
}
).bind(this),params.api.Environment.os.name == "linux" ? 500 : 0);]]></constructor>

            <method name="hide">
                <body><![CDATA[if (this.mAnimateWindowClose)
{
this.move(- 1);
}
else
{
this.close();
}
]]></body>
            </method>

            <method name="close">
                <body><![CDATA[if (this._animationInterval)
window.clearInterval(this._animationInterval);
this.hidePopup();]]></body>
            </method>

            <method name="transition">
                <parameter name="stage"/>
                <body><![CDATA[this.getElement("stack").removeAttribute("yaHidden");
var smallBlock = this.getElement("smallBlock").boxObject;
if (smallBlock.width)
{
let contentHolderBO = this.getElement("contentHolder").boxObject;
if (contentHolderBO.width > this.boxObject.width)
this.style.maxWidth = contentHolderBO.width + "px";
}

if (this.mAnimateWindowMove)
{
let scr = screen;
this.moveTo(scr.availLeft + scr.availWidth - this.boxObject.width + 1,scr.availTop + scr.availHeight - stage * this.boxObject.height + 1);
}

if (this.mAnimateWindowOpacity)
this.style.opacity = stage;]]></body>
            </method>

            <method name="move">
                <parameter name="direction"/>
                <body><![CDATA[if (this._animationInterval)
window.clearInterval(this._animationInterval);
direction = direction === undefined ? 1 : direction;
var stage = direction > 0 ? 0 : 1;
var step = 0.1 * direction;
this._animationInterval = window.setInterval((function () {
stage += step;
if (stage > 0.99)
{
stage = 1;
}
else
if (stage < 0.01)
{
stage = 0;
this.close();
return;
}

this.transition(stage);
}
).bind(this),50,this);]]></body>
            </method>

            <method name="onMouseOver">
                <body><![CDATA[this.setAttribute("data-hovered","true");
if (this._closeTimeout)
{
window.clearTimeout(this._closeTimeout);
this._closeTimeout = null;
}
]]></body>
            </method>
        </implementation>

        <handlers>
            <handler event="popuphidden"><![CDATA[if (event.target === this)
this.parentNode.removeChild(this);]]></handler>

            <handler event="mouseover"><![CDATA[if (this._mousemoved)
this.onMouseOver();]]></handler>

            <handler event="mousemove"><![CDATA[if (this._closeTimeout && this._mousemoved)
this.onMouseOver();
this._mousemoved = true;]]></handler>

            <handler event="mouseout"><![CDATA[this.removeAttribute("data-hovered");
if (this._closeTimeout)
window.clearTimeout(this._closeTimeout);
this._closeTimeout = window.setTimeout((function () {
if ("hide" in this)
this.hide();
}
).bind(this),1500);]]></handler>

            <handler event="click"><![CDATA[var data = this.yaWinArguments.data;
var url = this.getElement("contentHolder").getAttribute("data-url");
if (! url)
return;
var api = this.yaWinArguments.api;
var urlAction = this.getElement("contentHolder").getAttribute("data-url-action");
if (urlAction)
api.Statistics.logCustomAction(urlAction);
api.Controls.navigateBrowser({
target: "new tab",
url: url});
this.close();]]></handler>
        </handlers>
    </binding>
</bindings>
