// basic keyword search
// inspired by and partially derived from the Web Command Line by Russ Cox,
// available at <http://swtch.com/cmd/>.

var searchkeys = {
// search engines
//    "x":    ['https://eu.ixquick.com/do/metasearch.pl?query=%s',                                 'ixquick'],
    "g":    ['https://www.google.com/search?q=%s',                                               'google'],
    "gmap":   ['http://maps.google.com/maps?q=%s',                                                'google maps'],
    "gt":   ['http://translate.google.com/?hl=en&layout=1&eotf=0&sl=auto&tl=en&q=%s',           'google translate'],
    "gs":   ['http://scholar.google.com/scholar?q=%s',                                          'google scholar'],
    "wa":   ['http://www.wolframalpha.com/input/?i=%s',                                         'wolfram alpha'],
    "doi":   ['http://dx.doi.org/%s', 'doi'],
    "mr":   ['http://ams.math.uni-bielefeld.de/mathscinet/search/publications.html?pg4=ALLF&s4=%s&Submit=Search&dr=all&yrop=eq&arg3=&yearRangeFirst=&yearRangeSecond=&pg8=ET&s8=All&review_format=html', 'mathscinet'],
    "msc":   ['http://www.ams.org/mathscinet/msc/msc2010.html?s=%s&btn=Search', 'maths subject classification'],
// references
    "eom":  ['http://www.google.com/search?q=site:eom.springer.de+%s',  'springer encyclopaedia of mathematics'],
    "bwv":  ['http://www.jsbach.org/bwv%s.html',   'Bach-Werke-Verzeichnis'],
    "wp":   ['http://en.wikipedia.org/wiki/Special:Search?search=%s',   'wikipedia'],
    "wp-fr":['http://fr.wikipedia.org/wiki/Special:Search?search=%s',   'wikipedia (fr)'],
    "md":   ['http://www.merriam-webster.com/medlineplus/%s',		'medlineplus medical dictionary'],
    "bf":   ['http://www.bookfinder.com/search/?keywords=%s&st=sh&ac=qr&submit=',            'bookfinder.com'],
    "z":    ['http://www.amazon.com/s?keywords=%s',            'amazon.com'],
    "zg":   ['http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dus-worldwide-shipping-aps&field-keywords=%s&x=0&y=0',            'amazon global'],
    "z-uk": ['http://www.amazon.co.uk/s?keywords=%s',          'amazon.co.uk'],
    "z-de": ['http://www.amazon.de/s?keywords=%s',             'amazon.de'],
    "z-fr": ['http://www.amazon.fr/s?keywords=%s',             'amazon.fr'],
// languages and dictionaries
    "oed":  ['http://www.oed.com:80/search?searchType=dictionary&isWritersAndEditors=true&searchUri=All&q=%s',     'the oed'],
    "oed-th":  ['http://oed.com/browsethesaurus?thesaurusTerm=%s&searchType=words&type=thesaurussearch',     'the oed: historical thesaurus'],
    "dico-th":  ['http://dico.isc.cnrs.fr/dico/en/search?b=1&r=%s&send=Look+it+up',     'cnrs thesaurus'],
    "bnc":  ['http://bnc.bl.uk/saraWeb.php?qy=%s&mysubmit=Go',    'british national corpus'],
    "enfr":['http://www.wordreference.com/enit/translation.asp?w=%s&dict=enfr&B=Rechercher',   'wordreference (en2fr)'],
    "fren":['http://www.wordreference.com/enit/translation.asp?w=%s&dict=fren&B=Rechercher',   'wordreference (fr2en)'],
    "ende":['http://www.wordreference.com/enit/translation.asp?w=%s&dict=ende&B=Rechercher',   'wordreference (en2de)'],
    "deen":['http://www.wordreference.com/enit/translation.asp?w=%s&dict=deen&B=Rechercher',   'wordreference (de2en)'],
    "conj-fr": ['http://www.wordreference.com/conj/FRverbs.asp?v=%s',                              'wordreference (fr conjugator)'],
// epfl
    "lib":  ['http://opac.nebis.ch/F?local_base=nebis&con_lng=FRE&func=find-b&find_code=WRD&request=%s',    'nebis'],
    "ep-map": ['http://map.epfl.ch/?q=%s',                                                                    'epfl map'],
    "ep-dir": ['http://search.epfl.ch/compoundDirectory.do?q=%s',                                             'epfl dir'],
// switzerland
    "met":  ['http://www.meteosuisse.admin.ch/web/fr/meteo/previsions_en_detail/previsions_locales.html?language=fr&plz=%s&x=0&y=0','météosuisse'],
    "p":    ['http://www.poste.ch/post-startseite/post-privatkunden/post-track-and-trace-search.htm?formattedParcelCodes=%s','la poste suisse'],
// misc
    "pkg":  ['http://pkgsrc.se/search.php?so=%s',                       'pkgsrc.se']
}

function keytable()
{
    var s = '<table class="keywords">\n';

    for(var key in searchkeys)
        s += '    <tr><td><b>' + key + '</b></td><td>' + searchkeys[key][1] + '</td></tr>\n';

    s += '</table>';

    return s;
}

function runquery(input)
{
    var spc = input.indexOf(' ');
    var url = '';

    if(spc != -1) {
        var query   = input.substr(spc+1);
        var keyword = input.substr(0, spc);
        
        if(searchkeys[keyword] != undefined)
            url = searchkeys[keyword][0].replace('%s',escape(query));
        else
            url = '';
    } else {
        url = input;
    }

    window.location = url;

    return false;
}

