// Keyword based javascript multi-search box,
// Copyright (C) 2007-2009 Jared Updike
// This is released under the BSD license,
// so you can use it how you like but it's not public domain.

function startup()
{
   document.f.q.focus();
}

/*
"up",     "http://www.google.com/search?hl=en&q=site%3Aupdike.org+%s",

"mu",     "http://www.google.com/search?q=site:music.vt.edu+musicdictionary+%s&btnI=I'm+Feeling+Lucky",

*/

// replace every occurence of a (in s) with b
function sreplace(s, a, b)
{
  var result = "";
  var at = s.indexOf(a);

  if (at != -1)
  {
     result = s.substring(0, at) +b+ sreplace(s.substring(at+a.length, s.length), a, b);
  }
  else
     return s;

  return result;
}

function help()
{
  var s = "";
  for (var i = 0; i < keywords.length; i+= 2)
  {
    var k = keywords[i];
    var v = keywords[i+1];
    v = sreplace(v, "http://www.", "");
    v = sreplace(v, "http://", "");
    v = sreplace(v, "%s", "QUERY");
    s += k + "\t" + v + "\n";
  }
  alert(s);
}

// old "http://unixhelp.ed.ac.uk/CGI/man-cgi?%s",

// KEYWORD SEARCHES and SHORTCUTS
var keywords = new Array(
//BEGIN
"gb",     "http://www.google.com/bookmarks/find?hl=en&q=%s%20is%3Astarred&g=Time&btnSMH=Search+Bookmarks",
"man",    "http://www.updike.org/man.py?%s",
"info",   "http://whoopis.com/howtos/man.php?query=%s&type=2&section=0",
"sch",    "http://scholar.google.com/scholar?q=%s&ie=UTF-8&oe=UTF-8&hl=en&btnG=Search",
"cit",    "http://www.google.com/search?button.x=0&button.y=0&q=site%3Acaltech.edu+%s",
"ggl",    "http://www.google.com/search?hl=en&lr=&safe=active&q=%s",
"gov",    "http://www.google.com/unclesam?hl=en&lr=&safe=active&q=%s&btnG=Google+Search",
"go",     "http://www.google.com/search?hl=en&lr=&safe=active&q=%s&btnI=I'm+Feeling+Lucky",
"math",   "http://www.google.com/search?q=site:mathworld.wolfram.com+%s&btnI=I'm+Feeling+Lucky",
"help",   "javascript:help()",
"amg",    "http://www.google.com/search?q=site:allmusic.com+%s&btnI=I'm+Feeling+Lucky",
"citgo",  "http://www.google.com/search?q=site:caltech.edu+%s&btnI=I'm+Feeling+Lucky",
"wik",    "http://www.google.com/search?q=site:en.wikipedia.org+%s&btnI=I'm+Feeling+Lucky",
"gmail",  "https://mail.google.com/mail/?ui=1",
"se",     "http://wordreference.com/es/en/translation.asp?spen=%s",
"es",     "http://wordreference.com/es/translation.asp?tranword=%s",
"hsr",    "http://www.homestarrunner.com/",
"h",      "http://haskell.org/hoogle/?q=%s",
"rd",     "http://roughlydrafted.com/",
"rt",     "http://www.rottentomatoes.com/search/full_search.php?search=%s",
"c",      "http://www.updike.org/calc.cgi?action=start&q=%s",
"has",    "http://www.google.com/search?hl=en&lr=&safe=active&q=site:haskell.org+%s",
"acm",    "http://www.google.com/search?q=site:portal.acm.org+%s&btnI=I'm+Feeling+Lucky",
"imdb",   "http://www.google.com/search?q=site:imdb.com+%s&btnI=I'm+Feeling+Lucky",
"az",     "http://www.google.com/search?q=site:amazon.com+%s&btnI=I'm+Feeling+Lucky",
"ge",     "http://dict.tu-chemnitz.de/dings.cgi?lang=en&noframes=1&query=%s",
"eg",     "http://dict.tu-chemnitz.de/dings.cgi?lang=en&noframes=1&query=%s",
"mu",     "http://www.google.com/musicsearch?q=%s&btnG=Search+Music",
"msdn",   "http://www.google.com/search?q=site:msdn2.microsoft.com+%s",
"yt",     "http://www.youtube.com/results?search_query=%s&search=",
"gr",     "https://www.google.com/reader/",
"gm",     "http://maps.google.com/?q=%s",
"gc",     "http://www.google.com/codesearch?q=%s&amp;hl=en&amp;btnG=Search+Code",
"py",     "http://www.google.com/search?q=site:pydoc.org+%s",
"mc",     "http://www.metacritic.com/search/process?sort=relevance&termType=all&ts=%s&ty=2&x=0&y=0",
"wa",     "http://www.wolframalpha.com/input/?i=%s",
"def",    "http://en.wiktionary.org/wiki/Special:Search?search=%s&go=Define"
//END
);


// "def",    "http://dictionary.reference.com/search?q=%s",

// replace every occurence of character a (in s) with b
function myreplace(s, a, b)
{
  var result = "";
  for (var k = 0; k < s.length; k++)
  {
    if (s.charAt(k) == a)
      result = result + b;
    else
      result = result + s.charAt(k);
  }
  return result;
}

// what happens when user clicks Search or presses return?
function submit()
{
  var q = document.f.q.value;
  var loc = "http://www.google.com/";

  if (q != ""){
    var copyq = escape(q);
    copyq = myreplace(copyq, "+", "%2b"); // escape neglects to escape plus signs for some reason
    loc = "http://www.google.com/search?hl=en&lr=&safe=active&q=" + copyq;

    var qq = q.split(" ");
    var first = qq[0];

    for (var k = 0; k < keywords.length; k += 2)
    {
       var keyword = keywords[k];
       var link = keywords[k+1];

       if (first == keyword)
       {
          // put the query back together, minus the first word
          loc = "";
          for (var i = 1; i < qq.length; i++)
          {
             if (i == 1)
                loc = loc + qq[i];
             else
                loc = loc + " " + qq[i];
          }
          // replace "%s" in location with the query string
          loc = sreplace(link, "%s", loc);
          break;
       }
    }
  }
  // encode # because google doesn't like it with a pound sign getting through
  loc = sreplace(loc, "#", "%23");

  window.location.href = loc;
}

/*
function qs(el)
{
  if (window.RegExp && window.encodeURIComponent)
  {
    var qe=encodeURIComponent(document.f.q.value);
    alert(document.f.action);
    document.f.action = "jupdike";
    write(qe);
    if (el.href.indexOf("q=")!=-1) {
      el.href=el.href.replace(new RegExp("q=[^&$]*"),"q="+qe);
    }
    else {
      el.href+="&q="+qe;
    }
  }
  return 1;
}
*/
