<?xml version='1.0'?>
<!DOCTYPE bindings [
<!ENTITY % town SYSTEM "/town.dtd">
%town;
]>
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <binding extends="//toolkit/native/bindings/widget.xml#widget" id="town">
        <resources>
            <stylesheet src="town.css"/>
        </resources>

        <content>
            <xul:toolbarbutton anonid="title-button" cbDefaultLabel="&wgt.town.widget.name;" class="chromeclass-toolbar-additional toolbarbutton-1 label2" hidden="true" oncommand="if (event.target == this) _showWidgetMenu();" type="menu" yb-second-label="&wgt.town.widget.name;">
                <xul:menupopup anonid="title-menu">
                    <xul:menuitem label="&wgt.town.refresh;" oncommand="sendManualRequest();"/>
                    <xul:menuseparator/>
                    <xul:menuitem label="&wgt.town.setup;" oncommand="setupWidget();"/>
                    <xul:menuitem label="&wgt.town.delete;" oncommand="removeWidget();"/>
                    <xul:menuitem label="&wgt.town.add;" oncommand="addWidget();"/>
                </xul:menupopup>
            </xul:toolbarbutton>
            <xul:toolbarbutton anonid="weather-button" class="chromeclass-toolbar-additional toolbarbutton-1 label2" data-sliceOnly="true" hidden="true" oncommand="event.stopPropagation(); _onWeatherButtonCommand(event);" tooltip="weather-tooltip" tooltip-text="&wgt.town.weather.tooltip;" type="menu-button" yb-second-label-off="false">
                <xul:menupopup onpopupshowing="_showWeatherSlice(); return false;"/>
            </xul:toolbarbutton>
            <xul:toolbarbutton anonid="traffic-button" class="chromeclass-toolbar-additional toolbarbutton-1 label2" data-sliceOnly="true" hidden="true" oncommand="event.stopPropagation(); _onTrafficButtonCommand(event);" tooltip="traffic-tooltip" tooltip-text="&wgt.town.traffic.tooltip;" type="menu-button" yb-second-label-off="false">
                <xul:menupopup onpopupshowing="_showTrafficSlice(); return false;"/>
            </xul:toolbarbutton>
        </content>

        <implementation>
            <field name="_wdgtxConstructed">false</field>
            <field name="_cachedResource">null</field>

            <field name="mWIID" readonly="true"><![CDATA[this.getAttribute("yb-native-widget-wiid");]]></field>

            <field name="mTitleButton" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","title-button");]]></field>

            <field name="mTitleMenu" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","title-menu");]]></field>

            <field name="mTrafficButton" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","traffic-button");]]></field>

            <field name="mWeatherButton" readonly="true"><![CDATA[document.getAnonymousElementByAttribute(this,"anonid","weather-button");]]></field>

            <property name="mTrafficSliceOnly" onget="return this.mTrafficButton.getAttribute('data-sliceOnly') == 'true';" onset="this.mTrafficButton.setAttribute('data-sliceOnly', val);"/>

            <property name="mWeatherSliceOnly" onget="return this.mWeatherButton.getAttribute('data-sliceOnly') == 'true';" onset="this.mWeatherButton.setAttribute('data-sliceOnly', val);"/>

            <method name="observe">
                <parameter name="subject"/>
                <parameter name="topic"/>
                <parameter name="data"/>
                <body><![CDATA[if (! subject.contentAsXML)
return;
var url = this.module.makeURLForWIID(this.mWIID);
this.setExportData(subject.contentAsXML);
this.module.updateToolbaritemData(url,this.getExportData());
this.module.updateSliceData(url,this.getExportData());
this.updateData();]]></body>
            </method>

            <method name="setExportData">
                <parameter name="data"/>
                <body><![CDATA[this.module.Export.setDataFor(this.mWIID,data);]]></body>
            </method>

            <method name="resetExportData">
                <body><![CDATA[this.module.Export.resetDataFor(this.mWIID);]]></body>
            </method>

            <method name="getExportData">
                <body><![CDATA[return this.module.Export.getDataFor(this.mWIID);]]></body>
            </method>

            <method name="addRequest">
                <body><![CDATA[var expireTimeout = 60 * 60;
var url = this.module.makeURLForWIID(this.mWIID);
if (this.module.Export.isDataDefinedForURL(url))
{
this.subscribeForExport();
this.updateData();
}

this._cachedResource = this.module.API.Network.getCachedResource({
url: url,
method: "GET",
updateInterval: this.module.API.Settings.getValue("update-interval") * 60,
expireInterval: expireTimeout});
this._cachedResource.addListener("changed",this);
this._cachedResource.addListener("invalidated",this);]]></body>
            </method>

            <method name="removeRequest">
                <body><![CDATA[if (! this._cachedResource)
return;
this._cachedResource.removeListener("changed",this);
this._cachedResource.removeListener("invalidated",this);
this._cachedResource = null;]]></body>
            </method>

            <method name="resetRequest">
                <body><![CDATA[this.removeRequest();
this.addRequest();]]></body>
            </method>

            <method name="sendManualRequest">
                <body><![CDATA[if (! this._cachedResource)
return;
var url = this.module.makeURLForWIID(this.mWIID);
this.module.switchToolbaritemThrobber(url,true);
this._cachedResource.update(true,true);
setTimeout((function _sendManualRequest() {
this.module.switchToolbaritemThrobber(url,false);
}
).bind(this),0);]]></body>
            </method>

            <method name="setupWidget">
                <body><![CDATA[this.module.API.Controls.openSettingsDialog(window,this.mWIID);]]></body>
            </method>

            <method name="removeWidget">
                <body><![CDATA[this.module.API.Controls.removeWidget(this.mWIID);]]></body>
            </method>

            <method name="addWidget">
                <body><![CDATA[var [instanceID, widgetElement] = this.module.API.Controls.addWidget("http://bar.yandex.ru/packages/yandexbar#town",null,this,true);
this.module.API.Controls.openSettingsDialog(window,instanceID);]]></body>
            </method>

            <method name="subscribeForExport">
                <body><![CDATA[var url = this.module.makeURLForWIID(this.mWIID);
this.module.Export.subscribeWidget(this.mWIID,url);]]></body>
            </method>

            <method name="updateData">
                <body><![CDATA[var url = this.module.makeURLForWIID(this.mWIID);
var data = this.getExportData();
var title = this.module.API.Settings.getValue("title",this.mWIID) || data && data.values.title || this.mTitleButton.getAttribute("cbDefaultLabel");
this.mTitleButton.setAttribute("yb-second-label",title);
var weatherData = data && data.values.weather;
var weatherIcon = weatherData && weatherData.icon || this.module.defaultTownIconPath;
this.mWeatherButton.style.listStyleImage = "url(\"" + weatherIcon + "\")";
this.mWeatherButton.setAttribute("yb-second-label",weatherData && weatherData.temperature || "");
this.mWeatherButton.setAttribute("hidden",! this.module.API.Settings.getValue("show-weather",this.mWIID));
var trafficData = data && data.values.traffic;
var trafficIcon = trafficData && trafficData.icon || "";
this.mTrafficButton.style.listStyleImage = "url(\"" + trafficIcon + "\")";
var trafficLevel = trafficData && trafficData.level || "";
this.mTrafficButton.setAttribute("yb-second-label",trafficLevel);
var trafficHidden = trafficLevel === "" || ! this.module.API.Settings.getValue("show-traffic",this.mWIID);
this.mTrafficButton.setAttribute("hidden",trafficHidden);]]></body>
            </method>

            <method name="wdgtxConstructor">
                <body><![CDATA[if (this._wdgtxConstructed)
return;
this._wdgtxConstructed = true;
var settings = this.module.API.Settings;
this.mWeatherSliceOnly = ! settings.getValue("openWeatherPageOnClick");
this.mTrafficSliceOnly = ! settings.getValue("openTrafficPageOnClick");
this.module.API.Settings.observeChanges(this,this.mWIID);
this._setElementsVisibility();
this.resetRequest();
this.updateData();]]></body>
            </method>

            <method name="wdgtxDestructor">
                <body><![CDATA[if (! this._wdgtxConstructed)
return;
this._wdgtxConstructed = false;
this.removeRequest();
this.module.API.Settings.ignoreChanges(this,this.mWIID);]]></body>
            </method>

            <constructor><![CDATA[this.wdgtxConstructor();]]></constructor>

            <destructor><![CDATA[this.wdgtxDestructor();]]></destructor>

            <method name="_setElementsVisibility">
                <body><![CDATA[var settings = this.module.API.Settings;
this.mTitleButton.setAttribute("hidden",! settings.getValue("show-name",this.mWIID));
this.mWeatherButton.setAttribute("hidden",! settings.getValue("show-weather",this.mWIID));
this.mTrafficButton.setAttribute("hidden",! settings.getValue("show-traffic",this.mWIID));]]></body>
            </method>

            <method name="onSettingChange">
                <parameter name="aSettingName"/>
                <parameter name="aNewValue"/>
                <body><![CDATA[switch (aSettingName) {
case "id":
this.module.updateSlicesURLs(this.mWIID);
this.resetExportData();
this.resetRequest();
case "title":
this.updateData();
return;
case "update-interval":
this.resetRequest();
break;
case "openWeatherPageOnClick":
this.mWeatherSliceOnly = ! aNewValue;
break;
case "openTrafficPageOnClick":
this.mTrafficSliceOnly = ! aNewValue;
break;
}

this._setElementsVisibility();]]></body>
            </method>

            <field name="_loadingData">false</field>
            <field name="_loadingTimestamp">0</field>
            <field name="_loadingTimerId"/>

            <property name="loadingData">
                <getter><![CDATA[return this._loadingData;]]></getter>
                <setter><![CDATA[var setAttr = (function _setAttr() {
this.mWeatherButton && this.mWeatherButton.setAttribute("loading",val);
this.mTrafficButton && this.mTrafficButton.setAttribute("loading",val);
}
).bind(this);
this._loadingData = val;
if (this._loadingTimerId)
clearTimeout(this._loadingTimerId);
if (val)
{
setAttr();
this._loadingTimestamp = Date.now() + 700;
}
else
{
this._loadingTimerId = setTimeout(setAttr,Math.max(0,this._loadingTimestamp - Date.now()));
}
]]></setter>
            </property>

            <method name="_showWidgetMenu">
                <parameter name="aEvent"/>
                <body><![CDATA[this.mTitleMenu.openPopup(this.mTitleButton,"after_start");]]></body>
            </method>

            <method name="_onWeatherButtonCommand">
                <parameter name="aEvent"/>
                <body><![CDATA[this.mWeatherSliceOnly ? this._showWeatherSlice() : this._goToWeatherService(aEvent);]]></body>
            </method>

            <method name="_showWeatherSlice">
                <body><![CDATA[this.module.API.Statistics.logCustomAction(10);
var weatherSlice = this.module.weatherSlices[this.mWIID];
weatherSlice.notify("weather-before-popup");
var button = this.mWeatherButton;
weatherSlice.show(button,function onHide() button.checked = false);]]></body>
            </method>

            <method name="_goToWeatherService">
                <parameter name="aEvent"/>
                <body><![CDATA[var data = this.getExportData();
var weatherData = data && data.values.weather;
var url = weatherData && weatherData.url || this.module.DEFAULT_WEATHER_URL;
this.module.API.Controls.navigateBrowser({
unsafeURL: url,
eventInfo: aEvent});]]></body>
            </method>

            <method name="_onTrafficButtonCommand">
                <parameter name="aEvent"/>
                <body><![CDATA[this.mTrafficSliceOnly ? this._showTrafficSlice() : this._goToTrafficService(aEvent);]]></body>
            </method>

            <method name="_showTrafficSlice">
                <body><![CDATA[this.module.API.Statistics.logCustomAction(1);
var trafficSlice = this.module.trafficSlices[this.mWIID];
trafficSlice.notify("traffic-before-popup");
var button = this.mTrafficButton;
trafficSlice.show(button,function onHide() button.checked = false);]]></body>
            </method>

            <method name="_goToTrafficService">
                <parameter name="aEvent"/>
                <body><![CDATA[var data = this.getExportData();
var trafficData = data && data.values.traffic;
var url = trafficData && trafficData.url || this.module.DEFAULT_TRAFFIC_URL;
this.module.API.Controls.navigateBrowser({
unsafeURL: url,
eventInfo: aEvent});]]></body>
            </method>
        </implementation>

        <handlers>
            <handler button="1" event="click"><![CDATA[switch (event.originalTarget) {
case this.mWeatherButton:
this._goToWeatherService(event);
break;
case this.mTrafficButton:
this._goToTrafficService(event);
break;
default:
break;
}
]]></handler>
        </handlers>
    </binding>
</bindings>
