/**

	jSimpleSuggest 0.1
	----------------------------------------------------------------

	Copyright (c) 2009 Giorgio Passoni

	Licensed under MIT

	Permission is hereby granted, free of charge, to any person
	obtaining a copy of this software and associated documentation
	files (the "Software"), to deal in the Software without
	restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following
	conditions:
	
	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	OTHER DEALINGS IN THE SOFTWARE.

 */

/**
 * attach SimpleSuggest to Text Input
 */
jQuery.fn.SimpleSuggest = function(options) {

	// Set up default values for options		
	var defaults = {
		
		proxy: '',
		proxy_params: {},
		proxy_method: 'post',
       	
		list_width: 183,
		
		data: [],
		save_dataid: true
	};
	var options = jQuery.extend(defaults, options);

	// Catch the text input
	var textInput = jQuery(this);

	// If key pressed fire up the suggest		
	jQuery(textInput).keyup(function() {
		// Reset the rel attr
		jQuery(textInput).attr("rel","");
		// Check if json proxy is active
		if (options.proxy !== false) {
			// Add the query string to params
			options.proxy_params["query"] = jQuery(textInput).val();
			// Make the ajax call
			jQuery.ajax({
				type: options.proxy_method,
				url: options.proxy,
				data: options.proxy_params,
				dataType: 'json',
				success: function(data) {
					// Create the suggest list
					var top = jQuery(textInput).position().top + jQuery(textInput).height() + 5;
					var left = jQuery(textInput).position().left;
					// Remove all instances of list
					jQuery('.suggestList').remove();
					// If there is at least 1 matched element..
					if (data.length > 0) {
						var suggestList = "<ul class='suggestList' style='top: "+top+"px; left: "+left+"px; width: "+options.list_width+"px;'>";
						for (i=0; i<data.length; i++) {
							var regex = new RegExp(options.proxy_params["query"], "g");
							suggestList += "<li class='suggestList-item' rel='"+data[i].id+"'>"+data[i].full_name.replace(regex, "<b>" + options.proxy_params["query"] + "</b>")+"</li>";
						}
						suggestList += "</ul>"
						jQuery(textInput).parent().append(jQuery(suggestList));
						jQuery(".suggestList-item").click(function() {
							// Detect tags
							var matchTag = /<(?:.|\s)*?>/g;
							jQuery(textInput).val(jQuery(this).html().replace(matchTag, ''));
							jQuery(textInput).attr("rel",jQuery(this).attr("rel"));
							jQuery('.suggestList').remove();
						});
					}
				}
			});		
		}
	});
			
}
		
