$(document).ready(function () {
	$("#live_search_query").keyup(function()
	{   
		if(this.value != this.lastValue) {

			// for each checkbox
			var formfields = new Object();

			$("#live_search_query").each(function()
			{
				formfields[this.name] = this.value;
			});
			
					
			$("#live_search_url").each(function()
			{
				formfields[this.name] = this.value;
			});
			if(this.timer) {
				clearTimeout(this.timer);
			}

			this.timer = setTimeout(function() {
				jQuery('#live-search-results > *').hide('fast', function(){
					jQuery('.loading').css( 'display', 'block' );
					$.ajax({
						  type: 'post', 
						  url: 'confex/ajax_livesearch',
						  data: formfields,
						  success: handleLiveSearchResponse
						});
				});
			}, 500);

			this.lastValue = this.value;
		}
	});
	$("#live_search_query_code").keyup(function()
	{   
		var min_length = 0;
		$("#search_string_rule").each(function()
		{
			min_length = this.value;
		});
		if(this.value != this.lastValue) {
			if ( this.value.length > min_length )
			{
				// for each checkbox
				var formfields = new Object();
	
				$("#live_search_query_code").each(function()
				{
					formfields[this.name] = this.value;
				});
				
						
				$("#live_search_url").each(function()
				{
					formfields[this.name] = this.value;
				});
				if(this.timer) {
					clearTimeout(this.timer);
				}
	
				this.timer = setTimeout(function() {
					jQuery('#live-search-results > *').hide('fast', function(){
						jQuery('.loading').css( 'display', 'block' );
						$.ajax({
							  type: 'post', 
							  url: 'confex/ajax_livesearch_code',
							  data: formfields,
							  success: handleLiveSearchResponse
							});
					});
				}, 500);
	
				this.lastValue = this.value;
			}
		}
	});
	
	
	/* handle live search response */
	function handleLiveSearchResponse(data)
	{
		jQuery('.loading').fadeOut( 'slow', function () {
			$('#live-search-results').hide().html(data).show('slow');
		});
	}
	
});

