1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Activate the search box:
6(function() {
7  var form = document.getElementById('chrome-docs-cse-search-form');
8  var searchInput = document.getElementById('chrome-docs-cse-input');
9
10  var cx = '010997258251033819707:7owyldxmpkc';
11
12  var gcse = document.createElement('script');
13  gcse.type = 'text/javascript';
14  gcse.async = true;
15  gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
16      '//www.google.com/cse/cse.js?cx=' + cx;
17  var s = document.getElementsByTagName('script')[0];
18  s.parentNode.insertBefore(gcse, s);
19
20  var executeQuery = function(e) {
21    var element = google.search.cse.element.getElement('results');
22    if (searchInput.value == '') {
23      element.clearAllResults();
24    } else {
25      element.execute(searchInput.value);
26    }
27    e.preventDefault();
28    return true;
29  }
30
31  form.addEventListener('submit', executeQuery);
32
33  // Attach autocomplete to the search box
34  var enableAutoComplete = function() {
35    google.search.CustomSearchControl.attachAutoCompletionWithOptions(
36      cx, searchInput, form,
37      // set to true to prevent the search box form from being submitted, since
38      // the search control displaying the results is on the same page.
39      {'preferOnSubmitToSubmit': true}
40     );
41  };
42
43  var myAutocompleteCallback = function() {
44    // Search module is loaded.
45    if (document.readyState == 'complete') {
46      enableAutoComplete();
47    } else {
48      google.setOnLoadCallback(enableAutoComplete, true);
49    }
50  };
51
52  window.__gcse = {
53    callback: myAutocompleteCallback
54  };
55
56})();
57