/**
 * Auto Complete v2.0
 * June 10, 2009
 * Corey Hart @ http://www.codenothing.com
 *
 * Auto Complete takes input from the user and runs a check through PHP to find what the user
 * is looking for. This test case runs a limited search on words that begin with the letter 'a'.
 *
 * @selclass: Optional class for arrow up/down's, defaults to 'non-404'
 * @ajaxscript: Script name for ajax request, defaults to ajax.php
 */ 

;(function($){
	var ac_counter = 0;
	var mouse_pressed = 0;
	var timeout = null;
        var oneSettingVal="";
	$.fn.autoComplete = function(css){
		this.each(function(){
			// Cache objects
			
			var $obj = $(this), 
				$input = $("input[type='text']", $obj), 
				settings = {
					opt: -1,
					inputval: '',
					css: (css) ? css : 'non-404',
					ajax: $("input[name='href']", $obj).val()
				};

			// Run on keyup
			$input.keydown(function(e){
                                var key = (e.keyCode ? e.keyCode : e.which);
				if (!key) key = 0;
				if ((key >= 48 && key <= 105) || key == 8 || key == 0){
                                        ac_counter++;
					settings.opt = -1;
					settings.inputval = $input.val();
					// call erst nach 200 ms
					if (timeout)
                                         {
                                           clearTimeout(timeout);
                                         }
					timeout = setTimeout(function(){sendRequest(settings.inputval, ac_counter)},100);
				}
                                // Enter 13 or tab 9
                                else if ((key == 13)  || (key == 9))
                                {
                                       if (settings.opt >= 0){
                                                val = settings.inputval; 
                                                parts = val.split("||");
                                                prepareNewLine(val);
                                                settings.opt = -1;
                                                //if (val) $input.val(val);
                                                $('ul', $obj).html('');
                                                $('ul', $obj).hide();
                                        }
                                        else if(oneSettingVal)
                                        {
                                                parms = oneSettingVal;
                                                oneSettingVal = "";
                                                prepareNewLine(parms);
                                                
                                                $('ul', $obj).html('');
                                                $('ul', $obj).hide();
                                        }

                                        return false;
                                }
				else if (key == 37 || key == 39){
					settings.opt = -1;
					$('ul', $obj).html('');
					$('ul', $obj).hide();
				}
				else if (key == 38){
					if (settings.opt >= 0){
						settings.opt--;
						var val = $('ul li', $obj).removeClass(settings.css).eq(settings.opt).addClass(settings.css).attr('rel');
						val = (settings.opt < 0) ? settings.inputval : val;
                                                settings.inputval = val;
						//if (val) $input.val(val);
					}
				}
				else if (key == 40){
					if (settings.opt < $('ul li', $obj).length-1){
						settings.opt++;

                                                if(settings.opt > 8)
                                                {
                                                      $('#ularound').animate({scrollDown: 20}, 500);
                                                }
						var val = $('ul li', $obj).removeClass(settings.css).eq(settings.opt).addClass(settings.css).attr('rel');
                                                settings.inputval = val;
						//if (val) $input.val(val);
					}
				}
			});

	
			// Ajax Request
/*			var sendRequest = function(val){
				$.post(settings.ajax, {value: val, }, function(json){
					// Clear the List
					$('ul', $obj).html('');
					$('ul', $obj).hide();
					// Evaluate the return obj
					json = eval(json);
					// Show the list if there is a return
					if (json && json.length > 0){
						for (i in json){
							$('ul', $obj).append('<li rel="'+json[i].value+'">'+json[i].display+'</li>');
						}
						$('ul', $obj).show();
						// Start mouse actions after list is set
						mouseaction();
					}
				});
			}
*/

			var sendRequest = function(val, my_counter){
				var fields = 
						{
							cartsearch:				$('input[name="cartsearch"]').val(),
                                                        langID:                                 langID
						}
                                hideNoHit();
				if (my_counter == ac_counter && $('input[name="cartsearch"]').val().length > 1) {
					$.post(settings.ajax, fields, function(json){
						// Clear the List
						$('ul', $obj).html('');
						$('ul', $obj).hide();
						// Evaluate the return obj
						json = eval(json);
						// Show the list if there is a return
						if (json && json.length > 0 && my_counter == ac_counter) 
                                                {
                                                        if(json.length == 1)
                                                         {
                                                                 oneSettingVal = json[0].display;
                                                         }
						         for (i in json){
                                                                     parts = json[i].display.split("||");
							  	     $('ul', $obj).append('<li rel="'+json[i].display+'"><div style="float:left;width:50px">'+json[i].value+'</div><div style="width:190px;overflow:hidden;float:left">'+parts[0]+'</div><br clear="all"></li>');
							      }
							      $('ul', $obj).show();
						  	     // Start mouse actions after list is set
							     mouseaction();
						}
                                                else if(my_counter == ac_counter)
                                                {
                                                       showNoHit();
                                                       settings.opt = -1;
                                                       oneSettingVal = "";
                                                }

					});

				}
			}

	
			// Run Mouse Actions
			function mouseaction(){
				// List effects
				$('ul li', $obj).mouseover(function(){
					$('ul li', $obj).removeClass(settings.css);
                                        settings.inputval =  $(this).addClass(settings.css).attr('rel');
				    /*	$input.val( $(this).addClass(settings.css).attr('rel') );*/
				}).click(function(){
					$('ul', $obj).html('');
					$('ul', $obj).hide();
                                         
  				        prepareNewLine( $(this).addClass(settings.css).attr('rel') );
                                        settings.opt = -1;
                                        oneSettingVal = "";
					// IE8 will still see mouse-out event
					mouse_pressed = 1;
				});
	
				// Return orignal val when not hovering
				$('ul', $obj).mouseout(function(){
					// IE 8 needs this, otherwise mouseout event still 
					// triggered
					if (!mouse_pressed) {
					//	$input.val(settings.inputval);
					}
					// this will make the mouse not work
					//$('ul', $obj).hide();
				});
	
				// Clear the list when user clicks outside the form
				$(document).click(function(){
					$('ul', $obj).html('');
					$('ul', $obj).hide();
				});
			}
		});
	};
})(jQuery);

