var noun_type_drupal_ref = {
  _name: "function name",

  get_functions: function(text) {
    var functions = [];
    if(text.length > 0 && text !== 'drupal ') {
      jQuery.ajax({
        url: 'http://api.drupal.org/api/autocomplete/6/' + text,
        dataType: "json",
        async: false,
        success: function(data){
          for (var functionName in data) {
            functions.push(functionName);
          }
        }
      });
    }
    else {
      functions.push(' ');
    }

    return functions;
  },

  suggest: function( text, html ) {
    var suggestions  = [];
    var functions = noun_type_drupal_ref.get_functions(text);
    for (var i = 0; i < functions.length; i++) {
      suggestions.push(CmdUtils.makeSugg(functions[i]));
    }
    return suggestions;
  }
  
}

CmdUtils.CreateCommand({
  names: ["drupal"],
  arguments: [{role: 'object', nountype: noun_type_drupal_ref, label: 'query'}],
  homepage: "http://joshhuckabee.com/",
  author: { name: "Josh Huckabee", email: "joshhuckabee@gmail.com"},
  contributors: ["Josh Huckabee"],
  license: "MPL",

  preview: function( pblock, args ) {
    var functionName = args.object.text;
    if (functionName.length > 1){
      var msg = "Lookup <b style=\"color:yellow\">%s</b> in the Drupal API.";
      var data = {function: functionName};
      pblock.innerHTML = CmdUtils.renderTemplate(_("Lookup <b style=\"color:yellow\">${f}</b> in the Drupal API.", data));
    }
    else {
      pblock.innerHTML = _("Lookup a function the Drupal API.", {});
    }
  },

  execute: function(args) {
    var doc =  Application.activeWindow.activeTab.document;
    var apiURL = "http://api.drupal.org/api/search/6/" + args.object.text;
    Utils.openUrlInBrowser(apiURL);
  }
});

