Autocompleter.DWR = Class.create();
Autocompleter.DWR.prototype = Object.extend(new Autocompleter.Base(), {
   initialize: function(element, update, populator, options) {
     this.baseInitialize(element, update, options);
     this.options.array = new Array(0);
	 this.myElement = element;
	 this.populator = populator;
     if (this.options.afterUpdateElement ) {
             this.afterUpdateCallback = this.options.afterUpdateElement;
             this.options.afterUpdateElement = this.afterUpdateElement.bind(this);
         }
   },

   // called by the autocompleter on an event.
   getUpdatedChoices: function() {
     this.populator(this, this.getToken()); // this is the populator specified in the constructor.
   },

    afterUpdateElement: function(element,selectedElement) {
         this.afterUpdateCallback(element, selectedElement, this.options.array[this.index]);
     },

	/*setChoices: function(array) {
	 this.options.array = array;
	 //if a single result is returned auto populate
	 if(this.options.array.length == 1) {
		 //alert(this.options.array.length);
		 alert(this.myElement);
		 //alert(this.afterUpdateCallback)
		 alert(this.options.array[0]);
		 //alert(this.getCurrentEntry());
		 this.afterUpdateCallback(this.myElement, null, this.options.array[0]);
	 }

	 this.updateChoices(this.options.selector(this));
   },*/
   // should be called by the populator (specified in the constructor)
   setChoices: function(array) {
	 this.options.array = array;
	 this.updateChoices(this.options.selector(this));
   },

   setOptions: function(options) {
	 this.options = Object.extend({
       choices: 100,
       partialSearch: true,
       partialChars: 2,
       ignoreCase: true,
       fullSearch: true,
       selector: function(instance) {
		 var ret       = []; // Beginning matches
         var partial   = []; // Inside matches
         var entry     = instance.getToken();
         var count     = 0;
		 var valueSelector = instance.options.valueSelector; //function(){return "checkit"}//
		 for (var i = 0; i < instance.options.array.length && ret.length < instance.options.choices; i++)
		 {
           var elem = valueSelector(instance.options.array[i]);
		   //alert("Element: " + elem);
		   var foundPos = instance.options.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase()):elem.indexOf(entry);
		   if(foundPos != -1)
		   {
			   while (foundPos != -1)
			   {
			     if (foundPos == 0 && elem.length != entry.length) {
				   ret.push("<li class='product'><strong>" + elem.substr(0, entry.length) + "</strong>" + elem.substr(entry.length) + "</li>");
				   break;
				 } else if (entry.length >= instance.options.partialChars &&
				   instance.options.partialSearch && foundPos != -1) {
				   if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1)))
				   {
					 //partial.push("<li class='product'>" + elem.substr(0, foundPos) + "<strong>" + elem.substr(foundPos, entry.length) + "</strong>" +
					 ret.push("<li class='product'>" + elem.substr(0, foundPos) + "<strong>" + elem.substr(foundPos, entry.length) + "</strong>" +
					 elem.substr(foundPos + entry.length) + "</li>");
					 break;
				   }
				 }
				 foundPos = instance.options.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : elem.indexOf(entry, foundPos + 1);
			   }
			}
		    else
		    {
			   ret.push("<li class='product'><strong>" + elem.substr(0, entry.length) + "</strong>" + elem.substr(entry.length) + "</li>");
		    }
			//ret.push("<li class='product'>" + elem + "</li>");
		 }
         if (partial.length)
           ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
		 return "<ul class='products'>" + ret.join('') + "</ul>";
       },
	   valueSelector: function(object){ return object; }
     }, options || {});
   }
});

/**
 * Create a useLoadingImage function in the DWRUtil.
 */
function useLoadingImage(imageSrc) {
  var loadingImage;
  if (imageSrc) loadingImage = imageSrc;
  else loadingImage = "/global/images/general/spinner.gif";
  DWREngine.setPreHook(function() {
    var disabledImageZone = $('disabledImageZone');
    if (!disabledImageZone) {
      disabledImageZone = document.createElement('div');
      disabledImageZone.setAttribute('id', 'disabledImageZone');
      disabledImageZone.style.position = "absolute";
      disabledImageZone.style.zIndex = "1000";
      disabledImageZone.style.left = "0px";
      disabledImageZone.style.top = "0px";
      disabledImageZone.style.width = "100%";
      disabledImageZone.style.height = "100%";
      var imageZone = document.createElement('img');
      imageZone.setAttribute('id','imageZone');
      imageZone.setAttribute('src',loadingImage);
      imageZone.style.position = "absolute";
      imageZone.style.top = "0px";
      imageZone.style.right = "0px";
      disabledImageZone.appendChild(imageZone);
      document.body.appendChild(disabledImageZone);
    }
    else {
      $('imageZone').src = loadingImage;
      disabledImageZone.style.visibility = 'visible';
    }
  });
  DWREngine.setPostHook(function() {
    $('disabledImageZone').style.visibility = 'hidden';
  });
}
