jQuery.fn.liveSearch = function (conf) {
	var config = jQuery.extend({
		url:			'/?module=SearchResults&q=', 
		id:				'jquery-live-search', 
		duration:		400, 
		typeDelay:		250,
		loadingClass:	'loading', 
		onSlideUp:		function () {}
	}, conf);
	var liveSearch	= jQuery('#' + config.id);
	var liveResults	= jQuery('#search_results');

	//display all data when loaded
	jQuery.get(config.url + 'all', function (data) {
							//input.removeClass(config.loadingClass);
							$('#indicator').removeClass(config.loadingClass);
							//if (data.length && q.length) {
								liveResults.html(data);
							//}
	});
	
	
	if (!liveResults.length) {
		//liveResults = 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'))) {
				//liveResults.slideUp(config.duration, function () {
					//config.onSlideUp();
				//});
			//}
		//});
						jQuery.get(config.url + 'all', function (data) {
							//input.removeClass(config.loadingClass);
							$('#indicator').removeClass(config.loadingClass);
							//if (data.length && q.length) {
								liveResults.html(data);
							//}
						});
	}
	
	if (liveResults.length){
		jQuery(document.body).change(function(event) {
			var clicked = jQuery(event.target);
			if (clicked.is("input[name=pa]")){
				//$("input[name=pa]:checked").val('');
				//alert("clicked");
				$('#indicator').addClass(config.loadingClass);
					var q = this.lastValue;

					if (this.timer) {
						clearTimeout(this.timer);
					}

					this.timer = setTimeout(function () {
						jQuery.get(config.url + $('#q').val() + "&p=" + $('input[name=pa]:checked').val(), function (data) {
							//input.removeClass(config.loadingClass);
							$('#indicator').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',
								//	width:		inputDim.widthNShit + 'px'
								//});

								liveResults.html(data).slideDown(config.duration);
							//}
							//else {
								//liveResults.slideUp(config.duration, function () {
									config.onSlideUp();
								//});
							//}
						});
					}, config.typeDelay);

					//this.lastValue = this.value;
			}
		});
	}
	
	return this.each(function () {
		var input		= jQuery(this).attr('autocomplete', 'off');
		//var resultsShit	= parseInt(liveSearch.css('paddingLeft'), 10) + parseInt(liveResults.css('paddingRight'), 10) + parseInt(liveResults.css('borderLeftWidth'), 10) + parseInt(liveResults.css('borderRightWidth'), 10);

		input
			.focus(function () {
				if (this.value !== '') {
					if (liveResults.html() == '') {
						this.lastValue = '';
						//input.keyup();
						liveResults.slideDown(config.duration);
					}
					else {
						liveResults.slideDown(config.duration);
					}
				}
			})
			.keyup(function () {
				if (this.value != this.lastValue) {
					//input.addClass(config.loadingClass);
					$('#indicator').addClass(config.loadingClass);
					var q = this.value;

					if (this.timer) {
						clearTimeout(this.timer);
					}

					this.timer = setTimeout(function () {
						jQuery.get(config.url + q + "&p=" + $('input[name=pa]:checked').val(), function (data) {
							//input.removeClass(config.loadingClass);
							$('#indicator').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',
								//	width:		inputDim.widthNShit + 'px'
								//});

								liveResults.html(data).slideDown(config.duration);
							}
							else {
								//liveResults.slideUp(config.duration, function () {
									//config.onSlideUp();
								//});
								jQuery.get(config.url + "all", function (data) {
								//input.removeClass(config.loadingClass);
								$('#indicator').removeClass(config.loadingClass);
								//if (data.length && q.length) {
								liveResults.html(data);
								//}
								});
							}
						});
					}, config.typeDelay);

					this.lastValue = this.value;
				}
			});
	});
	
	
};
