/* 
 * audi.jSuggest modification based on jSuggest V. 1.0 (May 26, 2008)
 * Minimised functional range.
 * Requires: jQuery 1.2.6+
 * @description:    Minimised functional range. Key handling reduced to ESC,
 * 					ENTER and general input. Removed cursor key handlings.
 * 					Extended markup generation.
 * 					Added 'prepareLink' calls for tracking.
 * @author: Jan Konzack
 * @version 1.0
 */
(function($) {
		  
	$.fn.jSuggest = function(options) {
		// merge users option with default options
		var opts = $.extend({}, $.fn.jSuggest.defaults, options);
		audi_ngw.livesearch.opts = opts;
		var jH = ".jSuggestHover";
		var jsH = "jSuggestHover";
		var iniVal = this.value;
		var textBox = this;
		var textVal = this.value;
		var jC = "#jSuggestContainer";
		var doreq;
		
		$("body").append('<div id="jSuggestContainer" class="audi_livesearch"></div>');
		$(jC).hide();
		$(this).bind("keyup click", function(e){
			textBox = this;
			textVal = this.value;
			if (this.value.length >= opts.minchar && $.trim(this.value)!="Search Terms") {
				var offSet = $(this).offset();
				
				$(jC).css({
					position: "absolute",
					top: offSet.top + $(this).outerHeight() + "px",
					left: offSet.left,
					width: $(this).outerWidth()-2 + "px",
					//opacity: opts.opacity,
					zIndex: opts.zindex
				}).show();
				
				// if escape key
				if (e.keyCode == 27 ) {
					$(jC).hide();
				}
				
				// if enter key
				else if (e.keyCode == 13 ) {
					if ($(jH).length == 1) {
						$(textBox).val($(jH).text());
						$(jC).hide();
						iniVal = textBox.value;
					}
				}
				
				// new query detected
				else if (textBox.value != iniVal){
					window.clearTimeout(doreq);
					
					iniVal = textBox.value;
					
					$(jC).find('ul').remove();
					
					opts.data = $(this).serialize();
					var _url = opts.url;
					if (_url.indexOf('?') !== -1) {
						_url += '&'+opts.data+'&cb=audi_ngw.livesearch.insertResult';
					} else {
						_url += '?'+opts.data+'&cb=audi_ngw.livesearch.insertResult';
					}
					
					// optimize server performance by loading at intervals
					
					doreq = window.setTimeout(function () {
						jQuery.ajaxSetup({cache: true});
						audi_ngw.livesearch.textBox = textBox;
						$.getJSON(_url);
					}, opts.delay);
				}
			}
			// if text is too short do nothing and hide everything
			else {
				$(jH).removeClass(jsH);
				$(jC).hide();
			}
			
			// no bubbling, click is binded to textBox to prevent document bind from firing
			return false;
		});
		
		// why no use $(this).blur ?, because jSuggest box is hidden before click fires so this is the only way to do it
		// alternate way is to say that text blur will fire before$("#jSuggestContainer ul li") click.
		$(document).bind("click", function(){
			$(jC).hide();
			iniVal = textBox.value;
		});

	};
	
	$.fn.jSuggest.defaults = {
		minchar: 3,
		opacity: 1.0,
		zindex: 20000,
		delay: 2500,
		loadingImg: 'ajax-loader.gif',
		loadingText: 'Loading...',
		autoChange: false,
		url: "",
		type: "GET",
		data: ""
	};
		
	
		  

})(jQuery); 
