var Toolsearch = {
  // the toolname and category for which an 
  // HTTP request has been initiated
  httpRequestQueryString: undefined,

  // The object containg the cache as an assoc array
  // with: queryString => HTML
  cache: undefined,

  // URL to the PHP page called for receiving 
  // suggestions for tools
  fUrl: '../../../index.php',

  // time for last keystroke, is used to wait so that we
  // do not process HTTP request while the user is typing
  lastKS: new Date().getTime(),

  // initializing the toolsearch
  init: function() 
  {
    Toolsearch.cache = new Object();
    Toolsearch.lookup_toolname();
    $('search_toolname').setAttribute('autocomplete', 'off');
  },

  lookup_toolname: function() 
  {
    // if has there been less than 250ms since the 
    // last keystroke, we return
    if(!( new Date().getTime() - Toolsearch.lastKS > 250 )) {
      return;
    }

    // return if there is no input
    if ($F('search_toolname') == "" && $F('search_toolcategory') == 0)
    {
      return;
    }

    var current_vars = {
        searchtext: $F('search_toolname'),
        category_id: $F('search_toolcategory')
      };

    var h = $H(current_vars);
    var queryString = h.toQueryString();

    if (Toolsearch.httpRequestQueryString == queryString) {
      return;
    }

    if (Toolsearch.check_cache(queryString)) {
      Toolsearch.showResponse(Toolsearch.cache[queryString]);
      return;
    }

    // add the querystring to the temporary cache, so that we can
    // cache the result from the HTTP request
    Toolsearch.httpRequestQueryString = queryString;

    var pars = 'module=FreaksTools&func=autocomplete&' + queryString;

    var myAjax = new Ajax.Request(
        Toolsearch.fUrl, 
        {
          method: 'get',
          parameters: pars,
          onComplete: Toolsearch.ajaxResponse
        }
      );
  },

  ajaxResponse: function(originalRequest) {
    // add the response to the cache if it ain't there from before
    if (Toolsearch.httpRequestQueryString != undefined 
        && !Toolsearch.check_cache(Toolsearch.httpRequestQueryString)) {

      Toolsearch.cache[Toolsearch.httpRequestQueryString] = originalRequest.responseText;
    }

    // show the response
    Toolsearch.showResponse(originalRequest.responseText);
  },

  check_cache: function(queryString) {
    return Toolsearch.cache[queryString] != undefined;
  },

  showResponse: function(responseText) 
  {
    var div = $('toolsearch_options');
    div.innerHTML = responseText;
    div.style.border = "1px solid rgb(196, 196, 195)";
    div.style.backgroundColor = "rgb(233, 241, 243)";

    // We must re-apply the CSS rules when we alter the DOM
    Behaviour.apply(ToolsearchRules);
  }
}

var ToolsearchRules = {
  '#search_toolname': function( element )
  {
    element.onkeyup = function() {
      // look up the toolname every time we have 
      // changed the toolname-input

      // add time for last keystroke
      Toolsearch.lastKS = new Date().getTime();

      // timeout, so that user can type more
      // if the user use more than 300ms to
      // type again, we will have a HTTP request
      setTimeout( Toolsearch.lookup_toolname, 300 ); 
    }
  },

  '#search_toolcategory': function( element )
  {
    element.onclick = function() {
      Toolsearch.lookup_toolname();
    }
  }
}

Behaviour.register(ToolsearchRules);
register_onload_function(Toolsearch.init);
