noun_type_jd = new CmdUtils.NounType( "Selections", ["page", "tabs", "this", "all"]);

CmdUtils.CreateCommand({
  names: ["jdownloader"],
  icon: "http://jdownloader.org/_media/de/knowledge/wiki/jd_logo.png?w=16&h=16&cache=cache",
  homepage: "http://www.jdownloader.org/",
  author: { name: "scr4ve, JD-Team", email: "scr4ve@jdownloader.org"},
  license: "GPL",
  description: "Download files using JDownloader.",
  arguments: [{role: 'object', nountype: noun_type_jd, label: "this, tabs, page, all"}],
  getTabs: function(){
    var tabs  = [];
    for( var j=0; j < Application.windows.length; j++ ) {
      var window = Application.windows[j];
      for( var i=0; i < window.tabs.length; i++ ) {
        tabs.push(window.tabs[i]);
      }
    }
    CmdUtils.log(tabs);
    return tabs;
  },
  preview: function( pblock, args) {
    switch(args.object.text)
    {
      case "tabs":
        pblock.innerHTML = "Download all urls of your tabs.";
        break;
      case "page":
        pblock.innerHTML = "Download all urls on this page.";      
        break;
      case "this":
        pblock.innerHTML = "Download all urls from your current selection.";
        break;
      case "all":
        pblock.innerHTML = "Download all urls from your tabs and their contents.";
        break;
      default:
        pblock.innerHTML =
 "<table>"
+"<tr><td><b>&bdquo;this&ldquo;</b></td><td> to download all urls from your current selection</td></tr>"
+"<tr><td><b>&bdquo;all&ldquo;</b></td><td>  to download all urls of your tabs <b>with their contents</b></td></tr>"
+"<tr><td><b>&bdquo;tabs&ldquo;</b></td><td> to download all urls of your tabs <b>without their contents</b></td></tr>"
+"<tr><td><b>&bdquo;page&ldquo;</b></td><td> to download all urls on this page</td></tr></table>";           
        break;
    }
  },
  execute: function(args) {
    var toclip;
    switch(args.object.text)
    {
      case "all":
        tabs = this.getTabs();
        for (var i = 0; i < tabs.length; i++)
        {
          for (var j = 0; j < tabs[i].document.links.length; j++)
          {
            toclip += " " + tabs[i].document.links[j].href;
          }
        }
      case "tabs":
        (typeof tabs != 'undefined')
        { tabs = this.getTabs(); }

        for (var i = 0; i < tabs.length; i++)
        {
          CmdUtils.log(tabs[i]);
           toclip += " " + tabs[i].document.URL;
        }       
        break;
      
      case "page":
        toclip  = CmdUtils.getDocument().URL;
        for (var i = 0; i < CmdUtils.getDocument().links.length; i++)
        {
          toclip += " " + CmdUtils.getDocument().links[i].href;
        }
        break;
      
      case "this":
        toclip = CmdUtils.getHtmlSelection();
        break;
    }
    //Show Feedback (might be positive or negative)
    if (typeof toclip != 'undefined')
    {
      //Copy to clipboard
      CmdUtils.copyToClipboard(toclip);
      displayMessage("Make sure that your Clipboard-Detection is turned on! (Or press \"Add\" in JD manually)");
    }
    else
    {
      displayMessage("Which links should i download, huh?");
    }
}
});




