// provide globally accessible variables
var $results, searchTimer;

// create our search function
// this will be called when the user types into the search box
function doSearch(searchQuery) {
	// do not display anything or perform a search if there's nothing provided to search on
	if (searchQuery == '') {
		$results.html('');
		return;
	}

	// perform our ajax request
	$.ajax({
		url: 'http://ajax.googleapis.com/ajax/services/search/web',
		data: {
			v: '1.0',
			q: searchQuery,
			rsz: 'large'
		},
		dataType: 'jsonp',
		type: 'GET',
		success: function(data) {
			// explain what was searched on and place in our search-results section
			$results.html('Searched on <strong><a href="' + data.responseData.cursor.moreResultsUrl + '">' + searchQuery + '</a></strong>');
			
			// loop through the results provided and output into our search-results section
			$(data.responseData.results).each(function() {
				var $this = $(this).get(0);
				$results.append('<div class="result"><img src="' + $this.url.substring(0, $this.url.indexOf('/', 9) + 1) + 'favicon.ico" /><a href="' + $this.url +  '" target="_blank">' + $this.titleNoFormatting + '</a><br />' + $this.content + '<br />' + $this.url + '</div>').find('img').error(function() { $(this).remove(); });
			});
		}
	});
}
