function mySearchComplete(searchControl, searcher) {
  var main = $('search_result');
  main.childNodes[0].style.display = 'none';    // table
  var content = main.childNodes[1];             // div A
  if (content.firstChild.innerHTML == "") {
    main.innerHTML = "<div class='doc'><h2>Không tìm thấy kết quả nào</h2><div class='content'></div></div>";
    return;
  }
  var moreContent = content.childNodes[1];      // div C
  var n = moreContent.childNodes.length;
  var controlNode = moreContent.childNodes[n-1];
  var details = [];
  var ids = [];
  var types = [];
  for (var i = 0; i < n; i++) {
    if (i == 0) result = content.childNodes[0];  //div B0
    else result = moreContent.childNodes[i-1];   //div B1,2
    getSearchItemContent(result, ids, details, types);
    result.style.display = 'none';
  }
  if (moreContent.lastChild.firstChild.childNodes.length <= 1) moreContent.lastChild.style.display = 'none';
  var searchResult = document.createElement('div');
  searchResult.id = 'search_display';
  searchResult.innerHTML = "Đang tìm kiếm...";
  moreContent.appendChild(searchResult);
  new Ajax.Updater('search_display', '/' + g_blogname + 'home/showsearch/ids/' + ids.toString() +
    '/keys/' + types.toString(), {asynchronous:true, evalScripts:false, onComplete: function() {
    var n = ids.length;
    for (var i = 0; i < n; i++) {
      if ($('w'+ids[i]) != undefined) $('w'+ids[i]).innerHTML = details[i][0];
      if ($('p'+ids[i]) != undefined) $('p'+ids[i]).innerHTML = details[i][1];
    }
  }});
}

function JSGoogleSearch(searchString, addition) {
  var searchControl = new google.search.SearchControl();

  var options = new google.search.SearcherOptions();
  options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
  options.setRoot(document.getElementById("search_result"));

  var siteSearch = new google.search.WebSearch();
  siteSearch.setQueryAddition(addition);
  searchControl.setSearchCompleteCallback(null, mySearchComplete);

  searchControl.addSearcher(siteSearch, options);
  searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);

  searchControl.draw(document.getElementById("search_control"));
  searchControl.execute(searchString);
}

function getSearchItemContent(item, ids, details, types) {
  item = item.childNodes[0].childNodes[2].childNodes[0].childNodes[0].childNodes[1];
  var url = item.childNodes[0].childNodes[0].href;
  var s = url.match(/(entry|doc|pr)_id[\/=]([0-9]+)/gi) + '';
  if (s != undefined) {
    s = s.split(/[\/=]/, 4);
    types.push(s[0]);
    ids.push(s[1]);
    var site = item.childNodes[2].childNodes[0].innerHTML;
    if (site == 'violet.vn') site = url.match('(violet.vn/[0-9a-zA-Z_-]+)')[1];
    details.push([site, item.childNodes[1].innerHTML]);
  }
}

function PHPGoogleSearch(searchString, addition) {
  var main = $('search_result');
  main.innerHTML = 'Đang tìm kiếm...';
  new Ajax.Request('/' +g_blogname + 'home/googlesearch/q/'+encodeURIComponent(searchString), {
    onComplete: function(response) {main.innerHTML = response.responseText;}
  });
}