google.load("search", "1", {nocss:true});
//google.load("search", "1");
  			// Call this function when the page has been loaded
		      function cse() {
		       this.searchControl = new google.search.SearchControl();
		        
		        var theResultsDiv = document.getElementById('searchContent');
		        var theSearchForm = document.getElementById('searchForm');
		        this.searchForm = new google.search.SearchForm(true, theSearchForm);
		        // bind clear and submit functions
        		this.searchForm.setOnSubmitCallback(this, cse.prototype.onSubmit);
        		this.searchForm.setOnClearCallback(this, cse.prototype.onClear);

				// We added this on 20090429.  The jquery autocomplete function want to work with a field whose id is 'cse_search'
				// The searchControl class creates a form whose input field has no id.  Let's add it now:
				this.searchForm.input.id = 'cse_search';
				$("#cse_search").autocomplete(searchdata);
	        
		        var searcher = new google.search.WebSearch();
		        searcher.setUserDefinedLabel('CPR.org');
		        searcher.setUserDefinedClassSuffix("siteSearch");
      			//searcher.setSiteRestriction("cpr.org");
		        
		        var options = new google.search.SearcherOptions();
		        // web search, open
				options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
		        searcher.setSiteRestriction("002444405749966402148:secirjsz8v8");
		        this.searchControl.addSearcher(searcher, options);
		        this.searchControl.draw(theResultsDiv);
// TODO: comment this one out if/when we want to use a search of our own db instead of submitting search requests to Google:
//		        this.searchControl.setSearchCompleteCallback(this, cse.prototype.OnSearchComplete);
		        
		      }
		      
		      cse.prototype.onSubmit = function(form) {
		        var q = form.input.value;
		        if (q && q!= "") {
// TODO: temp:
// use a search of our own db instead of submitting search requests to Google:

var page = 0;
$.post("/lib/search/searchdata.php", { q : q, doWhat : 'processSearch', page : page },
	function(data) {
//		alert(data);
		//open the search results tab
		$("#tabs").tabs('select', '#searchResultsTab');
		$('#searchResultsTabHeader').show();
		$('#searchResultNav').removeClass('navOff');
		$('#searchContent').html(data);
	}
);


// TODO: comment this one out if/when we want to use a search of our own db instead of submitting search requests to Google:
//		          this.searchControl.execute(q);

		        }
		        return false;
		      }
			cse.prototype.OnSearchComplete = function()
			{
				//open the search results tab
				$("#tabs").tabs('select', '#searchResultsTab');
				$('#searchResultsTabHeader').show();
				$('#searchResultNav').removeClass('navOff');
				
				$('a.gs-title').each( function() {
					var newtitle = $(this).text().substr(22);
					$(this).text(newtitle);
					//inject a page from CPR into the content tab
					$(this).click(function () {
						
						var urlMinusHash = $(this).attr('href').split("#");
						var urlParts = new Array(); 
					 	urlParts = urlMinusHash[0].split("/");
						if (urlParts[urlParts.length - 2] == "article")
						{
							var custom_url = urlParts[urlParts.length - 1];
							loadContent("load_article", custom_url);
						}
						if (urlParts[urlParts.length - 2] == "category")
						{
							var category_name = urlParts[urlParts.length - 1];
							loadContent("load_category_page", category_name);
						}
						if (urlParts[urlParts.length - 2] == "tag")
						{
							var tag = urlParts[urlParts.length - 1];
							loadContent("load_tag_page", tag);
						}
						$("#contentTabHeader").show();
						
						$("#tabs").tabs("select", "#contentTab"); 
						return false;
					});
				});
			}
		      // when the form fires a clear, call the left and right control
		      cse.prototype.onClear = function(form) {
		        this.searchControl.clearAllResults();
		        
		        form.input.value = "";
		        return false;
		      }
		      function onLoad()
		      {
		      	new cse();
		      }
		      
		      google.setOnLoadCallback(onLoad, true);
  		