CmdUtils.CreateCommand({
  name: "pause-TSO",
  author: { name: "Jean-Philippe Martin", email: "pandamail@fastmail.fm"},
  contributors: ["JP Martin"],
  license: "BSD",
  description: "pause/restarts the music from TheSixtyOne.com",
  homepage: "http://www.jpemartin.com/ubiq/pause-TSO.html",
  //help: '',

  preview: function(pbox,arg) {
    var text = 'Press ENTER to ';
    var v = this._findT61();
    if (v==null) return this._say(pbox,text+"(try to) pause the current song."); 
    v = v.current_song;
    if (v==null) return this._say(pbox,text+"(try to) pause the current song.");
    if (v.playing) text += 'pause ';
    else text += 'resume ';
    text += '"'+v.name+'" by '+v.artist;
    if (null!=v.photo_base_url) text += '<br/><img src="'+v.photo_base_url+'_small"/>'
    this._say(pbox, text); 
  },

  _say:function(pbox, text) {
    pbox.innerHTML = text;
  },

  execute: function() {
     var v = this._findT61(); //CmdUtils.getWindowInsecure().t61;
     if (null==v) { displayMessage("Could not find TheSixtyOne.com web page"); return; }
     var song = v.current_song;
     if (null==song) return; // already stopped
     if (song.playing) song.pause();
     else song.play();
  },

  _findT61:function() {
     var v = CmdUtils.getWindowInsecure().t61;
     //if (null!=v) return v;
     for( var j=0; j < Application.windows.length; j++ ) {
       var window = Application.windows[j];
       v = window.t61;
       if (null!=v) return v;
       for (var i = 0; i < window.tabs.length; i++) {
         var scope = window.tabs[i].document.defaultView.wrappedJSObject;
         v = scope.t61;
         if (null!=v) return v;
       }
     }
     return null; // not found
  },

});
