// Taken from http://tutorialzine.com/2010/09/google-powered-site-search-ajax-jquery/

$(document).ready(function(){
	var config = {
		siteURL		: 'mayfaircentre.org.uk/',
		searchSite	: true,
		type		: 'web',
		append		: false,
		perPage		: 5, // A maximum of 8 is allowed by Google
		page		: 0	// The start page
	}

	// Focusing the input text box:
	$('#query').focus();

	// Do the Google search on submit
	$('#searchForm').submit(function(){
		googleSearch();
		return false;
	});
	
	function googleSearch(settings){
		// If no parameters are supplied to the function,
		// it takes its defaults from the config object above:
		
		settings = $.extend({},config,settings);
		settings.term = settings.term || $('#query').val();
		
		if(settings.searchSite){
			// Using the Google site:example.com to limit the search to a
			// specific domain:
			settings.term = 'site:' + settings.siteURL + ' ' + settings.term;
		}
		
		// URL of Google's AJAX search API
		var apiURL = 'http://ajax.googleapis.com/ajax/services/search/' + settings.type + '?v=1.0&callback=?';
		var resultsDiv = $('#middle');
		
		$.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){
			
			var results = r.responseData.results;
			
			if(results.length)
			{
				// If results were returned, add them to a pageContainer div,
				// after which append them to the #resultsDiv:
				var pageContainer = $('<div>',{className:'pageContainer'});
				
				for(var i=0;i<results.length;i++){
					// Creating a new result object and firing its toString method:
					pageContainer.append(new result(results[i]) + '');
				}
				
				if(!settings.append){
					// This is executed when running a new search, 
					// instead of clicking on the More button:
					resultsDiv.empty();
				}
				
				pageContainer.append('<div class="clear"></div>')
							 .hide().appendTo(resultsDiv)
							 .fadeIn('slow');
				
				var cursor = r.responseData.cursor;
				
				// Checking if there are more pages with results, 
				// and deciding whether to show the More button:
				
				if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
					$('<a>').attr("href", "#123").text('Find More...').appendTo(resultsDiv).click(function(){
						event.preventDefault(); // Prevents scrolling to top of page
						googleSearch({append:true,page:settings.page+1});
						$(this).fadeOut();
					});
				}
			}
			else 
			{
				// No results were found for this search.
				resultsDiv.empty();
				$('<h2>',{className:'notFound',html:'No Results Were Found!'}).hide().appendTo(resultsDiv).fadeIn();
			}
		});
	}
	
	// This is class definition. Object of this class are created for
	// each result. The markup is generated by the .toString() method.
	function result(r){
		var arr = [];
		arr = [
			'<div class="webResult">',
			'<h3><a href="',r.unescapedUrl,'" target="_self">',r.title,'</a></h3>',
			'<p>',r.content,'</p>',
			'</div>'
		];

		// The toString method.
		this.toString = function(){
			return arr.join('');
		}
	}
});

