<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE bindings SYSTEM "/native/fx/pagetranslator.dtd"><bindings xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <binding id="notification">
        <resources>
            <stylesheet src="notification.css"/>
        </resources>
    </binding>

    <binding id="close">
        <content>
            <xul:label>&pagetranslator.notification.close;</xul:label>
            <xul:image/>
        </content>
    </binding>

    <binding extends="#notification" id="panel">
        <resources>
            <stylesheet src="panel.css"/>
        </resources>

        <content>
            <xul:stack align="left" anonid="main" flex="1">
                <xul:hbox anonid="preloader" flex="1">
                    <xul:label class="text preloader" flex="1">&pagetranslator.notification.process;</xul:label>
                </xul:hbox>

                <xul:hbox anonid="error" flex="1">
                    <xul:label class="text">&pagetranslator.notification.error;</xul:label>
                    <xul:hbox flex="1">
                        <xul:hbox anonid="repeat" class="pagetranslator-button">
                            <xul:label>&pagetranslator.notification.error.repeat;</xul:label>
                        </xul:hbox>
                    </xul:hbox>

                    <xul:hbox anonid="revert-error" class="close"/>
                </xul:hbox>

                <xul:hbox anonid="pagetranslator-container" flex="1">
                    <xul:hbox anonid="revert" class="pagetranslator-button">
                        <xul:image anonid="arrow"/>
                        <xul:label>&pagetranslator.notification.revert;</xul:label>
                    </xul:hbox>
                    <xul:hbox flex="1">
                        <xul:hbox align="center" anonid="info"/>
                        <xul:hbox anonid="promt" class="promt">
                            <xul:label class="text">&pagetranslator.notification.promt.preambula;</xul:label>
                            <xul:button anonid="promt-button">&pagetranslator.notification.promt.name;</xul:button>
                            <xul:image/>
                        </xul:hbox>
                    </xul:hbox>

                    <xul:hbox anonid="close" class="close"/>
                </xul:hbox>
            </xul:stack>
        </content>

        <implementation implements="nsIDOMEventListener">
            <field name="mPageTranslatorContainer">
                document.getAnonymousElementByAttribute(this, "anonid", "pagetranslator-container");
            </field>

            <field name="mPreloader">
                document.getAnonymousElementByAttribute(this, "anonid", "preloader");
            </field>

            <field name="mError">
                document.getAnonymousElementByAttribute(this, "anonid", "error");
            </field>

            <property name="mCloseNodes" readonly="true">
                <getter><![CDATA[var nodes = [];
nodes.push(document.getAnonymousElementByAttribute(this,"anonid","close"));
return nodes;]]></getter>
            </property>

            <property name="mRevertNodes" readonly="true">
                <getter><![CDATA[var nodes = [];
nodes.push(document.getAnonymousElementByAttribute(this,"anonid","revert"));
nodes.push(document.getAnonymousElementByAttribute(this,"anonid","revert-error"));
return nodes;]]></getter>
            </property>

            <field name="mRepeatButton">
                document.getAnonymousElementByAttribute(this, "anonid", "repeat");
            </field>

            <field name="mNotificationBox">
                gBrowser.getNotificationBox();
            </field>

            <field name="mMenuList">
                document.getAnonymousElementByAttribute(this, "anonid", "menulist-langs");
            </field>

            <field name="mInfo">
                document.getAnonymousElementByAttribute(this, "anonid", "info");
            </field>

            <field name="mPROMT">
                document.getAnonymousElementByAttribute(this, "anonid", "promt");
            </field>

            <field name="mPROMTButton">
                document.getAnonymousElementByAttribute(this, "anonid", "promt-button");
            </field>

            <field name="mStringBundleService">
                Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
            </field>

            <field name="mMenuListContainer">
                null
            </field>

            <field name="mPROMTPairs">
                []
            </field>

            <constructor><![CDATA[var doc = this.browser.contentDocument;
doc.addEventListener("PageTranslator_onTranslateFinish",this,false);
doc.addEventListener("PageTranslator_onTranslateError",this,false);
this.generateInfo();
this.show("preloader");
var lang = this.getAttribute("lang");
this.removeAttribute("lang");
this.setLang(lang);]]></constructor>

            <desctructor><![CDATA[var doc = this.browser.contentDocument;
doc.removeEventListener("PageTranslator_onTranslateFinish",this,false);
doc.removeEventListener("PageTranslator_onTranslateError",this,false);]]></desctructor>

            <method name="handleEvent">
                <parameter name="event"/>
                <body><![CDATA[switch (event.type) {
case "PageTranslator_onTranslateFinish":
this.show("info");
break;
case "PageTranslator_onTranslateError":
this.show("error");
break;
default:
break;
}
]]></body>
            </method>

            
            <method name="show">
                <parameter name="type"/>
                <body><![CDATA[this.mPageTranslatorContainer.style.setProperty("display","none","important");
this.mError.style.setProperty("display","none","important");
this.mPreloader.style.setProperty("display","none","important");
switch (type) {
case "preloader":
this.mPreloader.style.setProperty("display","-moz-box","important");
break;
case "error":
this.mError.style.setProperty("display","-moz-box","important");
break;
case "info":

default:
this.mPageTranslatorContainer.style.setProperty("display","-moz-box","important");
break;
}
]]></body>
            </method>

            <method name="generateInfo">
                <body><![CDATA[var bundlePath = this.pageTranslator.api.Package.resolvePath("/native/fx/pagetranslator.properties");
var bundle = this.mStringBundleService.createBundle(bundlePath);
var xulString = bundle.GetStringFromName("pagetranslator.notification.info");
var xulNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
var menuListContainer = document.createElementNS(xulNS,"hbox");
menuListContainer.setAttribute("anonid","menulist-langs-container");
menuListContainer.mNotification = this;
this.mMenuListContainer = menuListContainer;
var that = this;
xulString.replace(/([\s\S]*?)%lang([\s\S]*)/g,function (str, p1, p2) {
var prefix = p1.trim();
var postfix = p2.trim();
if (prefix)
{
let preLabel = document.createElementNS(xulNS,"label");
preLabel.setAttribute("value",prefix);
preLabel.className = "text";
that.mInfo.appendChild(preLabel);
}

that.mInfo.appendChild(menuListContainer);
if (postfix)
{
let postLabel = document.createElementNS(xulNS,"label");
postLabel.setAttribute("value",postfix);
postLabel.className = "text";
that.mInfo.appendChild(postLabel);
}

}
);]]></body>
            </method>

            
            <method name="translateOn">
                <parameter name="lang"/>
                <body><![CDATA[this.show("preloader");
if (lang)
this.setLang(lang);
this.pageTranslator.translate(lang);]]></body>
            </method>

            <method name="remove">
                <parameter name="skipAnimation"/>
                <body><![CDATA[var doc = this.browser.contentDocument;
doc.removeEventListener("PageTranslator_onTranslateFinish",this,false);
doc.removeEventListener("PageTranslator_onTranslateError",this,false);
this.mNotificationBox.removeNotification(this,skipAnimation);]]></body>
            </method>

            <method name="reloadPage">
                <body><![CDATA[this.remove();
this.pageTranslator.reloadPage();]]></body>
            </method>

            <method name="setLang">
                <parameter name="lang"/>
                <body><![CDATA[this.mMenuListContainer && this.mMenuListContainer.setLang(lang);
var pair = this.pageTranslator.translator.getLangPair(lang,this.pageTranslator.translator.language);
if (~ this.mPROMTPairs.indexOf(pair))
{
this._showPROMT();
}
else
{
this._hidePROMT();
}
]]></body>
            </method>

            <method name="openPROMTPage">
                <body><![CDATA[this.pageTranslator.api.Controls.navigateBrowser({
"url": "http://translate.ru"});]]></body>
            </method>

            <method name="_showPROMT">
                <body><![CDATA[this.mPROMT.style.setProperty("display","-moz-box","important");]]></body>
            </method>

            <method name="_hidePROMT">
                <body><![CDATA[this.mPROMT.style.setProperty("display","none","important");]]></body>
            </method>
        </implementation>

        <handlers>
            <handler button="0" event="click" phase="capturing"><![CDATA[var target = event.originalTarget;
for (let i = 0, length = this.mCloseNodes.length;i < length;i++) {
let node = this.mCloseNodes[i];
if (node && node.contains(target))
{
this.remove();
return;
}

}

for (let i = 0, length = this.mRevertNodes.length;i < length;i++) {
let node = this.mRevertNodes[i];
if (node && node.contains(target))
{
this.pageTranslator.revert();
return;
}

}

if (this.mRepeatButton.contains(target))
{
this.translateOn();
return;
}

if (this.mPROMTButton.contains(target))
this.openPROMTPage();]]></handler>
        </handlers>
    </binding>

    <binding id="pagetranslator-menulist">
        <content>
            <xul:menulist anonid="menulist-langs">
                <xul:menupopup>
                    <xul:menuitem label="&pagetranslator.notification.english.gen;" value="en"/>
                    <xul:menuitem label="&pagetranslator.notification.russian.gen;" value="ru"/>
                    <xul:menuitem label="&pagetranslator.notification.german.gen;" value="de"/>
                    <xul:menuitem label="&pagetranslator.notification.spanish.gen;" value="es"/>
                    <xul:menuitem label="&pagetranslator.notification.italian.gen;" value="it"/>
                    <xul:menuitem label="&pagetranslator.notification.french.gen;" value="fr"/>
                    <xul:menuitem label="&pagetranslator.notification.ukrainian.gen;" value="uk"/>
                    <xul:menuitem label="&pagetranslator.notification.turkish.gen;" value="tr"/>
                    <xul:menuitem label="&pagetranslator.notification.polish.gen;" value="pl"/>
                </xul:menupopup>
            </xul:menulist>
        </content>

        <implementation>
            <field name="mMenuList">
                document.getAnonymousElementByAttribute(this, "anonid", "menulist-langs");
            </field>

            <field name="mLang">null</field>

            <constructor><![CDATA[this._generateItems();
this.mMenuList.addEventListener("command",this.translateOn.bind(this),false);]]></constructor>

            <destructor><![CDATA[this.mNotification = null;]]></destructor>

            <method name="_generateItems">
                <body><![CDATA[var pageTranslator = this.mNotification.pageTranslator;
var items = this.mMenuList.menupopup.childNodes;
var tlang = pageTranslator.translator.language;
for (let i = 0, length = items.length;i < length;i++) {
let item = items[i];
let slang = item.value;
let pair = slang + "-" + tlang;
if (pageTranslator.translator.getAvailablePair(pair))
{
item.style.setProperty("display","-moz-box","important");
}
else
{
item.style.setProperty("display","none","important");
}

}
]]></body>
            </method>

            <method name="translateOn">
                <body><![CDATA[var menuitem = this.mMenuList.selectedItem;
if (this.mLang === menuitem.value)
{
return;
}

var lang = menuitem.value;
this.mNotification.translateOn(lang);]]></body>
            </method>

            <method name="setLang">
                <parameter name="lang"/>
                <body><![CDATA[var menuitem = document.getAnonymousElementByAttribute(this.mMenuList,"value",lang);
this.mLang = lang;
if (menuitem)
{
this.mMenuList.selectedItem = menuitem;
}
]]></body>
            </method>
        </implementation>
    </binding>

    
    <binding extends="#notification" id="tooltip">
        <resources>
            <stylesheet src="tooltip.css"/>
        </resources>

        <content>
            <xul:stack align="left" anonid="main" flex="1">
                <xul:hbox anonid="pagetranslator-container" flex="1">
                    <xul:hbox align="center" flex="1" pack="center">
                        <xul:image/>
                        <xul:label class="text">&pagetranslator.notification.tooltip;</xul:label>
                        <xul:hbox anonid="tooltip-image">
                            <xul:label class="text">&pagetranslator.translate;</xul:label>
                        </xul:hbox>
                    </xul:hbox>

                    <xul:hbox anonid="close" class="close"/>
                </xul:hbox>
            </xul:stack>
        </content>

        <handlers>
            <handler button="0" event="click"><![CDATA[if (event.originalTarget.getAttribute("anonid") === "close")
{
this.remove();
}
]]></handler>
        </handlers>
    </binding>
</bindings>
