window.GoogleSearch = {
	searchControl: null,
	webSearch: null,
	initialPage: false,

	initialize: function(initialPage, query){
		this.initialPage = initialPage;
		
		if(!this.initialPage && String.isNotEmpty(query)){
			this.queryToSearch = query;
		}
	},

	gotoPage: function(page){
		this.webSearch.gotoPage(page);
	},

	_completeSearch: function(){
		$("containerGoogleSearch").show();
		if (this.webSearch.results.length == 0){
			$("google.search.results").hide();
			$("googleNoResult").show();
		}else{
			$("googleNoResult").hide();
			$("google.search.results").show();
		}
	},

	_startSearch: function(){
		if(this.initialPage){
			this.searchControl.cancelSearch();
			this.searchControl.clearAllResults();
	
			var strQuery = $A(arguments)[2];
			Processing.show(true);
			var googleSearchForm = $("googleSearchForm");
			googleSearchForm.appendChild(new Element("input", {type:"hidden", name:"searchQuery", value:strQuery}));
			googleSearchForm.submit();
		}else{
			$("content").hide();
			$("google.search.results").show();
		}
	},

	onLoad: function(){
		// Mais em http://code.google.com/apis/ajaxsearch/documentation/reference.html

		// Create a search control
        this.searchControl = new google.search.SearchControl();
        this.webSearch = new google.search.WebSearch();

        this.webSearch.setSiteRestriction("018122259127476339366:guixwzznqec");
        //this.webSearch.setSiteRestriction(Bundle.getSystemProperty("portal.url"));
        this.webSearch.setUserDefinedLabel(Bundle.getMessage("txt.google.result.search"));
        //this.webSearch.setNoHtmlGeneration(); TODO:Não Funcionou....
        this.searchControl.setSearchStartingCallback(this, this._startSearch.bind(this));
        this.searchControl.setSearchCompleteCallback(this, this._completeSearch.bind(this));

        var options = new google.search.SearcherOptions();
        options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
        this.searchControl.addSearcher(this.webSearch, options);

        //this.webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
		//this.searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);

		this.webSearch.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
		this.searchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);

        // Add in a full set of searchers
        //var localSearch = new google.search.LocalSearch();
        //searchControl.addSearcher(localSearch);
        //searchControl.addSearcher(new google.search.VideoSearch());
        //searchControl.addSearcher(new google.search.BlogSearch());
        //searchControl.addSearcher(new google.search.NewsSearch());
        //searchControl.addSearcher(new google.search.ImageSearch());
        //searchControl.addSearcher(new google.search.BookSearch());
        //searchControl.addSearcher(new google.search.PatentSearch());

        // Set the Local Search center point
        //localSearch.setCenterPoint("New York, NY");

		this.searchControl.draw(this.initialPage ? null : $("google.search.results"), this.getDrawOptions());
		
		if(this.queryToSearch!=null){
			this.searchControl.execute(this.queryToSearch);
			this.queryToSearch=null;
		}
		
	},

	getDrawOptions: function(){
		var drawOptions = new GdrawOptions();
        drawOptions.setSearchFormRoot($("google.search.control"));
        //drawOptions.setInput($("search.query"));
        drawOptions.setDrawMode(GSearchControl.DRAW_MODE_LINEAR);
        return drawOptions;
	}
};
