jQuery.fn.ajaxdrop = function (options) {
	
	var opt = jQuery.extend({
		URL: '', 
		GET: true,
		defaultListClass: 'default-list',
		loadListClass: 'load-list',
		getUrl: ''
	}, options);	
	
	return this.each(function () {
		init(this);
	});

	function init (drop) {
		/*if (opt.URL == '') {
			alert('Please to state option "url" for #'+$(drop).attr('id'));
			return false;
		}*/
		
		var $input = $(drop).find('input:text').eq(0);
		var $content = $(drop).find('div').eq(0);
		var $defaultList = $content.find('ul.'+opt.defaultListClass);
		var $items = $content.find('li');
		var text = '';
		var oldText = ' ';
		var blurTime = keyTime = null;
		var ajaxFlag = null;
		var classOdd = '';
		
		var defaultItemsArray = new Array ();
		
		$defaultList.find('li').each(function (i) {
			defaultItemsArray[i] = $(this).text();
		});
		
		$input.attr('autocomplete', 'off');
		
		$input.bind('focus', function () {
			$('div.select-content').hide();
			//if($items.length) {
				text = jQuery.trim($input.val().toLowerCase());
				if (text == '') $content.find('ul.'+opt.loadListClass).eq(0).remove();
				compareInDrop();
				var ind = null;
				$(this).bind('keydown mousedown', function(e) {
					if (e.which == 40) {
						ind = $items.index($content.find('li.active').eq(0))+1;
						if (ind > $items.length-1) ind = 0;
						$items.removeClass('active');
						while ($items.eq(ind).is(':hidden') && ind < $items.length) {
							ind++;
							if (ind == $items.length) ind = 0;
						}
						$items.eq(ind).addClass('active');
					} else if (e.which == 38) {
						ind = $items.index($content.find('li.active').eq(0))-1;
						if (ind < 0) ind = $items.length-1; 
						$items.removeClass('active');
						while ($items.eq(ind).is(':hidden') && ind >= 0) {
							ind--;
							if (ind < 0) ind = $items.length-1;
						}
						$items.eq(ind).addClass('active');
					} else if (e.which == 13) {
						/*$items.each(function () {
							if ($(this).hasClass('active') && $(this).is(':visible')) {
								alert(1);
								$input.val($(this).text());
								if ($(this).attr("rel"))
									$input.attr("defgroups",$(this).attr("rel"));
								$content.hide();
								return false;
							}
						});*/
						//alert(ind);
						if(ind) {
							var $activeEl = $items.eq(ind);
							$input.val($activeEl.text());
							if ($activeEl.attr("rel"))
								$input.attr("defgroups", $activeEl.attr("rel"));
							if (typeof opt.outDrop != 'undefined') opt.outDrop($input.val());
							if (typeof opt.outDrop1 != 'undefined') opt.outDrop1($input.val(),$input.attr("defgroups"));
						}
						
						if ($content.is(':visible') && ind) {
							$content.hide();
							$items.removeClass('active');
							return false;
						} else {
							return true;	
						}
						
						//alert(2);
						/*if ($items.hasClass('active')) {
							$items.removeClass('active');
							$content.hide();
							return false;
						} else {
							$content.hide();
							return true;
						}*/
					}  else if (e.which == 9) { 
						hideDrop();
					} else {
						$items.removeClass('active');
						
						if (keyTime) clearTimeout(keyTime);
						keyTime = setTimeout(function () {
							text = jQuery.trim($input.val().toLowerCase());
							if (text.length > 0) {
								compareInDrop();
								if (text.indexOf(oldText) == -1) {
									oldText = text;
									if (opt.GET) {
										ajaxLoadGET();
									} else {
										ajaxLoadPOST();
									}
								} else {
									$content.find('ul.'+opt.loadListClass).show();	
								}
							} else {
								$defaultList.find('li').removeClass('even').removeClass('odd').show();
								$defaultList.find('li:nth-child(odd)').addClass('odd');
								$content.find('ul.'+opt.loadListClass).hide();
								$content.show();
							}
						}, 800);
					}
				});
				listUpdate();
			//}
		});
		
		/*$input.bind('blur', function () {
			if (blurTime) clearTimeout(blurTime);
			blurTime = setTimeout(function () {
				$input.unbind('keydown mousedown');
				$items.removeClass('active');
				return true;
			}, 200);
		});*/
		
		$(document).click(function (e) {
			var ev = $(e.target);
			if (!ev.parents('div.dropDown').length) {
				hideDrop();
			}
		});
		
		function hideDrop () {
			if (blurTime) clearTimeout(blurTime);
			blurTime = setTimeout(function () {
				$input.unbind('keydown mousedown');
				$content.hide();
				$items.removeClass('active');
				if (typeof opt.outDrop != 'undefined') opt.outDrop($input.val());
				if (typeof opt.outDrop1 != 'undefined') opt.outDrop1($input.val(),$input.attr("defgroups"));
				return true;
			}, 200);
		}
		
		function compareInDrop () {
			var fl = false;
			classOdd = 'even';
			$items.each(function () {
				$(this).removeClass('even').removeClass('odd');
				if ($(this).text().toLowerCase().indexOf(text.toLowerCase()) == -1) {
					$(this).css('display', 'none');
				} else {
					$(this).css('display', 'block');
					classOdd = (classOdd == 'even')? 'odd':'even';
					$(this).addClass(classOdd);
					fl = true;
				}
			});
			if (fl) {
				$content.show();
			} else {
				$content.hide();
			}
		}
		
		function listUpdate () {
			$items = $content.find('li');
			$items.hover(function () {$items.removeClass('active'); $(this).addClass('active');}, function () {$(this).removeClass('active')});
			$items.click(function () {
				$input.val($(this).text());
				$input.css('color','#000000');
				if ($(this).attr("rel")){
					$input.attr("defgroups",$(this).attr("rel"));
				}
				hideDrop();
			});
		}
		
		function ajaxLoadGET () {
			if (opt.getUrl != '') {
				opt.URL = window[opt.getUrl];
			}
			if (ajaxFlag) ajaxFlag.abort();
			ajaxFlag = $.ajax({
				type: "GET",
				url: opt.URL+text,
				cache: true,
				timeout: 30000,
				success: function(text){
					createAjaxList(text);
				},
				error: function(){
					//ajaxLoadGET ();
				}
			});
		}
		
		function ajaxLoadPOST () {
			if (ajaxFlag) ajaxFlag.abort();
			ajaxFlag = $.ajax({
				type: "POST",
				data: "text="+text,
				url: opt.URL,
				cache: false,
				timeout: 30000,
				success: function(text){
					createAjaxList(text);
				},
				error: function(){
					ajaxLoadPOST ();
				}
			});
		}
		
		function createAjaxList (arr) {
			var listItems = new Array();
			var list = '';
			listItems = arr.split("\n");
			$content.find('ul.'+opt.loadListClass).eq(0).remove();
			list += '<ul class="'+opt.loadListClass+'">';
			for (var i=0; i<listItems.length; i++) {
				var itemParameters = listItems[i].split('|');
				if ($.inArray(itemParameters[0], defaultItemsArray) != -1) continue;
				if (itemParameters[1])
					list += '<li rel="' + itemParameters[1] +'">'+ itemParameters[0] +'</li>';
				else
					list += '<li>'+ itemParameters[0] +'</li>';
			}
			list += '</ul>';
			$content.append(list);
			listUpdate();
			compareInDrop();
		}
	}
};


