google.load('search', '1');

$(function() {
	$(".JSHideMe").hide();	// Hide our Javascript warning
	
	var webSearch;	// Our google object
	var currentResultIndex;
	var currentPageIndex;
	var targetDomain;	// Domain we're searching for
	var stopping = false;	// Used for user-initiated search stopping
	
	// Setup the GUI elements
	var uxStopButton = $("#Stop");
	var uxOutputDiv = $("#Results");
	var uxSearchTerm = $("#SearchTerm");
	var uxSearchDomain = $("#SearchDomain");
	
	// Setup the webSearch object.
	webSearch = new google.search.WebSearch();
	webSearch.setNoHtmlGeneration();	// We have no need to generate the HTML for all elements.
	webSearch.setResultSetSize(GSearch.LARGE_RESULTSET);	// Gives us the max result amount per page, 8
	webSearch.setSearchCompleteCallback(this, searchComplete, null);
	
	function searchComplete() {
		if (webSearch.results && webSearch.results.length > 0) {
			currentPageIndex++;
			
			var results = webSearch.results;
			var termFound = false;

			for (var resultIndex = 0; resultIndex < results.length; resultIndex++) {
				// Did the user want to cancel the search?
				if (stopping == true)
					break;

				var result = results[resultIndex];
				
				// Isolate the domain portion of the result
				var domain = domainFromUrl(result.url);

				// Is this a match?
				if (domain.indexOf(targetDomain) > -1) {
					termFound = true;
					webSearch.createResultHtml(result);
					uxOutputDiv.html("Found <strong>" + domain + "</strong> at index " + currentResultIndex + ", which is page " + Math.ceil(currentResultIndex / 10) + ".<hr />" + result.html.innerHTML);
					break;
				} else	// Not found yet, report current status to user.
					uxOutputDiv.text("Checking index " + currentResultIndex + " (" + domain + ")...");
					
				currentResultIndex++;
			}
			
			// The search goes on if there's more pages to search, the term was not found, and the user did not ask top stop the search
			if ((currentPageIndex < webSearch.cursor.pages.length) && (termFound == false) && (stopping == false))
				webSearch.gotoPage(currentPageIndex);
			else {	// The search is over.
				toggleStopButton();
				if (stopping)
					uxOutputDiv.text("Action canceled by user.");
				else if (!termFound)
					uxOutputDiv.text("Sorry! Not found in the first " + currentResultIndex + " results :(");
			}
		}
		stopping = false;
	}
	
	function domainFromUrl(url) {
		var domain = url;
		
		// Remove protocol, such as "http://"
		var protocolSlashIndex = domain.indexOf("//");
		if (protocolSlashIndex > -1)
			domain = domain.substring(protocolSlashIndex+2);

		// Remove directory from the end of the string
		var uriSlashIndex = domain.indexOf("/");
		if (uriSlashIndex > -1)
			domain = domain.substring(0, uriSlashIndex);
			
		return domain;
	}
	
	function stopSearch() {
		uxStopButton.text("Stopping...");
		stopping = true;
	}
	
	function toggleStopButton() {
		uxStopButton.text("Stop Search");
		uxStopButton.toggle(250);
	}
	
	function search(term, domain) {	
		stopping = false;
		toggleStopButton();
		uxOutputDiv.show(250);
		currentResultIndex = 1;
		currentPageIndex = 0;
		targetDomain = domainFromUrl(domain);
		webSearch.execute(term);
	}
	
	$("#loadSample").click(function() {
		uxSearchTerm.attr("value", "apples");
		uxSearchDomain.attr("value", "whfoods.org");
	});

	uxStopButton.click(function() {
		stopSearch();
	});
	
	$("#SearchForm").submit(function() {
		search(uxSearchTerm.attr("value"), uxSearchDomain.attr("value"))
	});
});