﻿function initZoekEvents()
{
  Spif.DOMEvents.attach(document.getElementById("searchButton"), "click", addSearchLogic);
  Spif.DOMEvents.attach(document.getElementById("zoekenMoreResults"), "click", zoekenMoreResults);
  Spif.DOMEvents.attach(document.getElementById("zoekenCloseResults"), "click", zoekenCloseResults);
  Spif.DOMEvents.attach(document.getElementById("seachInput"), "keydown", formSubmitCheck);
}


function addSearchLogic()
{
  var el = document.getElementById("seachInput");
  var elPlate = document.getElementById("searchPlate");
  var searchString = el.value + " site:kindertelefoon.nl";
  
  if (Spif.ClassNameAbstraction.contains(elPlate, "zoekenOverlayBox-collapsed"))
    Spif.ClassNameAbstraction.remove(elPlate, "zoekenOverlayBox-collapsed");
  
  if (el)
    GoogleSearchInit(searchString);
  return false;
}

function formSubmitCheck(evt)
{
  if (evt && evt.keyCode == "13")
  {
    addSearchLogic();
    return false;
  }
  
}

function GoogleSearchInit(searchString) 
{
  // Create a search control
  var searchControl = new GSearchControl();
  
  // create a searcher options object
  // set up for open expansion mode
  // load a searcher with these options
  var options = new GsearcherOptions();
  options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);

  // Add in a full set of searchers
  searchControl.addSearcher(new GwebSearch(), options);
  searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);  
  
  //set OnSearchComplete
  searchControl.setSearchCompleteCallback(this, GoogleSearchFinished);

  // tell the searcher to draw itself and tell it where to attach
  searchControl.draw(document.getElementById("searchcontrol"));

  // execute an inital search
  searchControl.execute(searchString);
}

function GoogleSearchFinished()
{
  var searchResultNumber = 0;
  var els = getElementsByTagNameAttributeValue(document.getElementById("searchcontrol"), "div", "class", "gsc-stats");
  
  var el = els[0];
  if (!el)
    return;

  if (el.innerHTML.substr(1,1) == "0")
  {
    if (!Spif.ClassNameAbstraction.contains(document.getElementById("searchcontrol"), "searchcontrol-off"))
      Spif.ClassNameAbstraction.add(document.getElementById("searchcontrol"), "searchcontrol-off")
    else (Spif.ClassNameAbstraction.contains(document.getElementById("zoekenNoResult"), "zoekenNoResult-off"))
      Spif.ClassNameAbstraction.remove(document.getElementById("zoekenNoResult"), "zoekenNoResult-off")
  }
  else
  {
    if (Spif.ClassNameAbstraction.contains(document.getElementById("searchcontrol"), "searchcontrol-off"))
      Spif.ClassNameAbstraction.remove(document.getElementById("searchcontrol"), "searchcontrol-off")
    else (!Spif.ClassNameAbstraction.contains(document.getElementById("zoekenNoResult"), "zoekenNoResult-off"))
      Spif.ClassNameAbstraction.add(document.getElementById("zoekenNoResult"), "zoekenNoResult-off")
  }
  
  setLinkTargets();
}

function setLinkTargets()
{
  //debugger;
  var els = getElementsByTagNameAttributeValue(document.getElementById("searchcontrol"), "div", "class", "gs-title");
  for (var i = 0; i < els.length; i++)
  {
    el = els[i].firstChild;
    if (el.tagName.toLowerCase() == "a")
      el.target = "_self";
  }
}

function zoekenCloseResults()
{
  var el = document.getElementById("searchcontrol");
  var elPlate = document.getElementById("searchPlate");
  
  el.innerHTML = "";
  if (!Spif.ClassNameAbstraction.contains(elPlate, "zoekenOverlayBox-collapsed"))
    Spif.ClassNameAbstraction.add(elPlate, "zoekenOverlayBox-collapsed");
}

function zoekenMoreResults()
{
  var el = document.getElementById("seachInput");
  var searchString = "http://www.google.nl/search?q=" + el.value + " site:kindertelefoon.nl";
  window.location = searchString;
}



