	
	/*
	 *	jquery.suggest 1.1 - 2007-08-06
	 *	
	 *	Uses code and techniques from following libraries:
	 *	1. http://www.dyve.net/jquery/?autocomplete
	 *	2. http://dev.jquery.com/browser/trunk/plugins/interface/iautocompleter.js	
	 *
	 *	All the new stuff written by Peter Vulgaris (www.vulgarisoip.com)	
	 *	Feel free to do whatever you want with this file
	 *
						e.preventDefault(); 
						e.stopPropagation();
						selectCurrentResult();
	 */
	
	(function($) {

		$.suggest = function(input, options) {
	
			var $input = $(input).attr("autocomplete", "off").click(function(e) {this.select()});;
      //~ var options.defaultValue = $input.value;
		if (options.ParentControl)
			var $ParentControl=true;
		else
			var $ParentControl=false;
			if (options.resdiv)
        var $results = options.resdiv;
       else
       {
        var $results = $(document.createElement("ul"));
        $results.addClass(options.resultsClass).appendTo('body');
        }

			var timeout = false;		// hold timeout ID for suggestion results to appear	
			var prevLength = 0;			// last recorded length of $input.val()
				

			resetPosition();
			$(window)
				.load(resetPosition)		// just in case user is changing size of page while loading
				.resize(resetPosition);

			$input.blur(function() {
				if ($ParentControl) return;
				setTimeout(function() { $results.hide() }, 200);
			});
			
			
			// help IE users if possible
			try {
				$results.bgiframe();
			} catch(e) { }


			// I really hate browser detection, but I don't see any other way
			if ($.browser.mozilla)
				$input.keypress(processKey);	// onkeypress repeats arrow keys in Mozilla/Opera
			else
				$input.keydown(processKey);		// onkeydown repeats arrow keys in IE/Safari
			



			function resetPosition() {
				// requires jquery.dimension plugin
				var offset = $input.offset();
				$results.css({
					top: (offset.top + input.offsetHeight) + 'px',
					left: offset.left + 'px'
				});
			}
			
			
			function processKey(e) {
				
				// handling up/down/escape requires results to be visible
				// handling enter/tab requires that AND a result to be selected
				if ((/27$|38$|40$/.test(e.keyCode) && $results.is(':visible')) ||
					(/^13$|^9$/.test(e.keyCode) && getCurrentResult())) {
		            
		            if (e.preventDefault)
		                e.preventDefault();
					if (e.stopPropagation)
		                e.stopPropagation();

					e.cancelBubble = true;
					e.returnValue = false;
				
					switch(e.keyCode) {
	
						case 38: // up
							prevResult();
							break;
				
						case 40: // down
							nextResult();
							break;
	
						case 9:  // tab
						case 13: // return
							selectCurrentResult();
							break;
							
						case 27: //	escape
							$results.hide();
							break;
	
					}
					
				} else if ($input.val().length != prevLength) {

					if (timeout) 
						clearTimeout(timeout);
					timeout = setTimeout(suggest, options.delay);
					prevLength = $input.val().length;
					
				}			
					
				
			}
			
			
			function suggest() {
			
				var q = $.trim($input.val()).replace(/[ΘΙΚΛθικλ]/g,"e");

				if (q.length >= options.minchars) {
					
						$.get(options.source, {q: q}, function(txt) {

							$results.hide();
              displayItems(txt);						
						});
						
					
				} else {
				
					$results.hide();
					
				}
					
			}
						
			
			function displayItems(html) {
				if (!html) {
					$results.hide();
					return;
				}
				$results.html(html)[0].init_options();
				$results[0].showoptions();							
			}
			
			
			function getCurrentResult() {
			
				if (!$results.is(':visible'))
					return false;
			
				var $currentResult = $results.children('li.' + options.selectClass);
				if (!$currentResult.length)
					$currentResult = false;
        else
          $results[0].scollto($currentResult[0]);
					
				return $currentResult;

			}
			
			function selectCurrentResult() {
			
				$currentResult = getCurrentResult();
			
				if ($currentResult) {
					$input.val($currentResult[0].textContent);
					$results.hide();
					
					if (options.onSelect)
						options.onSelect.apply($currentResult[0]);
						
				}
			
			}
			
			function nextResult() {
			
				$currentResult = getCurrentResult();
            
				if ($currentResult)
          {
          $results.children('li').removeClass(options.selectClass).addClass(options.defaultClass);
					$currentResult.next().addClass(options.selectClass);
          }
				else
					$results.children('li:first-child').removeClass(options.defaultClass).addClass(options.selectClass);
			
			}
			
			function prevResult() {
			
				$currentResult = getCurrentResult();
			
				if ($currentResult)
          {
          $results.children('li').removeClass(options.selectClass).addClass(options.defaultClass);
					$currentResult.prev().addClass(options.selectClass);
          }
				else
					$results.children('li:last-child').removeClass(options.defaultClass).addClass(options.selectClass);
			
			}
	
		}
		
		$.fn.suggest = function(source, options) {
		
			if (!source)
				return;
		
			options = options || {};
			options.source = source;
			options.delay = options.delay || 100;
			options.resultsClass = options.resultsClass || 'ac_results';
			options.selectClass = options.selectClass || 'ac_over';
			options.defaultClass = options.defaultClass || null;
			options.minchars = options.minchars || 2;
			options.delimiter = options.delimiter || '\n';
			options.onSelect = options.onSelect || false;
			options.resdiv = options.resdiv  || null;
	
			this.each(function() {
				new $.suggest(this, options);
			});
	
			return this;
			
		};
		
	})(jQuery);
	

