
/**
 * The RawSearchControl uses Searcher Objects
 * outside of the standard GSearchControl. This includes calling
 * searcher .execute() methods, reacting to search completions,
 * and if you had previously disabled html generation, how to generate
 * an html representation of the result.
 */
function RawSearchControl() {
  // latch on to key portions of the document
  this.searcherform = document.getElementById("searcher");
  this.cursor = document.getElementById("cursor");
  this.searchform = document.getElementById("searchform");
  this.results = document.getElementById("results");

  // create map of searchers as well as note the active searcher
  // (for now, just one searcher...)
  this.activeSearcher = "web";
  this.searchers = new Array();

  // create and wire up an instance of GwebSearch.
  // Note that we disable html generation. We are doing
  // this so that we can manually create it later.
  // Note also that we register to handle search completion notifications
  // when searches complete; they are called in the context of this instance
  // of RawSearchControl and they are passed the searcher that just completed
  var siteSearch = new google.search.WebSearch();
  siteSearch.setUserDefinedLabel("SCFHP");
  siteSearch.setUserDefinedClassSuffix("siteSearch");
  siteSearch.setSiteRestriction("www.scfhp.com");
  siteSearch.setNoHtmlGeneration();
  siteSearch.setSearchCompleteCallback(this,
                                       RawSearchControl.prototype.searchComplete,
                                       [siteSearch]
                                     );
  this.searchers["web"] = siteSearch;
  this.searchers["web"].setResultSetSize(google.search.Search.LARGE_RESULTSET);

  // now, create a search form and wire up a submit and clear handler
  this.searchForm = new google.search.SearchForm(true, this.searchform);
  this.searchForm.setOnSubmitCallback(this, RawSearchControl.prototype.onSubmit);
  this.searchForm.setOnClearCallback(this, RawSearchControl.prototype.onClear);

//alert("end of rawsearchcontrol");
}

/**
 * figure out which searcher is active by looking at the radio
 * button array
 */
RawSearchControl.prototype.computeActiveSearcher = function() {
  this.activeSearcher = "web";
  return;
  // For multiple searchers, do the following:
  // for (var i=0; i<this.searcherform["searcherType"].length; i++) {
  //   if (this.searcherform["searcherType"][i].checked) {
  //     this.activeSearcher = this.searcherform["searcherType"][i].value;
  //     return;
  //   }
  // }
}

/**
 * onSubmit - called when the search form is "submitted" meaning that
 * someone pressed the search button or hit enter. The form is passed
 * as an argument
 */
RawSearchControl.prototype.onSubmit = function(form) {
  this.computeActiveSearcher();
  if (form.input.value) {
    // if there is an expression in the form, clear page main content
    // area and call the active searchers .execute method
    document.getElementById('main').innerHTML='';
    this.searchers[this.activeSearcher].execute(form.input.value);
  }

  // always indicate that we handled the submit event
  return false;
}

/**
 * onClear - called when someone clicks on the clear button (the little x)
 */
RawSearchControl.prototype.onClear = function(form) {
  this.clearResults();
}

/**
 * searchComplete - called when a search completed. Note the searcher
 * that is completing is passes as an arg because thats what we arranged
 * when we called setSearchCompleteCallback
 */
RawSearchControl.prototype.searchComplete = function(searcher) {

  // always clear old from the page
  this.clearResults();


//alert("before search results");
  // if the searcher has results then process them
  if (searcher.results && searcher.results.length > 0) {

    // toss the searchbox div, since we are presenting results
    removeElement("searchbox", "searcher");

    // print the result titles
    // titles?  titles?  What the heck do we want with titles?  For now, they are tossed; uncomment this section if you want them...
    // var div = createDiv("Result Titles", "header");
    // this.results.appendChild(div);
    // for (var i=0; i<searcher.results.length; i++) {
    //   var result = searcher.results[i];
    //   var titleLine = result.title;
    //
    //   // add in lat,lng for local results
    //   if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS) {
    //     titleLine += " (" + result.lat + ", " + result.lng + ")";
    //   }
    //   if (result.html) {
    //     titleLine += " ** html is present **";
    //   }
    //   div = createDiv(titleLine);
    //   this.results.appendChild(div);
    // }

    // now manually generate the html that we disabled
    // initially and display it
    var div = createDiv("Search results from SCFHP website", "header");

    this.results.appendChild(div);

//alert(div);
    for (var i=0; i<searcher.results.length; i++) {
      var result = searcher.results[i];
      searcher.createResultHtml(result);
      if (result.html) {
        div = result.html.cloneNode(true);
      } else {
        div = createDiv("** failure to create html **");
      }
      this.results.appendChild(div);
    }

//alert("before cursor");
    // now, see if we have a cursor, and if so, create the 
    // cursor control
    if (searcher.cursor) {
      var cursorNode = createDiv(null, "gsc-cursor");
       for (var i=0; i<searcher.cursor.pages.length; i++) {
        var className = "gsc-cursor-page";
        if (i == searcher.cursor.currentPageIndex) {
          className = className + " gsc-cursor-current-page";
        }
        var pageNode = createDiv(searcher.cursor.pages[i].label, className);
		//alert(className);
		//alert(searcher.cursor.pages[i].label);
        pageNode.onclick = methodClosure(this, this.gotoPage, 
                                         [searcher, i]); 
        cursorNode.appendChild(pageNode);
      }
      this.cursor.appendChild(cursorNode);
      var more = createLink(searcher.cursor.moreResultsUrl,
                            GSearch.strings["more-results"] + "&nbsp;&raquo;",
                            GSearch.LINK_TARGET_SELF,
                            "gsc-trailing-more-results");
      this.cursor.appendChild(more);
    }

    // finally, adjust breadcrumb to reflect search results page
    force_breadcrumb("Search Results");
  }
}

RawSearchControl.prototype.gotoPage = function(searcher, page) {
  searcher.gotoPage(page);
}

/**
 * clearResults - clear out any old search results
 */
RawSearchControl.prototype.clearResults = function() {
  removeChildren(this.results);
  removeChildren(this.cursor);
}


