/*
 * This is an automaic generated Javascript file from on-page CMS.
 */

/* New uncompressed script block */
jQuery.fn.autocomplete = function(url, settings ) 
{
	return this.each( function()//do it for each matched element
	{
		var textInput = jQuery(this);
		textInput.attr('autocomplete', 'off');
		textInput.blur(function() {
			checkblur();
		});
		textInput.focus(function() {
			checkblur(true);
		});
		textInput.after('<ul class="autocomplete" style="display:none;" role="region" aria-live="assertive" aria-atomic="true" aria-relevant="additions removals"></ul>');
		textInput.parent().addClass('autocomplete-wrapper');
		var list = textInput.next();
		var oldText = '';
		var typingTimeout;
		var size = 0;
		var selected = 0;

		settings = jQuery.extend(//provide default settings
		{
			minChars : 1,
			timeout: 1000,
			after : null,
			before : null,
			validSelection : true,
			parameters : {'inputName' : textInput.attr('name'), 'inputId' : textInput.attr('id')}
		} , settings);

		function getData(text)
		{
			window.clearInterval(typingTimeout);
			if (text != oldText && (settings.minChars != null && text.length >= settings.minChars))
			{
				if (settings.before == "function") 
				{
					settings.before(textInput,text);
				}
				textInput.addClass('autocomplete-loading');
				settings.parameters.text = text;
				jQuery.getJSON(url,settings.parameters,function(data)
				{
					var items = '';
					if (data)
					{
						//console.log(data);
						size = data.length;
						for (i = 0; i < data.length; i++)
						{
							jQuery.each(data[i], function(i, val) {

								items += '<li>' + val + '</li>';
							});
							list.html(items);
							jQuery('ul.autocomplete > li').children('a').bind('blur', function() {
								checkblur();
							});
							jQuery('ul.autocomplete > li').children('a').bind('focus', function() {
								checkblur(true);
							});
							list.show("fast");
						}
						if (settings.after == "function") 
						{
							settings.after(textInput,text);
						}
					}
					textInput.removeClass('autocomplete-loading');
				});
				oldText = text;
			}
		}
		
		function clear()
		{
			list.hide("slow");
			size = 0;
			selected = 0;
		}	

		function checkblur(hasfocus)
		{
			if(hasfocus)
			{
				list.stop('queue');
				return;
			}
			list.delay(100, 'queue').hide("slow");
		}	
		
		textInput.keydown(function(e) 
		{
			window.clearInterval(typingTimeout);
			if(e.which == 27)//escape
			{
				clear();
			}
			else if(e.which == 13)//enter 
			{ 
				clear();
				return true;
			}
			else
			{ 
				typingTimeout = window.setTimeout(function() { getData(textInput.val()) },settings.timeout);
			}
		});
	});
};

jQuery(document).ready(function($) {

	$("input.suggest").autocomplete(abs_cms_path + "ajax.php?module=autosuggest_index_search&method=autocomplete",{minChars:3,timeout: 200});
});


/* EOF */
