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

// Set this variable with search engine ID
// var siteRestrictionID;
// Set with the ID of the div used to display the results
// var resultElementID;
// Set with the ID of the text box with keyword to search
// var textBoxID;

var webSearch;

function addPaginationLinks() {
	// The cursor object has all things to do with pagination
	var cursor = webSearch.cursor;
	var curPage = cursor.currentPageIndex; // check what page the app is on
	var pagesDiv = "<ul class=\"pagination\">";
	for (var i = 0; i < cursor.pages.length; i++) {
		var page = cursor.pages[i];
		pagesDiv += "<li>";
		pagesDiv += "<a href=\"javascript:webSearch.gotoPage(" + i + ");\" ";
		if (curPage == i) { 
			pagesDiv += "class=\"active\"";
		}
		pagesDiv += ">" + page.label + "</a>";
		pagesDiv += "</li>";
	}
	pagesDiv += "</ul>";
	
	$("#"+resultElementID).append(pagesDiv);
}

function searchComplete() {
	// Check that we got results
	$("#"+resultElementID).empty();
	if (webSearch.results && webSearch.results.length > 0) {
		// Grab our content div, clear it.
		// Loop through our results, printing them to the page.
		var results = webSearch.results;
		for (var i = 0; i < results.length; i++) {
			var result = results[i];
			var resContainer = "<div class=\"result\">";
			
			var title = "<h4 class=\"title\">";
			title += "<a href=\"" + result.unescapedUrl + "\">" + result.title + "</a>";
			title += "</h4>";
			
			var content = "<p>";
			content += result.content;
			content += "</p>";
			
			var url = "<a href=\"" + result.unescapedUrl + "\" class=\"result_link\">" + result.visibleUrl + "</a>";
						
			resContainer += title;
			resContainer += content;
			resContainer += url;
			resContainer += "</div>";
			
			$("#"+resultElementID).append(resContainer);
		}
		
		// Now add the paging links so the user can see more results.
		addPaginationLinks(webSearch);
	} else {
		$("#"+resultElementID).append("<div class=\"no_result\>" + no_results_text + "</div>");
	}
	$("#"+resultElementID).append("<div class=\"branding\" id=\"branding\"></div>");
	google.search.Search.getBranding("branding");
	
	$("#loading").fadeOut();
	$("#loading").remove();
	
}

function search() {
	
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	
	var bodyWidth = $("body").width();
	var bodyHeight = $("body").height();
	
	var pageWidth = (windowWidth>bodyWidth)?windowWidth:bodyWidth;
	var pageHeight = (windowHeight>bodyHeight)?windowHeight:bodyHeight;
	
	$("body").append("<div id=\"loading\"><div id=\"animation\"></div></div>");
	
	$("#loading").width(pageWidth);
	$("#loading").height(pageHeight);
	$("#loading").css(
		{
			"opacity" 		: "0.65" ,
			"filter" 		: "alpha(opacity=65)",
			"-moz-opacity" 	: "0.65"
		}
	);
	
	var imageWidth = $("#animation").width();
	var imageHeight = $("#animation").height();
	
	$("#animation").css(
		{
			"margin-top"  : ((pageHeight-imageHeight)/2)+"px", 
			"margin-left" : ((pageWidth-imageWidth)/2)+"px"
		}
	);
	
	
	$("#loading").fadeIn();
	
	webSearch = new google.search.WebSearch();
	// Restrict search using ID
	webSearch.setSiteRestriction(siteRestrictionID);
		
	// Here we set a callback so that anytime a search is executed, it will call
	// the searchComplete function and pass it our WebSearch searcher.
	webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	webSearch.setSearchCompleteCallback(this, searchComplete, null);
	
	var query = $("#"+textBoxID).val();
	
	webSearch.execute($.trim(query));
	
}

