2 /****************************************************************/
\r
4 /****************************************************************/
\r
5 /* Copyright (c) 2002-2008 */
\r
6 /* Written by Harris Wong */
\r
7 /* Inclusive Design Institute */
\r
8 /* http://atutor.ca */
\r
10 /* This program is free software. You can redistribute it and/or*/
\r
11 /* modify it under the terms of the GNU General Public License */
\r
12 /* as published by the Free Software Foundation. */
\r
13 /****************************************************************/
\r
16 <script src="http://www.google.com/uds/api?file=uds.js<?php echo "&"; ?>hl=<?php echo $_SESSION['lang']; ?><?php echo "&"; ?>v=1.0<?php echo "&"; ?>key=<?php echo $search_key; ?>" type="text/javascript"></script>
\r
17 <script type="text/javascript">
\r
18 /* This can only be run after importing the google uds.
\r
19 * Ref: google ajax api.
\r
20 * @url http://code.google.com/apis/ajaxsearch/documentation/reference.html
\r
25 gsearch = new RawSearchControl(); //instantiate google search object.
\r
26 /* The following is for the side menu search. */
\r
27 var q = "<?php echo $side_menu_q; ?>";
\r
29 gsearch.instantSearch(q);
\r
34 * The RawSearchControl demonstrates how to use Searcher Objects
\r
35 * outside of the standard GSearchControl. This includes calling
\r
36 * searcher .execute() methods, reacting to search completions,
\r
37 * and if you had previously disabled html generation, how to generate
\r
38 * an html representation of the result.
\r
40 function RawSearchControl() {
\r
41 // latch on to key portions of the document
\r
42 this.searcherform = document.getElementById("searcher");
\r
43 this.results = document.getElementById("results");
\r
44 this.searchform = document.getElementById("searchform");
\r
47 this.currentSearchType = "";
\r
49 // create map of searchers as well as note the active searcher
\r
50 this.searchers = new Array();
\r
51 // create and wire up an instance of GwebSearch and one of
\r
52 // GlocalSearch. Note that we register to handle search completion notifications
\r
53 // when searches complete, they are called in the context of this instance
\r
54 // of RawSearchControl and they are passed the searcher that just completed
\r
55 for (var i=0; i<7; i++){
\r
56 var searcher = new GwebSearch();
\r
59 searcher = new GwebSearch();
\r
62 searcher = new GlocalSearch();
\r
63 searcher.setCenterPoint("98074");
\r
66 searcher = new GimageSearch();
\r
69 searcher = new GvideoSearch();
\r
72 searcher = new GnewsSearch();
\r
75 searcher = new GblogSearch();
\r
78 searcher = new GbookSearch();
\r
81 searcher.setNoHtmlGeneration();
\r
82 searcher.setSearchCompleteCallback(this,
\r
83 RawSearchControl.prototype.searchComplete,
\r
86 searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
\r
87 searcher.setLinkTarget("temp_new_search");
\r
88 this.searchers[i] = searcher;
\r
91 // now, create a search form and wire up a submit and clear handler
\r
92 this.searchForm = new GSearchForm(false, this.searchform);
\r
93 this.searchForm.setOnSubmitCallback(this,
\r
94 RawSearchControl.prototype.onSubmit);
\r
98 * figure out which searcher is active by looking at the radio
\r
101 RawSearchControl.prototype.computeActiveSearcher = function() {
\r
102 if (this.currentSearchType=="" ){
\r
103 this.activeSearcher = 0;
\r
105 this.activeSearcher = this.currentSearchType;
\r
110 * Inject the query into the form, and triggers a instant search
\r
113 RawSearchControl.prototype.instantSearch = function(q){
\r
114 this.searchForm.input.value = q;
\r
115 this.onSubmit(this.searchForm);
\r
119 * figure out which link is being selected, then change that style.
\r
120 * reset all other styles.
\r
121 * @param searcherTypeIndex is the integer that maps onto the searcherType string
\r
123 RawSearchControl.prototype.activate = function (searcherTypeIndex){
\r
124 for (var i=0; i<this.searchers.length; i++){
\r
125 var gsearcher = this.searchers[i];
\r
126 searcherType = this.getSearcherTypeString(i)[0];
\r
127 this.selector = document.getElementById(searcherType + "Selector");
\r
128 if (i==searcherTypeIndex){
\r
129 //Run this query, activate the menu bar.
\r
130 this.currentSearchType = searcherTypeIndex;
\r
131 cssSetClass(this.selector, "active");
\r
132 this.computeActiveSearcher();
\r
133 var q = this.searchForm.input.value;
\r
135 gsearcher.execute(q);
\r
138 cssSetClass(this.selector, "");
\r
144 * This functiion will retrieve the string of the searcher type by the given index
\r
146 RawSearchControl.prototype.getSearcherTypeString = function(searcherTypeIndex){
\r
148 switch(searcherTypeIndex){
\r
150 s = new Array("local", "http://www.google.ca/maps?");
\r
153 s = new Array("image", "http://www.google.ca/images?");
\r
156 s = new Array("video", "http://video.google.ca/videosearch?");
\r
159 s = new Array("news", "http://www.google.ca/news?");
\r
162 s = new Array("blog", "http://www.google.ca/blogsearch?");
\r
165 s = new Array("book", "http://www.google.ca/books?");
\r
169 s = new Array("web", "http://www.google.ca/search?");
\r
176 * onSubmit - called when the search form is "submitted" meaning that
\r
177 * someone pressed the search button or hit enter. The form is passed
\r
180 RawSearchControl.prototype.onSubmit = function(form) {
\r
181 this.computeActiveSearcher();
\r
182 if (form.input.value) {
\r
183 // if there is an expression in the form, call the active searcher's
\r
185 this.query = form.input.value;
\r
186 this.searchers[this.activeSearcher].execute(form.input.value);
\r
188 // always indicate that we handled the submit event
\r
193 * onClear - called when someone clicks on the clear button (the little x)
\r
195 RawSearchControl.prototype.onClear = function(form) {
\r
196 this.clearResults();
\r
200 * searchComplete - called when a search completed. Note the searcher
\r
201 * that is completing is passes as an arg because thats what we arranged
\r
202 * when we called setSearchCompleteCallback
\r
204 RawSearchControl.prototype.searchComplete = function(searcher) {
\r
205 // always clear old from the page
\r
206 this.clearResults();
\r
208 // if the searcher has results then process them
\r
209 if (searcher.results && searcher.results.length > 0) {
\r
210 // now manually generate the html that we disabled
\r
211 // initially and display it
\r
212 var div = createDiv("", "");
\r
213 this.results.appendChild(div);
\r
214 for (var i=0; i<searcher.results.length; i++) {
\r
215 var result = searcher.results[i];
\r
216 searcher.createResultHtml(result);
\r
218 div = result.html.cloneNode(true);
\r
220 div = createDiv("** failure to create html **");
\r
222 this.results.appendChild(div);
\r
224 div = createDiv("");
\r
225 a_elem = createAnchor("More results >>", this.generateMoreResultsURL());
\r
226 div.appendChild(a_elem);
\r
227 this.results.appendChild(div);
\r
229 var div = createDiv("No results for: " + this.searchForm.input.value, "");
\r
230 this.results.appendChild(div);
\r
235 * This function returns the appropriate link for the "More results" at the bottom of a search.
\r
236 * The "More results" should correspond to the category the search was performed in.(ie. maps, video, images...)
\r
237 * @return the URL described above.
\r
239 RawSearchControl.prototype.generateMoreResultsURL = function(){
\r
241 var link = this.getSearcherTypeString(this.activeSearcher)[1];
\r
242 return (link + "hl=<?php echo $_SESSION['lang']?>&q=" + this.searchForm.input.value);
\r
246 * clearResults - clear out any old search results
\r
248 RawSearchControl.prototype.clearResults = function() {
\r
249 removeChildren(this.results);
\r
253 * Static DOM Helper Functions
\r
255 function removeChildren(parent) {
\r
256 while (parent.firstChild) {
\r
257 parent.removeChild(parent.firstChild);
\r
260 function createDiv(opt_text, opt_className) {
\r
261 var el = document.createElement("div");
\r
263 el.innerHTML = opt_text;
\r
265 if (opt_className) { el.className = opt_className; }
\r
268 function createAnchor(a_text, a_href, a_className){
\r
269 var el = document.createElement("a");
\r
271 el.innerHTML = a_text;
\r
273 el.target = "temp_google_search";
\r
275 if (a_className){ a.className = a_className;}
\r
282 function cssSetClass(el, className) {
\r
283 el.className = className;
\r
286 // register to be called at OnLoad when the page loads
\r
287 GSearch.setOnLoadCallback(OnLoad);
\r
291 <form id="searcher" method="get" action="">
\r
293 <tr><td colspan="2">
\r
294 <div class="selector">
\r
296 <li><a id="webSelector" class="active" href="javascript:gsearch.activate(0);"><?php echo _AT('google_search'); ?></a></li>
\r
297 <li><a id="localSelector" href="javascript:gsearch.activate(1);"><?php echo _AT('google_search_local'); ?></a></li>
\r
298 <li><a id="imageSelector" href="javascript:gsearch.activate(2);"><?php echo _AT('google_search_images'); ?></a></li>
\r
299 <li><a id="videoSelector" href="javascript:gsearch.activate(3);"><?php echo _AT('google_search_videos'); ?></a></li>
\r
300 <li><a id="newsSelector" href="javascript:gsearch.activate(4);"><?php echo _AT('google_search_news'); ?></a></li>
\r
301 <li><a id="blogSelector" href="javascript:gsearch.activate(5);"><?php echo _AT('blogs'); ?></a></li>
\r
302 <li><a id="bookSelector" href="javascript:gsearch.activate(6);"><?php echo _AT('google_search_books'); ?></a></li>
\r
306 <td class="search-form">
\r
307 <div id="searchform">Loading</div>
\r
309 <!-- For clearing the results
\r
311 <input name="clearResult" value="clear" type="button" class="button" onclick="gsearch.clearResults();"/>
\r
317 <div id="results"></div>
\r