/*
makeSearchCommand({
  name: "newzbin",
  url: "http://v3.newzbin.com/search/query/p/?q={QUERY}&searchaction=Search&fpn=p&category=-1&area=-1&u_nfo_posts_only=0&u_url_posts_only=0&u_comment_posts_only=0&u_v3_retention=9504000&sort=date&order=desc&areadone=-1",
  icon: "http://www.newzbin.com/favicon.ico",
  description: "Searches Newzbin for your search term."
});

makeSearchCommand({
  name: "torrent-search",
  url: "http://thepiratebay.org/search/{QUERY}/0/3/0",
  icon: "http://thepiratebay.org/favicon.ico",
  description: "Searches The Pirate Bay for your search term.",
*/
  /*
  preview: function(pBlock, directObj) {
    if (directObj.text)
      pBlock.innerHtml = "Searches The Pirate Bay for " + directObj.text;
    else
      pBlock.innerHTML = "Searches The Pirate Bay for the given words.";
  }
	*/
	/*
});
*/
CmdUtils.CreateCommand({
    name: "torrents",
    icon: "http://www.newzbin.com/favicon.ico",
    takes: {"search_string": noun_arb_text},

    description: "Searches Newzbin, The Pirate Bay, and Mininova in new tabs.",

    author: { name: "Craig Patik", email: "craig@patik.com"},
    homepage: "http://www.patik.com/ubiquity/",
    license: "Public domain",

    preview: "Searches Newzbin, The Pirate Bay, and Mininova in new tabs.",
    execute: function( directObj ) {
       var search_string = encodeURIComponent(directObj.text);
       var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"]
         .getService(Components.interfaces.nsIWindowMediator);
       var browserWindow = windowManager.getMostRecentWindow("navigator:browser");
       var browser = browserWindow.getBrowser();
  
       browser.loadOneTab("http://v3.newzbin.com/search/query/p/?q=" + search_string + "&searchaction=Search&fpn=p&category=-1&area=-1&u_nfo_posts_only=0&u_url_posts_only=0&u_comment_posts_only=0&u_v3_retention=9504000&sort=date&order=desc&areadone=-1", null, null, null, false, false);
       browser.loadOneTab("http://thepiratebay.org/search/" + search_string + "/0/3/0", null, null, null, false, false);
       browser.loadOneTab("http://www.mininova.org/search/?search=" + search_string, null, null, null, false, false);
     }
});

CmdUtils.CreateCommand({
  name: "date",
  
  _date: function(){
    var date = new Date();
    return date.toLocaleDateString();
  },
  
  preview: function( pblock ) {
    var msg = 'Inserts todays date: "<i>${date}</i>"';
    pblock.innerHTML = CmdUtils.renderTemplate( msg, {date: this._date()} );
  },
  
  execute: function() {
    CmdUtils.setSelection( this._date() );
  }
});


/* Google unit conversion */

CmdUtils.CreateCommand({
  author: { name: "George Alexander", email: "blipo.a@gmail.com"},
  license: "Creative Commons 3.0 (by nc)",
  name: "cnvrt",
  takes: {"unit1 TO unit2": noun_arb_text},

  preview: function(pBlock, obj)
  {

    pBlock.innerHTML = "Convert: "+obj.text;


    if(obj.text.length > 1)
    {
      pBlock.innerHTML = "Searching...";
      jQuery.get("http://www.google.com/search?q=convert+"+obj.text, find);
    }


    function find(page)
    {
      var start = page.search("class=r>")+25;
      var end = page.search("</h2>")-4;
      var lngth = end - start;

      if(start!=-1 && end!=-1)
      {
        pBlock.innerHTML = page.substr(start,lngth);
      }

      else
        {
          pBlock.innerHTML = "sumtin brokd";
        }

      }

    }
  });


/* isgd Command from http://gist.github.com/13619 */

CmdUtils.CreateCommand({
  name: "isgd",
  author: {name: "Thomas Brownback", homepage: "http://thomasbrownback.com", blog: "http://theorybloc.com"},
  thanks: {first: "Aza Raskin", foremost: "Aza Raskin", honorableMention: "geero.net", lastAmpBangLeast: "users like you"},
  license: "PUBLIC DOMAIN: Any and all rights herein are hereby donated to the commons.",
  description: "Shortens a URL using is.gd.",
  help: "Replaces a selection with (or simply inserts) a shortened URL from is.gd.",
  takes: {"<i>url to shorten</i>": noun_arb_text},

/*
TODO
1) I'd really like to just call one function in both the preview and the execution, but haven't figured out how yet while still maintaining dynamic AJAXy previews. Something like this, except with setSelection instead of displayMessage:
  _shortenUrl: function( urlToShorten ) {
    var baseUrl = "http://is.gd/api.php";
    var params = {longurl: urlToShorten.text};
    var shortUrl = jQuery.get( baseUrl, params, function ( isgdResponse ) {
      displayMessage(isgdResponse);
      })
  },

2) Give an option to copy to clipboard instead of replacement/insertion.
I think geero.net had some ideas on that.
*/

  preview: function(pBlock, longUrl) {
    if (!longUrl.text) { 
      pBlock.innerHTML = '<b>Shortens a URL using is.gd.</b><br><i>Upon execution, isgd replaces the selection with a shortened URL, or if nothing is selected, simply inserts the shortened URL at the cursor.</i>';
    } else {
    var previewTemplate = "Hang on while I ask is.gd about <i>${query}</i>."; 
    var previewData = {query: longUrl.text}; 
    pBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData); 
    };

    var params = { longurl: longUrl.text}; 
    var isgdURL = 'http://is.gd/api.php?' + jQuery.param( params ); 
    jQuery.ajax({ 
      type: "GET", 
      url: isgdURL, 
      success: function(searchResponse) { 
        pBlock.innerHTML = "<i>" + longUrl.text + "</i> shortens to:<br>" + searchResponse;
      } 
    });
  }, 

  execute: function( urlToShorten ) {
    var baseUrl = "http://is.gd/api.php";
    var params = {longurl: urlToShorten.text};
    var shortUrl = jQuery.get( baseUrl, params, function ( isgdResponse ) {
      //displayMessage("replacing with " + isgdResponse); //this is probably too noisy
      CmdUtils.setSelection( isgdResponse);
      })
  }
})
