var GoogleApiSearchControl = null;

var App = {
		OnSearchComplete: function() {			
			    $('div.gs-webResult a.gs-title').removeAttr('target');
				
				$('div.gsc-results div.gs-webResult:even').css({'background-color':'#ffffff'});	
			    //var html = $('div.gsc-cursor-box').html();
			    $('div.gsc-cursor-box div.gsc-cursor').before(langPages + ' ');
			    
			    var countPages = $('div.gsc-cursor-box div.gsc-cursor-page').length;
			    
			    if ( countPages > 0 ) {
			    	$('#searchcontrol_header div.noresults').hide();
			    	$('#searchcontrol_header div.resultsfound').show();
			    } else {
			    	$('#searchcontrol_header div.noresults').show();
			    	$('#searchcontrol_header div.resultsfound').hide();
			    }
			    $('div.gsc-cursor-box div.gsc-cursor-page').each(function(i, item) {
			    	var html = $(item).html();
			    	
			    	html = '<div class="number">' + html + '</div>';
			    	
			    	if (countPages > 1 && i < countPages-1 ) {
			    		html += ',';
			    	}
			    	
			    	$(item).html(html);
			    });
			    
		}		
};

function GoogleApi_Load() {
	  // Create a search control
      GoogleApiSearchControl = new google.search.SearchControl();
      GoogleApiSearchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
      GoogleApiSearchControl.setSearchCompleteCallback(App, App.OnSearchComplete);

      
 	  // Options	
	  var options = new google.search.SearcherOptions();
	  options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
	

      // Add in a full set of searchers
      //var localSearch = new google.search.LocalSearch();
      //GoogleApiSearchControl.addSearcher(localSearch, options);
	  var siteSearch = new google.search.WebSearch();
	  
	  //siteSearch.setUserDefinedLabel("");
	  //siteSearch.setUserDefinedClassSuffix("siteSearch");
	  siteSearch.setSiteRestriction("studentenwerk-karlsruhe.de");
	  //siteSearch.setRestriction(google.search.Search.RESTRICT_SAFESEARCH,
      //        google.search.Search.SAFESEARCH_OFF);
	  // Set the Search Control to get the most number of results
	 	  
	  //siteSearch.setLinkTarget(GSearch.LINK_TARGET_SELF);
	 // siteSearch.setResultSetSize(GSearch.LARGE_RESULTSET);

      GoogleApiSearchControl.addSearcher(siteSearch, options);
      //GoogleApiSearchControl.addSearcher(new google.search.VideoSearch(), options);
      //GoogleApiSearchControl.addSearcher(new google.search.BlogSearch(), options);

      // Set the Local Search center point
      //localSearch.setCenterPoint("Karlsruhe, Germany");

      // Tell the searcher to draw itself and tell it where to attach
      GoogleApiSearchControl.draw(document.getElementById("searchcontrol"));

      // Execute an inital search
      GoogleApiSearchControl.execute('test');
    }
    //google.setOnLoadCallback(GoogleApi_Load);

	function GoogleApi_StartSearch(searchText) 
	{
		//alert({/literal}'{$C_LNG_ISO}'{literal});
		//alert('GoogleApi_StartSearch(' + searchText + ')');
		if ( GoogleApiSearchControl == null ) {			
			
			// Show header 'search results'
			var header = document.getElementById('searchcontrol_header');			
			header.style.display = 'block';
			header.style.visibility = 'visible';
			//alert(header.style.display);
			GoogleApi_Load();
		}
			
		GoogleApiSearchControl.execute(searchText);
	}