var siteRestrictionID	= "009212793687109903358:atpxh6pg62k";
var resultElementID 	= "searchResults";
var textBoxID			= "searchQuery";
var first 				= true;

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

var webSearch;

function searchButtonOver() {
	$("#searchSubmit").removeClass('searchButton').addClass('searchButtonHover');
}

function searchButtonOut() {
	$("#searchSubmit").removeClass('searchButtonHover').addClass('searchButton');
}

function searchBlur() {
	if($("#"+textBoxID).val()=='') {
		$("#"+textBoxID).val(search_text)
	}
}

function prepare_page() {
	var result_container_id = "content-main";

	if ($("#"+result_container_id).width() == null) {
		result_container_id = "middleBox";
	}
	$("#"+result_container_id).load(result_page, function() {
		first=false;
		search();
	});
}

function prepareSearch() {
	// The search page need to be prepared only first time
	if(first) { 
		prepare_page();
	} else {
		search();
	}
}

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 = '<div class="pagination"><span class="pagesCaption">Pagine: </span>';
	for (var i = 0; i < cursor.pages.length; i++) {
		var page = cursor.pages[i];
		var elem="";
		if (curPage == i) { 
			// If we are on the curPage, then don't make a link
			elem = '<span class="selectedPage">' + page.label + '</span>';
			
		} else {
			// If we aren't on the current page, then we want a link to this page.
			// So we create a link that calls the gotoPage() method on the searcher.
			elem = '<a href="javascript:webSearch.gotoPage(' + 
				i + ');" class="unSelectedPage">' + page.label + '</a>';
		}
		pagesDiv += elem;
	}
	pagesDiv += "</div>";
	
	$("#"+resultElementID).append(pagesDiv);
}

function searchComplete() {
	// Check that we got results
	$("#"+resultElementID).empty();
	if (webSearch.results && webSearch.results.length > 0) {
		// 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 title = '<div class="title"><a href="' + result.unescapedUrl + '" class="titleURL">' + result.title + '</a></div>';
			var content = '<div class="content">'+result.content+'</div>';
			var url = '<a href="' + result.unescapedUrl + '" class="url">' + result.visibleUrl + '</a>';
			
			var resContainer = '<div class="result">';
			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('<span class="noResult">La ricerca non ha prodotto alcun risultato</span>');
	}
	$("#"+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);
	
	var imageWidth = $("#animation").width();
	var imageHeight = $("#animation").height();
	
	$("#animation").css(
		{
			"margin-top"  : ((windowHeight-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);
	
	webSearch.execute($.trim($("#"+textBoxID).val()));
}

