
var results_div_id = 'content';
var results_div_class = 'content';
var searching_message = '<span class="ajax-message">Searching...</span>';
var originalContentHeight;
var originalSpacerHeight;
var originalLeftHeight;
var searchStatus = false;


function site_search(domain, query) {
  // must not be empty
  if (!query.match('\\S') || query == "Zoeken...") {
    alert('Geef een zoekopdracht.');
    $('.search form #query').attr('value','').focus();
    return;
  }
  this.query = query;
  if (!searchStatus){
    originalContentHeight = $('.contentContainer').height();
    originalLeftHeight = $('.leftPanel').height();
    originalSpacerHeight = $('.spacer').height();
    searchStatus = true;
  }
  // save the current page
  if (document.getElementById('old' + results_div_id))
    site_search_back(searchStatus);

  old_page = document.getElementById(results_div_id);
  old_page.id = 'old' + results_div_id
  old_page.style.display = 'none';

  // tell the user we're searching
  this.page = document.createElement('div');
  this.page.id = results_div_id;
  this.page.setAttribute('class', results_div_class);
  this.page.innerHTML += searching_message;
  old_page.parentNode.insertBefore(this.page, old_page);

  // set up the search
  this.gwebsearch = new GwebSearch();
  this.gwebsearch.setResultSetSize(GSearch.LARGE_RESULTSET);
  this.gwebsearch.setUserDefinedLabel('');
  this.gwebsearch.setLinkTarget(GSearch.LINK_TARGET_SELF);
  this.gwebsearch.setSiteRestriction(domain);

  this.gwebsearch.clearResults();
  this.gwebsearch.setSearchCompleteCallback(this, site_search.prototype.done);

  // set up the page
  this.page.innerHTML =
    '<h1 style="border: none;">Zoekresultaten</h1><hr /><div class="gsc-back"> <br /><a href="#" onclick="site_search_back(); return false;">' +
    '<span class="backButton left"> </span>&nbsp;&nbsp;&nbsp;&nbsp;Terug naar de webpagina</a> </div>' +
    '<div class="gsc-title"><br />Zoekresultaten voor <b>' + this.query + '</b>:</div>';
  this.branding = GSearch.getBranding();
  this.page.appendChild(this.branding);

  // go!
  this.gwebsearch.execute(query);
}

site_search.prototype.done = function() {
  results = this.gwebsearch.results;
  for (i = 0; i < results.length; i++) {
    // show the full URL, not just the domain. (this is an ugly brittle hack!)
    results[i].html.childNodes[2].innerHTML = results[i].unescapedUrl;
    this.page.insertBefore(results[i].html.cloneNode(true), this.branding);
  }

  cursor = this.gwebsearch.cursor;
  if (!cursor && results.length == 0) {
    this.page.innerHTML += '<p>Geen zoekresultaten.</p>';
  } else if (cursor.currentPageIndex < cursor.pages.length - 1) {
    // this is recursive. GWebSearch will re-call its callback, ie this
    // function, when it gets the next page of result.
    this.gwebsearch.gotoPage(cursor.currentPageIndex + 1);
  } else {
//     this.page.innerHTML += '<a href="' + cursor.moreResultsUrl +
//                            '">More results...</a>';
  }
  this.page.innerHTML += '<div class="spacer"><br /></div>';
  //alert(originalContentHeight + ' ' + $('.content').height());
  adjustPanelHeights();
  //alert(originalContentHeight + ' ' + $('.content').height());
  //adjustPanelHeights(originalContentHeight);
}

function site_search_back(sStatus) {
  results = document.getElementById(results_div_id);

  old_page = document.getElementById('old' + results_div_id);
  old_page.id = results_div_id;
  old_page.style.display = '';

  results.parentNode.removeChild(results);
  //adjustPanelHeights();
  $('.contentContainer').height(originalContentHeight);
  $('.spacer').height(originalSpacerHeight);
  $('.leftPanel').height(originalLeftHeight);
  if (sStatus) searchStatus = true;
  //alert(originalLeftHeight + ', ' + $('.content').height() + ', ' + $('.leftPanel').height() );
}

