<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE bindings SYSTEM "/barplayer.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 extends="//toolkit/native/bindings/widget.xml#widget" id="toolbarbutton">
        <resources>
            <stylesheet src="button.css"/>
        </resources>
        
        <content>
            <xul:toolbarbutton anonid="button" class="chromeclass-toolbar-additional toolbarbutton-1" oncommand="OnCommand(event)" paused="true" tooltiptext="&widget.tooltiptext.paused;">
                <xul:menupopup anonid="menupopup" noflash="true">
                    <xul:menuitem anonid="item_play" label="&widget.play;"/>
                    <xul:menuitem anonid="item_add" label="&widget.add;"/>
                    <xul:menuitem anonid="item_settings" label="&widget.settings;"/>
                    <xul:menuseparator anonid="item_ms1"/>
                    <xul:menuseparator/>
                    <xul:menuitem anonid="item_settings2" label="&widget.settings2;"/>
                </xul:menupopup>
            </xul:toolbarbutton>
        </content>
        <implementation implements="nsIDOMEventListener">
            <field name="player">this.application.player</field>
            <field name="settings">this.application.storage.settings</field>
            <field name="item_play">document.getAnonymousElementByAttribute(this, "anonid", "item_play")</field>
            <field name="menupopup">document.getAnonymousElementByAttribute(this, "anonid", "menupopup")</field>
            <field name="button">document.getAnonymousElementByAttribute(this, "anonid", "button")</field>
            <field name="settingsAPI">this.application.api.Settings</field>
            <field name="sepa">document.getAnonymousElementByAttribute(this, "anonid", "item_ms1")</field>
            <field name="observerService">Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);</field>

            <constructor><![CDATA[var $ = this;
var defaultObserver = {
QueryInterface: function defaultObserver_QueryInterface() {
return this;
}
,
observe: function defaultObserver_observe(subject, topic, data) {
try {
switch (topic) {
case "barplayer-on-settings-change":
$.OnSettingChange();
break;
case "barplayer-play":
$.Play();
break;
case "barplayer-pause":
$.Pause();
break;
case "barplayer-update":
$.UpdateMenu();
$.UpdateToolbar($.settings.selected);
break;
}

}
catch (e) {

}

;
}
};
this.observerService.addObserver(defaultObserver,"barplayer-on-settings-change",false);
this.observerService.addObserver(defaultObserver,"barplayer-play",false);
this.observerService.addObserver(defaultObserver,"barplayer-pause",false);
this.observerService.addObserver(defaultObserver,"barplayer-update",false);
this._window = null;
this.UpdateMenu();
this.UpdateToolbar(this.settings.selected);
this.paused = true;
if (! this.player.instances)
{
this.player.instances = 0;
}

this.player.instances++;]]></constructor>

            <destructor><![CDATA[this.observerService.notifyObservers(application,"barplayer-instance-closed",this.player.isPlay && ! this.player.isError ? 1 : 0);]]></destructor>
            
            <method name="OnSettingChange">
                <parameter name="name"/>
                <parameter name="value"/>
                <body><![CDATA[this.UpdateToolbar(this.settings.selected);]]></body>
            </method>
            
            <method name="UpdateMenu">
                <body><![CDATA[Array.slice(this.menupopup.childNodes).forEach(function (item) {
if (this.IsStation(item.getAttribute("anonid")))
item.parentNode.removeChild(item);
}
,this);
var stations = this.settings.stations;
var selected = this.settings.selected;
var count = 0;
for (let i = 0, len = stations.length;i < len;i++) {
let st = stations[i];
if (st.isDeleted)
{
continue;
}

count++;
let newItem = document.createElement("menuitem");
newItem.setAttribute("anonid","station_" + st.n);
newItem.setAttribute("label",st.name);
newItem.className = "menuitem-iconic";
if (st.name == selected)
{
newItem.setAttribute("selected","true");
newItem.setAttribute("playing",this.player.isPlay);
newItem.setAttribute("error",this.player.isError);
}

this.menupopup.insertBefore(newItem,this.sepa.nextSibling);
}

this.sepa.setAttribute("hidden",! count);]]></body>
            </method>
            
            <method name="UpdateToolbar">
                <parameter name="name"/>
                <body><![CDATA[
                    // update item_play label
                    let paused = !this.player.isPlay;
                    let st_count = this.player.GetStationsCount();
                    let noFlash = this.player.noflash == true;
                    let isError = this.player.isError;
                    let isThrobber = this.player.isThrobber && !noFlash;
                    
                    let show_name = noFlash ? "" : this.settingsAPI.getValue("show_name");
                    this.button.setAttribute("label", (name && show_name) ? name : show_name ? "]]>&widget.no-station;<![CDATA[" : "");
                    
                    this.button.setAttribute("throbber", isThrobber);
                    this.button.setAttribute("noflash", noFlash);
                    this.button.setAttribute("error", isError);
                    this.button.setAttribute("paused", paused);
                    
                    this.menupopup.setAttribute("noflash", noFlash);
                    if (noFlash) {
                        let altPopup = document.createElement("menupopup");
                        altPopup.setAttribute("anonid", "noflash-menupopup");
                        altPopup.setAttribute("noflash", "true");
                        
                        let altMenu = document.createElement("menuitem");
                        altMenu.setAttribute("label", "]]>&widget.noflash;<![CDATA[");
                        altMenu.setAttribute("anonid", "item_noflash");
                        
                        altPopup.appendChild(altMenu);
                        this.replacedPopup = this.button.replaceChild(altPopup, this.menupopup);
                    } else {
                        this.button.type = "menu-button";
                    }
                    
                    this.application.log("UpdateToolbar");
                    
                    if (this.item_play) {
                        this.item_play.setAttribute("label", paused || isError ? "]]>&widget.play;<![CDATA[" : "]]>&widget.pause;<![CDATA[");
                    }
                    
                    let ttext;
                    if (noFlash) {
                        ttext = "]]>&widget.noflash1;<![CDATA[" + "\n" + "]]>&widget.noflash2;<![CDATA[";
                    } else if (isError) {
                        ttext = "]]>&widget.tooltiptext.error;<![CDATA[";
                    } else if (isThrobber) {
                        ttext = "]]>&widget.tooltiptext.throbber;<![CDATA[";
                    } else {
                        if (st_count > 0) {
                            if (paused) {
                                ttext = "]]>&widget.tooltiptext.paused;<![CDATA[";
                                ttext += ("«" + this.settings.selected + "»");
                            } else {
                                let st = this.player.GetStationByName(this.settings.selected);
                                ttext = "]]>&widget.tooltiptext.normal1;<![CDATA[";
                                ttext += ("«" + this.settings.selected + "»\n");
                                if (st) {
                                    ttext += "]]>&widget.tooltiptext.source;<![CDATA[" + st.url + "\n";
                                }
                                ttext += "]]>&widget.tooltiptext.normal2;<![CDATA[";
                            }
                        } else {
                            ttext = "]]>&widget.tooltiptext.no-stations1;<![CDATA[" + "\n" + "]]>&widget.tooltiptext.no-stations2;<![CDATA[";
                        }
                    }
                    this.button.setAttribute("tooltiptext", ttext);
                    // this.player.UpdateSettingsWindow();
                ]]></body>
            </method>
            
            <method name="SelectStation">
                <parameter name="name"/>
                <body><![CDATA[this.application.api.Statistics.logCustomAction(113);
this.application.player.SetStation(name);]]></body>
            </method>
            
            <method name="PlayPause">
                <parameter name="override"/>
                <body><![CDATA[if (this.player.GetStationsCount() == 0)
{
this.OpenAddStationDialog();
}
else
{
let paused = (this.button.getAttribute("paused") || "true") == "true" || (this.button.getAttribute("error") || "true") == "true" || override;
paused ? this.application.player.Play() : this.application.player.Pause();
this.button.setAttribute("paused",! paused);
}

this.UpdateToolbar(this.settings.selected);]]></body>
            </method>
            
            <method name="Play">
                <body><![CDATA[
                    this.button.setAttribute("paused", "false");
                    this.item_play.setAttribute("label", "]]>&widget.play;<![CDATA[");
                ]]></body>
            </method>
            
            <method name="Pause">
                <body><![CDATA[
                    this.button.setAttribute("paused", "true");
                    this.item_play.setAttribute("label", "]]>&widget.pause;<![CDATA[");
                ]]></body>
            </method>
            
            <method name="IsStation">
                <parameter name="id"/>
                <body><![CDATA[return id && id.indexOf("station_") == 0;]]></body>
            </method>
                        
            <method name="OpenAddStationDialog">
                <body><![CDATA[var _window = this.player.openAddStationDialog();
if (_window)
{
_window.addEventListener("accept",this.OnAddStationAccept,false);
}
]]></body>
            </method>
            
            <method name="OnAddStationAccept">
                <parameter name="event"/>
                <body><![CDATA[var document = event.target.document;
var name = String(document.getElementById("station_name").value);
var url = String(document.getElementById("station_url").value);
var autoplay = document.getElementById("autoplay").checked;
this.parameters.application.api.Statistics.logCustomAction(112);
this.parameters.application.player.AddStation(name,url,autoplay);]]></body>
            </method>
            
            <method name="OpenSettingsDialog">
                <body><![CDATA[this.player.openSettingsDialog();]]></body>
            </method>
            
            <method name="OnCommand">
                <parameter name="event"/>
                <body><![CDATA[var target = event.target;
var anonid = target.getAttribute("anonid") || "#";
var name = target.getAttribute("label") || "#";
var noflash = this.player.noflash;
if (anonid != "#")
{
if (anonid == "button")
{
this.application.api.Statistics.logButtonClick();
}
else
{
this.application.api.Statistics.logMenuClick();
}

}

if (this.IsStation(anonid))
{
this.SelectStation(name);
this.PlayPause(true);
}
else
if (anonid == "button" || anonid == "item_play")
{
if (noflash)
{
this.player.getFlashPlayer();
}
else
{
this.PlayPause();
}

}
else
if (anonid == "item_add")
{
this.OpenAddStationDialog();
}
else
if (anonid == "item_noflash")
{
this.player.getFlashPlayer();
}
else
if (anonid == "item_settings")
{
this.OpenSettingsDialog();
}
else
if (anonid == "item_settings2")
{
this.application.api.Controls.openSettingsDialog(window,this.application.WIID);
}
else
{
this.application.log("onCommand: " + (target.getAttribute("anonid") || "#"));
}
]]></body>
            </method>
        </implementation>
    </binding>
    
    <binding extends="chrome://global/content/bindings/dialog.xml#dialog" id="addstation-dialog">
        <implementation>
            <field name="station_name">document.getElementById("station_name")</field>
            <field name="application">window.parameters.application</field>
            <field name="settings">window.parameters.application.storage.settings</field>
            <field name="button_accept">document.getAnonymousElementByAttribute(this, "dlgtype", "accept")</field>
            <field name="label_error1">document.getElementById("error-1")</field>
            <field name="label_error2">document.getElementById("error-2")</field>
            <field name="station_url">document.getElementById("station_url")</field>
            <field name="station_name">document.getElementById("station_name")</field>
            <constructor><![CDATA[
                let $ = this;
                let station_count = this.application.player.GetStationCount();
                let station_name_default = (this.station_name.getAttribute("prefix") || "") + station_count;
                
                this.station_name.setAttribute("value", station_name_default);
                this.button_accept.setAttribute("disabled", true);
                this.station_url.addEventListener("input", OnInput, true);
                this.station_name.addEventListener("input", OnInput, true);
                
                function OnInput(event) {
                    let url = $.station_url.value;
                    let name = $.station_name.value;
                    let exist = $.application.player.isStationExist(name);
                    let isValidURL = /^https?\:\/\/\w+/.test(url);
                    
                    $.button_accept.setAttribute("disabled", name.length == 0 || !isValidURL || exist);
                    
                    $.label_error1.value = exist ? "]]>&dialog.addstation.prefix;«<![CDATA[" + name + "]]>»&dialog.addstation.error2;<![CDATA[" : "";
                    $.label_error2.value = exist ? "]]>&dialog.addstation.error3;<![CDATA[" : "";
                }
            ]]></constructor>
            <destructor><![CDATA[]]></destructor>
        </implementation>
    </binding>
    
    <binding extends="chrome://global/content/bindings/dialog.xml#dialog" id="qd-dialog"/>
    
    <binding extends="chrome://global/content/bindings/dialog.xml#dialog" id="changestation-dialog">        
        <implementation>
            <field name="station_name">document.getElementById("station_name")</field>
            <field name="station_url">document.getElementById("station_url")</field>
            <field name="application">window.parameters.application</field>
            <field name="station">window.parameters.arguments.station</field>
            <field name="settings">window.parameters.application.storage.settings</field>
            <field name="button_accept">document.getAnonymousElementByAttribute(this, "dlgtype", "accept")</field>
            <constructor><![CDATA[var $ = this;
this.station_name.setAttribute("value",station.name);
this.station_url.setAttribute("value",station.url);
this.station_url.addEventListener("input",OnInput,true);
this.station_name.addEventListener("input",OnInput,true);
function OnInput(event) {
var txt = $.station_url.value;
var isHTTP = txt.indexOf("http://") == 0 || txt.indexOf("https://") == 0;
$.button_accept.setAttribute("disabled",txt.length == 0 || ! isHTTP);
}
]]></constructor>
                                
            <destructor><![CDATA[]]></destructor>
        </implementation>
    </binding>
    
    <binding id="settings-dialog">
        <implementation implements="nsISupports, nsIDOMEventListener">
            <field name="application">window.parameters.application</field>
            <field name="player">this.application.player</field>
            <field name="settings">window.parameters.application.storage.settings</field>
            <field name="st_list">document.getElementById("st_list")</field>
            <field name="arrow_up">document.getElementById("arrow_up")</field>
            <field name="arrow_down">document.getElementById("arrow_down")</field>
            <field name="button_play">document.getElementById("button_play")</field>
            <field name="button_add">document.getElementById("button_add")</field>
            <field name="button_change">document.getElementById("button_change")</field>
            <field name="button_delete">document.getElementById("button_delete")</field>
            <field name="tmr">null</field>
            <field name="tmr_count">0</field>
            <constructor><![CDATA[this.tmr = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
var context = this;
window.addEventListener("updatelist",function () {
context.OnUpdate(context);
}
,false);
this.st_list.addEventListener("click",function () {
context.OnCommand(context);
}
,false);
this.arrow_up.addEventListener("click",function () {
context.MoveUp(context);
}
,false);
this.arrow_down.addEventListener("click",function () {
context.MoveDown(context);
}
,false);
this.Update();]]></constructor>
            
            <method name="MoveUp">
                <body><![CDATA[if (this.st_list.selectedItem && ! this.arrow_up.disabled)
{
let st_name = this.st_list.selectedItem.childNodes[1].getAttribute("label") || "";
let idx = this.st_list.selectedIndex - 1;
if (idx >= 0)
{
this.player.MoveStation(st_name,- 1);
this.st_list.ensureIndexIsVisible(idx);
this.st_list.selectItem(this.st_list.getItemAtIndex(idx));
}

this.st_list.focus();
}
]]></body>
            </method>
            
            <method name="MoveDown">
                <body><![CDATA[if (this.st_list.selectedItem && ! this.arrow_down.disabled)
{
let st_name = this.st_list.selectedItem.childNodes[1].getAttribute("label") || "";
let idx = this.st_list.selectedIndex + 1;
if (idx < this.st_list.getRowCount())
{
this.player.MoveStation(st_name,1);
this.st_list.ensureIndexIsVisible(idx);
this.st_list.selectItem(this.st_list.getItemAtIndex(idx));
}

this.st_list.focus();
}
]]></body>
            </method>
            
            <method name="OnCommand">
                <body><![CDATA[if (this.tmr_count == 0)
{
let context = this;
this.tmr.initWithCallback(function () {
context.ClickChecker(context);
}
,200,Components.interfaces.nsITimer.TYPE_ONE_SHOT);
this.tmr_count++;
}
else
{
this.tmr_count++;
}
]]></body>
            </method>
            
            <method name="ClickChecker">
                <body><![CDATA[if (this.tmr_count > 1)
{
this.OnDoubleClick();
}
else
{
this.OnClick();
}
]]></body>
            </method>
            
            <method name="OnDoubleClick">
                <body><![CDATA[this.tmr_count = 0;
this.tmr.cancel();
this.OnChange();]]></body>
            </method>
                    
            <method name="OnClick">
                <body><![CDATA[
                    this.tmr_count = 0;
                    this.tmr.cancel();
                    
                    let idx = this.st_list.selectedIndex;
                    this.arrow_up.disabled = !(idx >= 0);
                    this.arrow_down.disabled = !(idx < (this.st_list.itemCount-1));
                    this.button_play.disabled = !(idx >= 0);
                    this.button_change.disabled = !(idx >= 0);
                    this.button_delete.disabled = !(idx >= 0);
                    
                    let item = this.st_list.getItemAtIndex(idx);
                    if (item) {
                        let isSelected = (item.childNodes[1].getAttribute("label") || "") == this.settings.selected;
                        let isPlay = this.player.isPlay;
                        this.button_play.setAttribute("status", String(isSelected && isPlay).toLowerCase());
                        this.button_play.setAttribute("label", isSelected && isPlay ? "]]>&widget.pause;<![CDATA[" : "]]>&widget.play;<![CDATA[");
                    }
                ]]></body>
            </method>
            
            <method name="Update">
                <body><![CDATA[while (this.st_list.itemCount != 0) {
this.st_list.removeItemAt(0);
}

for (let i = 0;i < this.settings.stations.length;i++) {
if (this.settings.stations[i].isDeleted)
{
continue;
}

let row = document.createElement("listitem");
let isSelected = this.settings.stations[i].name == this.settings.selected;
let isPlay = this.player.isPlay;
let cell = document.createElement("listcell");
cell.setAttribute("label",isSelected && this.st_list.getAttribute("playmark") || "");
row.appendChild(cell);
cell = document.createElement("listcell");
cell.setAttribute("label",this.settings.stations[i].name);
row.appendChild(cell);
cell = document.createElement("listcell");
cell.setAttribute("label",this.settings.stations[i].url);
row.appendChild(cell);
this.st_list.appendChild(row);
}
]]></body>
            </method>
            
            <method name="OnUpdate">
                <body><![CDATA[this.Update();]]></body>
            </method>
            
            <method name="OnClose">
                <body><![CDATA[window.close();]]></body>
            </method>
            
            <method name="OnChange">
                <body><![CDATA[var idx = this.st_list.selectedIndex;
if (idx != - 1)
{
let st_name = this.st_list.selectedItem.childNodes[1].getAttribute("label") || "";
this.application.player.station_lastname = st_name;
this.OpenChangeStationDialog(st_name);
}
]]></body>
            </method>
            
            <method name="OpenChangeStationDialog">
                <parameter name="name"/>
                <body><![CDATA[var _window = this.player.openChangeStationDialog(name);
if (_window)
{
_window.addEventListener("accept",this.OnChangeStationAccept,false);
}
]]></body>
            </method>
            
            <method name="OnChangeStationAccept">
                <parameter name="event"/>
                <body><![CDATA[var document = event.target.document;
var name = String(document.getElementById("station_name").value);
var url = String(document.getElementById("station_url").value);
var autoplay = document.getElementById("autoplay").checked;
this.parameters.application.player.ChangeStation(name,url,autoplay);]]></body>
            </method>
            
            <method name="OnAdd">
                <body><![CDATA[this.OpenAddStationDialog();]]></body>
            </method>
            
            <method name="OnPlay">
                <body><![CDATA[
                    this.application.log("selectedItem:" + this.st_list.selectedItem);
                    
                    if (this.st_list.selectedItem) {
                        let st_name = this.st_list.selectedItem.childNodes[1].getAttribute("label") || "";
                        let isPlay = this.player.isPlay;
                        let isSelected = (st_name == this.settings.selected);
                        this.application.log("isPlay:" + isPlay);
                        let idx = this.st_list.selectedIndex;
                        if (!isPlay || !isSelected) {
                            this.player.SetStation(st_name);
                            this.player.Play();
                            this.button_play.label = "]]>&widget.pause;<![CDATA[";
                        } else {
                            this.player.Pause();
                            this.button_play.label = "]]>&widget.play;<![CDATA[";
                        }
                    }
                    
                    this.Update();
                    this.st_list.ensureIndexIsVisible(idx);
                    this.st_list.selectItem(this.st_list.getItemAtIndex(idx));
                    this.st_list.focus();
                ]]></body>
            </method>
            
            <method name="DeleteQuestion">
                <body><![CDATA[this.OpenDeleteQuestion();]]></body>
            </method>
            
            <method name="OnDelete">
                <body><![CDATA[var idx = this.st_list.selectedIndex;
if (idx != - 1)
{
let st_name = this.st_list.selectedItem.childNodes[1].getAttribute("label") || "";
this.player.RemoveStation(st_name);
}
]]></body>
            </method>
            
            <method name="OpenAddStationDialog">
                <body><![CDATA[var $ = this;
var _window = this.player.openAddStationDialog();
if (_window)
{
_window.addEventListener("accept",$.OnAddStationAccept,false);
}
]]></body>
            </method>
            
            <method name="OpenDeleteQuestion">
                <body><![CDATA[this.player.settingsDialog.showModalDialog(this.application.api.Package.resolvePath("content/questdel.xul"));
if (this.player.settingsDialog.returnValue)
{
this.OnDelete();
}
]]></body>
            </method>
            
            <method name="OnAddStationAccept">
                <parameter name="event"/>
                <body><![CDATA[var document = event.target.document;
var name = String(document.getElementById("station_name").value);
var url = String(document.getElementById("station_url").value);
var autoplay = document.getElementById("autoplay").checked;
this.parameters.application.api.Statistics.logCustomAction(112);
this.parameters.application.player.AddStation(name,url,autoplay);]]></body>
            </method>
                    
            <destructor><![CDATA[]]></destructor>
        </implementation>
    </binding>
</bindings>
