function displayGCSSResults(response) {
	if(response.RESULTSET.hasOwnProperty("HTML")) {
		$("#regularOutput").html(response.RESULTSET.HTML);
		if(response.RESULTSET.hasOwnProperty("FILTERS")) {
			$("#searchFilters").html(response.RESULTSET.FILTERS);
		}
		$(".pagination").clone().appendTo("#regularOutput");
		$("div.category").each(
			function(index,element) {
				var category = $(this).attr("class");
				if($(this).find(".search-result").length == 0) {
					$("a." + category.replace(/\s/g,".")).parent("div").remove();
					$(this).remove();
				}
			}
		);
		$("div.categories div a.category").each(
			function(index,element) {
				var category = $(this).attr("class");
				if($("div." + category.replace(/\s/g,".")).length == 0) {
					$(this).parent("div").remove();
				}
			}
		);
		if($("div.category.Miscellaneous").length == 1 && $("div.categories div a.category.Miscellaneous").length == 0) {
			$("div.categories").append('<div><br/><a class="category Miscellaneous" href="#">Miscellaneous</a></div>');
		}
		$("div.next a,div.previous a").click(
			function(e) {
				e.preventDefault();
				$.post(
					"/ajax.cfm",
					{component:"google.site_search.rest",method:"getHTML",cse:$(this).attr("href")},
					displayGCSSResults,
					"json"
				);
			}
		);
		$("a.category").click(
			function(e) {
				e.preventDefault();
				if($(this).hasClass("All")) {
					$("div.category").show();
				} else {
					$("div.category").hide();
					$("div." + $(this).attr("class").toString().replace(/\s/g,".")).show();
				}
			}
		);
		$("a.suggestion").click(
			function(e) {
				e.preventDefault();
				$("#regularInput").val($(this).text());
				$.post(
					"/ajax.cfm",
					{component:"google.site_search.rest",method:"getHTML",q:$(this).attr("href"),raw:true},
					displayGCSSResults,
					"json"
				);
			}
		);
	}
}

function localSearch(e) {
	e.preventDefault();
	var site = "";
	if(e.hasOwnProperty("which") && e.which == 13) {
		site = $(".button.local.search:first").attr("href").toString().replace("#","");
	} else {
		site = $(this).attr("href").toString().replace("#","");
	}
	var params = {component:"google.site_search.rest",method:"getHTML",q:$("#regularInput").val(),as_sitesearch:site};
	var exclude = $("#exclude").val();
	if(exclude != null && exclude != '') {
		params.exclude = exclude;
	}
	$("#regularOutput").html('<img src="/lobby/assets/images/loading.gif" style="border:none;float:left;margin-right:5px;"/> Loading...');
	$.post(
		"/ajax.cfm",
		params,
		displayGCSSResults,
		"json"
	);
}

$(document).ready(
	function() {
		$(".button.local.search").click(localSearch);
		
		$(".button.global.search").click(
			function(e) {
				e.preventDefault();
				var params = {component:"google.site_search.rest",method:"getHTML",q: $("#regularInput").val()};
				var exclude = $("#exclude").val();
				if(exclude != null && exclude != '') {
					params.exclude = exclude;
				}
				$("#regularOutput").html('<img src="/lobby/assets/images/loading.gif"> Loading...');
				$.post(
					"/ajax.cfm",
					params,
					displayGCSSResults,
					"json"
				);
			}
		);
		/*
		$("#regularInput").focus(
			function() {
				if($(this).val() == 'I want to find local ...') {
					$(this).val("");
				}
			}
		);
		$("#regularInput").blur(
			function() {
				if($.trim($(this).val()) == '') {
					$(this).val("I want to find local ...");
				}
			}
		);
		$("#regularInput").keypress(
			function(e) {
				if(e.which == 13) {
					localSearch(e);
				}
			}
		);
		if($("#regularInput").val().toString() != 'I want to find local ...') {
			$(".button.local.search").click();
		}
		 
		$("div.next a,div.previous a").click(
			function(e) {
				e.preventDefault();
				$.post(
					"/ajax.cfm",
					{component:"google.site_search.rest",method:"getHTML",cse:$(this).attr("href")},
					displayGCSSResults,
					"json"
				);
			}
		);
		*/
		
		$(".pagination").clone().appendTo("#regularOutput");

		$("div.category").each(
			function(index,element) {
				var category = $(this).attr("class");
				if($(this).find(".search-result").length == 0) {
					$("a." + category.replace(/\s/g,".")).parent("div").remove();
					$(this).remove();
				}
			}
		);
		$("div.categories div a.category").each(
			function(index,element) {
				var category = $(this).attr("class");
				if($("div." + category.replace(/\s/g,".")).length == 0) {
					$(this).parent("div").remove();
				}
			}
		);

		if($("div.category.Miscellaneous").length == 1 && $("div.categories div a.category.Miscellaneous").length == 0) {
			$("div.categories").append('<div><br/><a class="category Miscellaneous" href="#">Miscellaneous</a></div>');
		}

		$("a.category").click(
			function(e) {
				e.preventDefault();
				if($(this).hasClass("All")) {
					$("div.category").show();
				} else {
					$("div.category").hide();
					$("div." + $(this).attr("class").toString().replace(/\s/g,".")).show();
				}
			}
		);
		$("a.suggestion").click(
			function(e) {
				e.preventDefault();
				$("#regularInput").val($(this).text());
				$.post(
					"/ajax.cfm",
					{component:"google.site_search.rest",method:"getHTML",q:$(this).attr("href"),raw:true},
					displayGCSSResults,
					"json"
				);
			}
		);
	}
);
