
$(document).ready(function () {
   
	search_timeout = 0;
	
	$('#search-result-title-shrink').click(function () {
	
		$.ajax({
			type: "POST",
			url: "/lucene/search",
			dataType: "json",
			data: "html="+search_string+"&section="
		});
	
		$("#search-results").hide();
		$("#search-result-title-shrink").hide();		
		//alert($("#search-results").html());
		$("#search").val('');
	});
   
	$('#search').keyup(function () {
		
		search_string = this.value;
		
		if (this.value) {
		
			clearTimeout(search_timeout);
			
		   	search_timeout = setTimeout(function() {
		   		totalHits = 0;
									$('#search-result_news').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=news",
				    		success: function(json) {
				    			$('#search-result_news ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_news').show();
				    			//} else {
				    			//	$('#search-result_news').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_news ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_news ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_news').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_news ul li').size() > 0) {
					//   	$('#search-result_news').show();
					//}
			    						$('#search-result_blogs').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=blogs",
				    		success: function(json) {
				    			$('#search-result_blogs ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_blogs').show();
				    			//} else {
				    			//	$('#search-result_blogs').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_blogs ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_blogs ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_blogs').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_blogs ul li').size() > 0) {
					//   	$('#search-result_blogs').show();
					//}
			    						$('#search-result_forums').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=forums",
				    		success: function(json) {
				    			$('#search-result_forums ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_forums').show();
				    			//} else {
				    			//	$('#search-result_forums').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_forums ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_forums ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_forums').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_forums ul li').size() > 0) {
					//   	$('#search-result_forums').show();
					//}
			    						$('#search-result_events').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=events",
				    		success: function(json) {
				    			$('#search-result_events ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_events').show();
				    			//} else {
				    			//	$('#search-result_events').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_events ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_events ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_events').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_events ul li').size() > 0) {
					//   	$('#search-result_events').show();
					//}
			    						$('#search-result_classifieds').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=classifieds",
				    		success: function(json) {
				    			$('#search-result_classifieds ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_classifieds').show();
				    			//} else {
				    			//	$('#search-result_classifieds').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_classifieds ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_classifieds ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_classifieds').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_classifieds ul li').size() > 0) {
					//   	$('#search-result_classifieds').show();
					//}
			    						$('#search-result_users').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=users",
				    		success: function(json) {
				    			$('#search-result_users ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_users').show();
				    			//} else {
				    			//	$('#search-result_users').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_users ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_users ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_users').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_users ul li').size() > 0) {
					//   	$('#search-result_users').show();
					//}
			    						$('#search-result_groups').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=groups",
				    		success: function(json) {
				    			$('#search-result_groups ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_groups').show();
				    			//} else {
				    			//	$('#search-result_groups').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_groups ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_groups ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_groups').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_groups ul li').size() > 0) {
					//   	$('#search-result_groups').show();
					//}
			    						$('#search-result_images').hide();
					hits = 0;
					$.ajax({
				    		type: "POST",
				    		url: "/lucene/search",
				    		dataType: "json",
				    		data: "search="+search_string+"&section=images",
				    		success: function(json) {
				    			$('#search-result_images ul').empty();

				    			//if (json.hits) {
				    			//	$('#search-result_images').show();
				    			//} else {
				    			//	$('#search-result_images').hide();
				    			//}
				    			
				    			hits = json.hits;
				    			totalHits += hits;
				    			$("#search-result-hits").html(totalHits);
				    			
						   		$.each(json.data,function(j,item) {
						   			if (item.type == 'category') {
						   				header = "<b>"+item.header+"</b>";
						   			} else {
						   				header = item.header;
						   			}
						   			if (item.type == 'image') {
							   			$('#search-result_images ul').append("<li style=\"padding-bottom:6px;\"><a href=\""+item.url+"\"><img src=\"/images/ps/"+item.image+"/210//0\" width=\"210\" border=\"0\" /></a><br /><a href=\""+item.url+"\">"+item.caption+"</a></li>");
						   			} else {
							   			$('#search-result_images ul').append("<li><a href=\""+item.url+"\">"+header+"</a></li>");
							   		}
						   		});
						   		
						   		if (hits > 0) {
						   			$('#search-result_images').show();
							   		$("#search-results").show();
									$("#search-result-title-shrink").show();
						   		}
				    		}
				    });
				    
				    $("#search-result-hits").html(totalHits);
					
			    	//if ($('#search-result_images ul li').size() > 0) {
					//   	$('#search-result_images').show();
					//}
			    				},500);
		} else {
			$("#search-results").hide();
			$("#search-result-title-shrink").hide();
		}
		
		$("#search-result-query").html(search_string);
	});
});
