jQuery.fn.liveSearch2 = function (conf) {
	var config = jQuery.extend({
		url:			'/buscar_get?q=', 
		id:				'live-search', 
		duration:		400, 
		typeDelay:		200,
		loadingClass:	'qsearch_load', 
		onSlideUp:		function () {}
	}, conf);
	var liveSearch	= jQuery('#' + config.id);

	if (!liveSearch.length) {
		liveSearch = jQuery('<div id="' + config.id + '"></div>').appendTo(document.body).hide().slideUp(0);

		jQuery(document.body).click(function(event) {
			var clicked = jQuery(event.target);

			if (!(clicked.is('#' + config.id) || clicked.parents('#' + config.id).length || clicked.is('input'))) {
				liveSearch.slideUp(config.duration, function () {
					config.onSlideUp();
				});
			}
		});
	}

	return this.each(function () {
		var input		= jQuery(this).attr('autocomplete', 'off');
		var resultsShit	= parseInt(liveSearch.css('paddingLeft'), 10) + parseInt(liveSearch.css('paddingRight'), 10) + parseInt(liveSearch.css('borderLeftWidth'), 10) + parseInt(liveSearch.css('borderRightWidth'), 10);

		input
			.focus(function () {
				if (this.value !== '') {
					if (liveSearch.html() == '') {
						this.lastValue = '';
						input.keyup();
					}
					else {
						liveSearch.slideDown(config.duration);
					}
				}
			})
			.keyup(function () {
				if (this.value != this.lastValue) {
					input.addClass(config.loadingClass);

					var q = this.value;

					if (this.timer) {
						clearTimeout(this.timer);
					}

					this.timer = setTimeout(function () {
						jQuery.get(config.url + q, function (data) {
							input.removeClass(config.loadingClass);

							if (data.length && q.length) {
								var tmpOffset	= input.offset();
								var inputDim	= {
									left:		tmpOffset.left, 
									top:		tmpOffset.top, 
									width:		input.outerWidth(), 
									height:		input.outerHeight()
								};

								inputDim.topNHeight	= inputDim.top + inputDim.height;
								inputDim.widthNShit	= inputDim.width - resultsShit;

								liveSearch.css({
									position:	'absolute', 
									left:		inputDim.left + 'px', 
									top:		inputDim.topNHeight + 'px'
								});

								liveSearch.html(data).slideDown(config.duration);
								document.getElementById("live-search").scrollTop = 0;
							}
							else {
								liveSearch.slideUp(config.duration, function () {
									config.onSlideUp();
								});
							}
						});
					}, config.typeDelay);

					this.lastValue = this.value;
				}
			});
	});
};
$(document).keydown(function(e){
	if (document.getElementById("live-search")) {
		var el = document.getElementById("live-search");
	}
	// Esc
	if (e.keyCode == 27) {
		if (el.style.display == "block") {
			$("#live-search").slideUp("fast");
			document.getElementById("q").value = "";
		}
	}
	// Enter
	if (e.keyCode == 13) {
		if (el.style.display == "block") {
			window.location = document.getElementById("url_"+currentResult).innerHTML;
			return false;
		}
	}
	// Flecha up
	if (e.keyCode == 38) {
		if (el.style.display == "block") {
			if (currentResult > 1) {
				currentResult--;
				hiResult(currentResult);
				var difResult = maxResult-3;
				if (el.scrollTop > 0 && difResult > currentResult) {
					el.scrollTop -= 76;
				}
			}
		}
	}
	// Flecha down
	if (e.keyCode == 40) {
		if (el.style.display == "block") {
			if (currentResult < maxResult) {
				currentResult++;
				hiResult(currentResult);
				if (currentResult > 4) {
					el.scrollTop += 76;
				}
			}
		}
	}
});
function hiResult(pos) {
	for (n=1;n<=maxResult;n++) {
		if (pos == n) {
			$("#search_"+n).addClass("highlight_res");
		} else {
			$("#search_"+n).removeClass("highlight_res");
		}
	}
}