/*
* AutoSuggest
* Copyright 2009-2010 Drew Wilson
* www.drewwilson.com
* code.drewwilson.com/entry/autosuggest-jquery-plugin
*
* Version 1.4 - Updated: Mar. 23, 2010
*
* This Plug-In will auto-complete or auto-suggest completed search queries
* for you as you type. You can add multiple selections and remove them on
* the fly. It supports keybord navigation (UP + DOWN + RETURN), as well
* as multiple AutoSuggest fields on the same page.
*
* Inspied by the Autocomplete plugin by: Jšrn Zaefferer
* and the Facelist plugin by: Ian Tearle (iantearle.com)
*
* This AutoSuggest jQuery plug-in is dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($){
$.fn.autoSuggest = function(data, options) {
var defaults = {
asHtmlID: false,
asHtmlName: false,
newValuesInputName: false,
startText: "Enter Name Here",
emptyText: "No Results Found",
preFill: {},
limitText: "No More Selections Are Allowed",
selectedItemProp: "value", //name of object property
selectedValuesProp: "value", //name of object property
searchObjProps: "value", //comma separated list of object property names
queryParam: "q",
retrieveLimit: false, //number for 'limit' param on ajax request
extraParams: "",
matchCase: false,
minChars: 1,
keyDelay: 400,
resultsHighlight: true,
neverSubmit: false,
selectionLimit: false,
showResultList: true,
start: function(){},
selectionClick: function(elem){},
selectionAdded: function(elem){},
selectionRemoved: function(elem){ elem.remove(); },
formatList: false, //callback function
beforeRetrieve: function(string){ return string; },
retrieveComplete: function(data){ return data; },
resultClick: function(data){},
resultsComplete: function(){}
};
var opts = $.extend(defaults, options);
var d_type = "object";
var d_count = 0;
if(typeof data == "string") {
d_type = "string";
var req_string = data;
} else {
var org_data = data;
for (k in data) if (data.hasOwnProperty(k)) d_count++;
}
if((d_type == "object" && d_count > 0) || d_type == "string"){
return this.each(function(x){
if(!opts.asHtmlID){
x = x+""+Math.floor(Math.random()*100); //this ensures there will be unique IDs on the page if autoSuggest() is called multiple times
var x_id = "as-input-"+x;
var as_values_name = "as_values_"+x;
} else {
x = opts.asHtmlID;
var x_id = x;
var as_values_name = "as_values_"+x;
}
if(opts.asHtmlName){
var as_values_name = opts.asHtmlName;
}
opts.start.call(this);
var input = $(this);
input.attr("autocomplete","off").addClass("as-input").attr("id",x_id).val(opts.startText);
var input_focus = false;
// Setup basic elements and render them to the DOM
input.wrap('
').wrap('');
var count_of_new = 0;
var selections_holder = $("#as-selections-"+x);
var org_li = $("#as-original-"+x);
var results_holder = $('').hide();
var results_ul = $('