// Additional hacks to make the Autocompleter work better for tokenized use:
//
// by mezza
// 
// The selected choice replaces the entire element content, and all supplied tokens are matched


/* Below variables are using for display message in the search box on the fisheries page (site_controller.rb)*/
var updateDiv =''
/* End of the variables */

Autocompleter.Mezza = Class.create();
Autocompleter.Mezza.prototype = Object.extend(new Autocompleter.Base(), {
  initialize: function(element, update, array, options) {
  	 
	 if (update == "profile_lookup_autocomplete_site")
	 	updateDiv = update
		
  	this.baseInitialize(element, update, options);
    this.options.array = array;
  },
  
  updateElement: function(selectedElement) {
    if (this.options.updateElement) {
      this.options.updateElement(selectedElement);
      return;
    }
	this.element.value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');
	
	/* Below code is using for display message in the search box on the fisheries page (site_controller.rb)*/
	if(this.element.value == "Don't see what you're looking for? Press 'Go'" )
		this.element.value = ''
	else if(this.element.value.indexOf("Showing") != -1)
		this.element.value = ''
	/* End of the code */	
		
	this.element.focus();
    
    if (this.options.afterUpdateElement)
      this.options.afterUpdateElement(this.element, selectedElement);
  },

  getUpdatedChoices: function() {
    this.updateChoices(this.options.selector(this));
  },

  setOptions: function(options) {
    this.options = Object.extend({
      choices: 10,
      partialSearch: true,
      partialChars: 2,
      ignoreCase: true,
      fullSearch: false,
      selector: function(instance) {
        var ret       = []; // Beginning matches
        var partial   = []; // Inside matches
        var entry     = instance.getToken();
        var count     = 0;
        var token_array = [];
		
		var value = instance.element.value.toLowerCase();        
        token_array = value.split(" ");
					
		/* Below code is using for display message in the search box on the fisheries page (site_controller.rb)*/
		var counter = 0	
		var instance_choices = instance.options.choices;		
		if (updateDiv == "profile_lookup_autocomplete_site") {
			ret.push("<li class=\"highlightclass\">" + "Don't see what you're looking for? Press 'Go'" + "</li>");
			instance_choices = instance.options.choices + 1;
			for (var i = 0; i < instance.options.array.length; i++) {
				var elem = instance.options.array[i];
				for (var j = 0; j < token_array.size(); j++) {
					if (elem.toLowerCase().indexOf(token_array[j]) != -1) {
						counter = counter + 1;
					}
				}
			}
		}

		/* End of the code */
                total = 0;
		for (var i = 0; i < instance.options.array.length && ret.length < instance_choices ; i++) { 
		  var elem = instance.options.array[i];
                  var result_score = 0;
                  for (var j = 0; j < token_array.size(); j++) {
                    if (elem.toLowerCase().indexOf(token_array[j]) != -1)
                    {
			result_score++;
                    }
		  }
                  if (result_score >= token_array.size()){
		    ret.push("<li>" + elem + "</li>");
                    total = total + 1;
                  }
		}

		/* Below code is using for display message in the search box on the fisheries page (site_controller.rb)*/		
		if(total >= 10)
			ret.push("<li class=\"highlightclass\">" + "Showing 10 of " + counter + " possible matches"  + "</li>");
		/* End of the code */
			
		return "<ul>" + ret.join('') + "</ul>";
      }
    }, options || {});
  }
});

