/*
 * jQuery UI Autocomplete
 * version: 1.0 (1/2/2008)
 * @requires: jQuery v1.2 or later, dimensions plugin
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Copyright 2007 Yehuda Katz, Rein Henrichs
 */
/*extern found jQuery*/

(function(jQuery) {
 
  jQuery.autocomplete = jQuery.autocomplete || {};
  jQuery.autocomplete.ext = jQuery.autocomplete.ext || {};
  
  jQuery.autocomplete.ext.ajax = function(opt) {
    var ajax = opt.ajax;
    return { getList: function(input) { 
      found = false;
      if (jQuery.jCache && opt.cacheSize > 0) {
        if (jQuery.jCache.hasItem(input.val())) {
          found = true;
          input.trigger("updateList", [jQuery.jCache.getItem(input.val())]);
        } 
      }  
      if (!found) {
	      jQuery.getJSON(ajax, "s=" + encodeURIComponent(input.val()), function(json) {
	        if (jQuery.jCache && opt.cacheSize > 0) {
	          jQuery.jCache.setItem(input.val(), json);
	        }
	        input.trigger("updateList", [json]); 
	      }); 
	    }
	  }};
  };
  
  jQuery.autocomplete.ext.templateText = function(opt) {
    var template = jQuery.makeTemplate(opt.templateText, "<%", "%>");
    return { template: function(obj) { return template(obj); } };
  };
  
})(jQuery);
