//

// Choice of multiple search engines
// Autor: Jakob Voss, Guillaume
// Version: 0.3.8
// Published as Public Domain

// Map of search engines and some OpenSearch-based fields
var searchEngines = {
  mediawiki: {
    ShortName: "Interne Suche",
    Template: "/w/index.php?search={searchTerms}"
  },
  globalwpsearch: {
    ShortName: "Global WP",
    Template: "http://vs.aka-online.de/cgi-bin/globalwpsearch.pl?timeout=120&search={searchTerms}"
  },
  google: {
    ShortName: "Google",
    Template: "http://www.google.com/search?as_sitesearch=de.wikipedia.org&hl={language}&q={searchTerms}"
  },
  wikiwix: {
    ShortName: "Wikiwix",
    Template: "http://de.wikiwix.com/index.php?action={searchTerms}&lang={language}"
  },
  webde: {
    ShortName: "Web.de",
    Template: "http://wiki.suche.web.de/wiki/?su={searchTerms}&wmc=suche@home.suche@wiki"
  },
  yahoo: {
    ShortName: "Yahoo!",
    Template: "http://de.search.yahoo.com/search?p={searchTerms}&vs=de.wikipedia.org"
  },
  wlive: {
    ShortName: "Windows Live",
    Template: "http://search.live.com/results.aspx?q={searchTerms}&q1=site:http://de.wikipedia.org"
  }
};

function changeSearchEngine(selectedId, searchTerms) {
  var currentId = document.getElementById("searchengineChoices").currentChoice;
  if (selectedId == currentId) return;
 
  // select
  document.getElementById("searchengineChoices").currentChoice = selectedId;
  var radio = document.getElementById('searchengineRadio-'  + selectedId);
  radio.checked = "checked";

  var engine = searchEngines[selectedId];
  var p = engine.Template.indexOf('?');
  var params = engine.Template.substr(p+1);

  var form;
  if (document.forms["search"]) {
    form = document.forms["search"];
  } else {
    form = document.getElementById("powersearch");
  }
  form.setAttribute("action", engine.Template.substr(0,p));

  var l = ("" + params).split("&");
  for (var i in l) {
    var p = l[i].split("=");
    var pValue = p[1];

    if (pValue == "{language}") {
    } else if (pValue == "{searchTerms}") {
      var input;
      if (document.forms["search"]) {
        input = document.getElementById("searchText");
      } else {
        input = document.getElementById("powerSearchText");
      } 

      input.name = p[0];
    } else {
      var input = document.getElementById("searchengineextraparam");

      input.name = p[0];
      input.value = pValue;
    }
  }
}

function externalSearchEngines() {
  if (typeof SpecialSearchEnhanced2Disabled != 'undefined') return;
  if (wgPageName != "Spezial:Suche") return;

  var mainNode;
  if (document.forms["search"]) { 
    mainNode = document.forms["search"];
  } else {
    mainNode = document.getElementById("powersearch");
    if (!mainNode) return;
    var mainNode = mainNode.lastChild;
    if (!mainNode) return;

    while(mainNode.nodeType == 3) {
      mainNode = mainNode.previousSibling;
    }
  }

  // selected search engine
  var firstEngine = "mediawiki";
 
  var choices = document.createElement("div");
  choices.setAttribute("id","searchengineChoices");
  choices.style.textAlign = "center";

  // get current search value and remove existing search form
  var lsearchbox = document.getElementById("powerSearchText");
  var initValue = lsearchbox.value;
 
  // create search forms and choice
  var space = "";
  for (var id in searchEngines) {
    var engine = searchEngines[id];
 
    if (space) choices.appendChild(space);
    space = document.createTextNode(" ");
 
    var attr = { 
      type: "radio", 
      name: "searchengineselect",
      value: id,
      onClick: "changeSearchEngine(this.value)",
      id: "searchengineRadio-"+id
    };
 
    var html = "