<?xml version='1.0'?>
<!DOCTYPE bindings SYSTEM "/mc.dtd"><bindings xmlns:islands="http://bar.yandex.ru/ui/islands" 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="chrome://global/content/bindings/notification.xml#notification" id="notification">
        <resources>
            <stylesheet src="../../styles/mc/notification.css"/>
            <stylesheet src="../../bindings/islands/islands.css"/>
        </resources>

        <content flex="1">
            <xul:hbox flex="1">
                <xul:hbox anonid="notification-image-box">
                    <xul:image anonid="notification-image"/>
                </xul:hbox>
                <xul:hbox>
                    <xul:deck xbl:inherits="selectedIndex=data-selectedIndex">
                        <xul:hbox>
                            <xul:label value="&mc.widget.suggestion.label;"/>
                            <xul:label anonid="suggection-label" class="text-link" xbl:inherits="value=data-suggestionURL"/>
                            <islands:button anonid="go-button" label="&mc.widget.go.label;" islands:size="S" islands:type="normal"/>
                            <islands:button anonid="cancel-button" label="&mc.widget.cancel.label;" islands:size="S" islands:type="normal"/>
                        </xul:hbox>
                        <xul:hbox>
                            <xul:label value="&mc.widget.changedFrom.label;"/>
                            <xul:label anonid="changed-from-label" class="text-link" xbl:inherits="value=data-changedFromURL"/>
                            <xul:label value="&mc.widget.changedTo.label;"/>
                            <xul:label anonid="changed-to-label" class="text-link" xbl:inherits="value=data-changedToURL"/>
                            <xul:label value="&mc.widget.changedTo2.label;"/>
                            <islands:button anonid="remove-rule-button" label="&mc.widget.removeRule.label;" islands:size="S" islands:type="normal"/>
                        </xul:hbox>
                    </xul:deck>
                </xul:hbox>
            </xul:hbox>
            <xul:hbox anonid="close-button">
                <xul:hbox>&mc.widget.close.label;</xul:hbox>
                <xul:image class="image-close"/>
            </xul:hbox>
        </content>

        <implementation implements="nsIDOMEventListener">
            <field name="faviconImage" readonly="true">
                this._elementByAttr("anonid", "notification-image");
            </field>

            <property name="thumbnail">
                <setter><![CDATA[this._slice.notify({
topic: "set-image",
data: val || ""});]]></setter>
            </property>

            <property name="_isCurrentNotification">
            	<getter><![CDATA[return this.control.currentNotification === this;]]></getter>
            </property>

            <field name="api" readonly="true">this.mistypeCorrectorElement.api</field>
            <field name="wListener" readonly="true">this.mistypeCorrectorElement.wListener</field>
            <field name="NOTIFICATION_NAME" readonly="true">this.mistypeCorrectorElement.NOTIFICATION_NAME</field>

            <field name="_notificationType" readonly="true">
                this.getAttribute("data-selectedIndex") === "0" ? "suggest" : "redirect"
            </field>

            <field name="_notificationTab" readonly="true"><![CDATA[{
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
this.control.getElementsByTagNameNS(XUL_NS,"browser")[0];
}
]]></field>

            <property name="destinationURL" readonly="true">
                <getter><![CDATA[return this._notificationType === "suggest" ? this.getAttribute("data-suggestionURL") : this.getAttribute("data-changedToURL");]]></getter>
            </property>

            <property name="screenGrabber" readonly="true">
                <getter><![CDATA[if (! this._screenGrabber)
{
Components.utils.import(this.api.Package.resolvePath("modules/scrgrab.js"),this);
this._screenGrabber = new this.ScrGrabber(this.api, this);
}

return this._screenGrabber;]]></getter>
            </property>

            <method name="onSShotCreated">
                <parameter name="aPageData"/>
                <body><![CDATA[if (aPageData.url !== this.destinationURL)
return;
this.thumbnail = aPageData.img;
this.api.Async.nextTick((function () {
if (this._notificationTab === gBrowser.mCurrentBrowser && (this._isCurrentNotification && this._notificationType === "suggest"))
{
this.showSiteThumbnail();
}

}
).bind(this));]]></body>
            </method>

            <constructor><![CDATA[this.close = (function close(originalClose) {
this._slice.hide();
this._removeTabData();
this.mistypeCorrectorElement.hideUrlbarImage();
return originalClose.apply(this);
}
).bind(this,this.close);
this._slice = this.api.Controls.createSlice({
url: this.api.Package.resolvePath("/slice/slice.html"),
disposable: false,
windowProperties: {
width: 281,
height: 214},
system: false,
noautohide: true,
messageHandler: (function Slice_messageHandler(message) {
if (message.topic !== "close")
throw new Error("Unknown message topic");
var destinationURL = this.destinationURL;
var mce = this.mistypeCorrectorElement;
mce.hideUrlbarImage();
mce.sendStatistic("redir.agree.preview",mce.mistyped,destinationURL);
mce.module.appendRule(mce.mistyped,destinationURL);
this.close();
openUILink(destinationURL);
}
).bind(this)});
var tabData = this.wListener.getTabData(this._notificationTab,this.NOTIFICATION_NAME);
if (! tabData.notificationProperties.shown)
{
tabData.notificationProperties.shown = true;
if (this._notificationType === "suggest")
{
this.api.Statistics.logShortAction(5300);
}
else
{
this._sendStatistic("del.show");
}

}

this._setFavicon();
this.screenGrabber.getCanvasForURL(this.destinationURL);
gBrowser.tabContainer.addEventListener("TabSelect",this,false);
this._notificationTab.addEventListener("click",this,false);
gBrowser.getNotificationBox().addEventListener("AlertActive",this,false);]]></constructor>

            <destructor><![CDATA[gBrowser.tabContainer.removeEventListener("TabSelect",this,false);
this._notificationTab.removeEventListener("click",this,false);
gBrowser.getNotificationBox().removeEventListener("AlertActive",this,false);
if (this._slice)
{
this._slice.hide();
this._slice = null;
}

if (this._screenGrabber)
{
this._screenGrabber.destroy();
this._screenGrabber = null;
}
]]></destructor>

            <method name="showSiteThumbnail">
                <body><![CDATA[setTimeout((function () {
this._slice && this._slice.show(this.faviconImage);
}
).bind(this),0);]]></body>
            </method>

            <method name="switchSiteThumbnail">
                <body><![CDATA[if (this._slice.isOpen)
this._slice.hide(); else
this.showSiteThumbnail();]]></body>
            </method>

            <method name="_elementByAttr">
                <parameter name="attr"/>
                <parameter name="value"/>
                <body><![CDATA[return document.getAnonymousElementByAttribute(this,attr,value);]]></body>
            </method>

            <method name="_setFavicon">
                <body><![CDATA[var destinationURL = this.destinationURL;
var destinationDomain = this.mistypeCorrectorElement.extractDomain(destinationURL);
var mistyped = this.mistypeCorrectorElement.mistyped;
this._setFaviconImage("http://favicon.yandex.net/favicon/" + destinationDomain,(function (exist) {
if (! ("_setFaviconImage" in this))
return;
if (exist)
{
this.mistypeCorrectorElement.sendStatistic("redir.show",mistyped,destinationURL);
return;
}

this._setFaviconImage("http://" + (destinationDomain + "/favicon.ico"),(function (exist) {
if (! ("_setFaviconImage" in this))
return;
if (exist)
{
this.mistypeCorrectorElement.sendStatistic("redir.show",mistyped,destinationURL);
return;
}

}
).bind(this));
}
).bind(this));]]></body>
            </method>

            <method name="_setFaviconImage">
                <parameter name="src"/>
                <parameter name="callback"/>
                <body><![CDATA[var image = this.faviconImage;
image.src = src;
image.onload = function (e) callback(image.clientWidth > 15);
image.onerror = function (e) callback(false);]]></body>
            </method>

            <method name="_removeTabData">
                <body><![CDATA[this.wListener.removeTabData(this._notificationTab,this.NOTIFICATION_NAME);]]></body>
            </method>

            <method name="_sendStatistic">
                <parameter name="type"/>
                <body><![CDATA[var mce = this.mistypeCorrectorElement;
mce.sendStatistic(type,mce.mistyped,this.destinationURL);]]></body>
            </method>

            <method name="handleEvent">
                <parameter name="aEvent"/>
                <body><![CDATA[switch (aEvent.type) {
case "click":

case "TabSelect":
this._slice.hide();
this._removeTabData();
break;
case "AlertActive":
if (! this._isCurrentNotification)
{
this._slice.hide();
}

break;
}
]]></body>
            </method>
        </implementation>

        <handlers>
            <handler button="0" event="click"><![CDATA[var target = event.originalTarget;
var anonid = target.getAttribute("anonid");
while (! anonid && target.parentNode) {
target = target.parentNode;
anonid = target.getAttribute("anonid");
}

var destinationURL = this.destinationURL;
var mce = this.mistypeCorrectorElement;
var mistyped = mistypeCorrectorElement.mistyped;
switch (anonid) {
case "notification-image-box":

case "notification-image":
{
this.switchSiteThumbnail();
return;
}

case "suggection-label":
{
mce.module.appendRule(mistyped,destinationURL);
openUILink(destinationURL);
this._sendStatistic("redir.agree.url");
break;
}

case "go-button":
{
mce.module.appendRule(mistyped,destinationURL);
openUILink(destinationURL);
this._sendStatistic("redir.agree.button");
break;
}

case "changed-from-label":
{
openUILink(target.getAttribute("value"));
this._sendStatistic("del.url");
break;
}

case "changed-to-label":
{
openUILink(target.getAttribute("value"));
this._sendStatistic("del.url");
break;
}

case "remove-rule-button":
{
mce.module.removeRule(mistyped);
this._sendStatistic("del.remove");
break;
}

case "cancel-button":
{
mce.module.incrementWhitelist(mistyped,2);
this._sendStatistic("redir.reject");
this.api.Statistics.logShortAction(5330);
break;
}

case "close-button":
{
if (this._notificationType === "redirect")
{
this._sendStatistic("del.close");
}
else
{
mce.module.incrementWhitelist(mistyped);
this._sendStatistic("redir.close");
this.api.Statistics.logShortAction(5330);
}

break;
}

default:
return;
}

this._removeTabData();
close();]]></handler>
        </handlers>
    </binding>
</bindings>
