	$(document).ready(function() {
		timeout = undefined;
		function delayedExecute(closure) {
			delay = 350;
			if (timeout != undefined) {
				clearTimeout(timeout);
			}
			timeout = setTimeout(closure, delay);
		}

		function renderSearchResultsList(data) {
			var categories = [
				{name: 'Online Services', url: 'http://www.riversideca.gov/services.asp'},
				{name: 'Forms', url: 'http://www.riversideca.gov/services.asp'},
				{name: 'Departments', url: 'http://www.riversideca.gov/depts.asp'},
				{name: 'Events and Activities', url: 'http://riversideca.gov'},
				{name: 'Municipal Code', url: 'http://riversideca.gov/municode'},
				{name: 'Neighborhoods', url: 'http://riversideca.gov/neighborhoods'},
				{name: 'Web Pages', url: 'http://riversideca.gov'}];

			var items = data.items;
			var searchResultsHtml = '<div id="searchresults"><table cellspacing="0" cellpadding="0">';
			if (items != null) {
				var lastCategory = null;
				for (var i=0; i < items.length; i++) {
					if (lastCategory != items[i].itemCategory.id) {
						var catId = parseInt(items[i].itemCategory.id) - 1;
						searchResultsHtml = searchResultsHtml + '<tr><td><span class="category">' + categories[catId].name + '</span></td></tr>';
					}
					searchResultsHtml = searchResultsHtml + '<tr><td class="item"><a href="' + items[i].url + '">';
					//searchResultsHtml = searchResultsHtml + '<img alt="item icon" src="/coresearch/search_images/' + items[i].image + '">';
					searchResultsHtml = searchResultsHtml + '<span class="searchheading">' + items[i].name + '</span>';
					searchResultsHtml = searchResultsHtml + '<span>' + items[i].description + '</span>';
					searchResultsHtml = searchResultsHtml + '</a></td></tr>';
					lastCategory = items[i].itemCategory.id;
				}
			}

			searchResultsHtml = searchResultsHtml + '<tr><td><span class="seperator">';
			searchResultsHtml = searchResultsHtml + '<a id="moreResultsSearchLink" href="http://www.riversideca.gov/searchresults.asp?' + data.googleCustomSearchOptions + '&q=' + data.q + '">View more search results.</a>';
			searchResultsHtml = searchResultsHtml + '</span>';
			searchResultsHtml = searchResultsHtml + '<br clear="all" /></td></tr>';
			searchResultsHtml = searchResultsHtml + '</table></div>';

			return searchResultsHtml;
		}

		var ajaxSearch = function() {
			var q = $('#coresearch').val();
			$('#loading').attr('src', 'https://cityjobs.riversideca.gov/hr/images/2011/ajax-loader.gif');
			$.getJSON('http://coresearch.riversideca.gov/coresearch-1.0/item/search?callback=?&q=' + q,
				function(data) {
					$('#suggestions').html(renderSearchResultsList(data));
					$('#suggestions').slideDown();
					$('#loading').attr('src', 'https://cityjobs.riversideca.gov/hr/images/2011/search.png');
				});
		};

		$('#coresearch').keyup(function(e) {
			if ($('#coresearch').val() == '') {
				$('#suggestions').hide();
				delayedExecute(function() { $('#loading').attr('src', 'https://cityjobs.riversideca.gov/hr/images/2011/search.png'); });
			} else {
				delayedExecute(ajaxSearch);
			}
		});

		$('#coresearch').blur(function(e) {
			$('#suggestions').fadeOut();
			$('#suggestions').css("opacity", ""); //Fixes fadeOut bug.
		});

		$('td.item').live('mouseover', function(e) {
			$(this).css("background-color", "#b7b7b7");
			$(this).children("a").css("background-color", "#b7b7b7");
			$(this).css("color", "#fff");
		});

		$('td.item').live('mouseout', function(e) {
			$(this).css("background-color", "#e4e4e4");
			$(this).children("a").css("background-color", "#e4e4e4");
			$(this).css("color", "#eee");
		});

		$('.seperator').live('click', function(e) {
			window.location = $('#moreResultsSearchLink').attr('href');
		});

		$('#coresearch').focus();

	});

