 var $googleSearchResults;
 var $mainContent;
 var googleSearchControl;
 
google.load('search', '1');
google.setOnLoadCallback(googleSearchAPIReady); 	

 $(function(){
 	$("#search_bar").defaultText();
 	$googleSearchResults = $("#googleSearchResults");
 	$mainContent = $("#mainContent");
 	$("#googleSearchSymbol").click(function(){
 		var searchStr = $("#search_bar").attr("value");
 		if($("#search_bar").get(0).defaultValue == searchStr) return;
 		searchGoogle(searchStr);
 	});
 });
 
function searchSiteKeyUp(searchStr,event){
	if(searchStr == ""){
		$mainContent.show();
		$googleSearchResults.hide();
		return;
	}
	
	if(event.keyCode==13){
		searchGoogle(searchStr);
	}
}

function googleSearchAPIReady() {
	googleSearchControl = new google.search.SearchControl();
	    
	// Add in a WebSearch
	var webSearch = new google.search.WebSearch();
	var forumSearch = new google.search.WebSearch();
	var bhujSearch = new google.search.WebSearch();
	var swaminarayanSatsangSearch = new google.search.WebSearch();
	
	// Restrict our search to pages
	webSearch.setSiteRestriction("www.sydneytemple.org");
	webSearch.setUserDefinedLabel("Sydney Temple");
	
	forumSearch.setSiteRestriction('forums.sydneytemple.org');
	forumSearch.setUserDefinedLabel("Forums");
	
	bhujSearch.setSiteRestriction("shreeswaminarayanmandirbhuj.org");
	bhujSearch.setUserDefinedLabel("Bhuj Mandir");

	swaminarayanSatsangSearch.setSiteRestriction("swaminarayansatsang.com");
	swaminarayanSatsangSearch.setUserDefinedLabel("Swaminarayan Satsang");
	
	// Set the Search Control to get the most number of results
	googleSearchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
	// Add the searcher to the SearchControl
	googleSearchControl.addSearcher(webSearch);
	googleSearchControl.addSearcher(forumSearch);
	googleSearchControl.addSearcher(bhujSearch);
	googleSearchControl.addSearcher(swaminarayanSatsangSearch);
	        
	    
	// Set the options to draw the control in tabbed mode
	var drawOptions = new google.search.DrawOptions();
	drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
	    
	// Draw the control onto the page
	googleSearchControl.draw(document.getElementById("googleSearchResults"), drawOptions);
	    
}

function searchGoogle(searchStr){
	$mainContent.hide();
	$googleSearchResults.show();
	googleSearchControl.execute(searchStr);
}
