/*/
/ Google API in ChatZilla
/ http://rdmsoft.com/
/ based on http://www.mozilla.org/projects/webservices/examples/babelfish-wsdl/
/*/

plugin.id = "googleapi";

var proxy    = null;
var wsdl_uri = "http://api.google.com/GoogleSearch.wsdl";


plugin.init =
function init(glob)
{
    plugin.major = 0;
    plugin.minor = 1;
    plugin.version = plugin.major + "." + plugin.minor;
    plugin.description = "Searches Google."
    plugin.prefary = plugin.prefary.concat([["googleapikey", "undefined", ""]]);
}

plugin.disable =
function disable()
{

    uncmdary = [
        {name: 'googlesearch'},
    ]

    client.commandManager.removeCommands(uncmdary);

    display(plugin.id+' - plugin disabled');

    return true;
}

plugin.enable =
function enable()
{

    var cmdary =
    [
     ["googlesearch", RDM_doGoogle, CMD_NEED_CHAN, "<googlequery>"],
    ]

    client.commandManager.argTypes["googlequery"] = 
        client.commandManager.argTypes["rest"];
    client.commandManager.defineCommands(cmdary);

    client.commandManager.commands.googlesearch.help = "Searches Google using the specified query, and shows the URL of the first result.";

    display(plugin.id+' - plugin enabled');

    return true;
}

    function RDM_doGoogle (e) 
    {
        if(plugin.prefs["googleapikey"] == "undefined")
        {
            display("No Google API Key set. Get a key from http://api.google.com/createkey and use the command '/plugin-pref googleapi googleapikey <yourkey>'."); return;
        }
      var aValue = e.googlequery;
      if (!proxy) {
        var listener = { 
          onLoad: function (aProxy) 
          {
            proxy = aProxy;
            proxy.setListener(listener);
            requestResults(aValue);
          },
         
          onError: function (aError) 
          {
            display("Error retrieving Google results: " + aError, "ERROR");
          },
      
          doGoogleSearchCallback : function (gresult) 
          {
              e.channel.dispatch("say",{message: "Top Google result for '" +
                  gresult.searchQuery + "': <" +
                  gresult.resultElements[0].getProperty("URL") + ">." });
          }
        };
        createProxy(listener);
      }
      else {
        requestResults(aValue);
      }
    }

    function createProxy(aCreationListener) 
    {
      try {
        var factory = new WebServiceProxyFactory();
        factory.createProxyAsync(wsdl_uri, "GoogleSearchPort", "", true, aCreationListener);
      }
      catch (ex) {
        alert(ex);
      }
    }

    function requestResults (value) 
    {
      if (proxy) {
	var key = plugin.prefs["googleapikey"];
	var q = value;
	var start = "value";
	var maxResults = 10;
	var filter = false;
	var restrict = "";
	var safeSearch = false;
	var lr = "";
	var ie = "UTF-8";
	var oe = "UTF-8";
        proxy.doGoogleSearch(key, q, start, maxResults, filter, restrict,
            safeSearch, lr, ie, oe);
      }
      else {
        alert("Error: Proxy set up not complete!");
      }
    }