1 function convertToId(search)
4 for (i=0;i<search.length;i++)
6 var c = search.charAt(i);
7 var cn = c.charCodeAt(0);
8 if (c.match(/[a-z0-9]/))
14 result+="_0"+cn.toString(16);
18 result+="_"+cn.toString(16);
24 function getXPos(item)
29 while (item && item!=document.body)
32 item = item.offsetParent;
38 function getYPos(item)
43 while (item && item!=document.body)
46 item = item.offsetParent;
52 /* A class handling everything associated with the search panel.
55 name - The name of the global variable that will be
56 storing this instance. Is needed to be able to set timeouts.
57 resultPath - path to use for external files
59 function SearchBox(name, resultsPath, inFrame, label)
61 if (!name || !resultsPath) { alert("Missing parameters to SearchBox."); }
63 // ---------- Instance variables
65 this.resultsPath = resultsPath;
67 this.keyTimeoutLength = 500;
68 this.closeSelectionTimeout = 300;
69 this.lastSearchValue = "";
70 this.lastResultsPage = "";
73 this.searchActive = false;
74 this.insideFrame = inFrame;
75 this.searchLabel = label;
77 // ----------- DOM Elements
79 this.DOMSearchField = function()
80 { return document.getElementById("MSearchField"); }
82 this.DOMSearchSelect = function()
83 { return document.getElementById("MSearchSelect"); }
85 this.DOMSearchSelectWindow = function()
86 { return document.getElementById("MSearchSelectWindow"); }
88 this.DOMPopupSearchResults = function()
89 { return document.getElementById("MSearchResults"); }
91 this.DOMPopupSearchResultsWindow = function()
92 { return document.getElementById("MSearchResultsWindow"); }
94 this.DOMSearchClose = function()
95 { return document.getElementById("MSearchClose"); }
97 this.DOMSearchBox = function()
98 { return document.getElementById("MSearchBox"); }
100 // ------------ Event Handlers
102 // Called when focus is added or removed from the search field.
103 this.OnSearchFieldFocus = function(isActive)
105 this.Activate(isActive);
108 this.OnSearchSelectShow = function()
110 var searchSelectWindow = this.DOMSearchSelectWindow();
111 var searchField = this.DOMSearchSelect();
113 if (this.insideFrame)
115 var left = getXPos(searchField);
116 var top = getYPos(searchField);
117 left += searchField.offsetWidth + 6;
118 top += searchField.offsetHeight;
120 // show search selection popup
121 searchSelectWindow.style.display='block';
122 left -= searchSelectWindow.offsetWidth;
123 searchSelectWindow.style.left = left + 'px';
124 searchSelectWindow.style.top = top + 'px';
128 var left = getXPos(searchField);
129 var top = getYPos(searchField);
130 top += searchField.offsetHeight;
132 // show search selection popup
133 searchSelectWindow.style.display='block';
134 searchSelectWindow.style.left = left + 'px';
135 searchSelectWindow.style.top = top + 'px';
138 // stop selection hide timer
139 if (this.hideTimeout)
141 clearTimeout(this.hideTimeout);
144 return false; // to avoid "image drag" default event
147 this.OnSearchSelectHide = function()
149 this.hideTimeout = setTimeout(this.name +".CloseSelectionWindow()",
150 this.closeSelectionTimeout);
153 // Called when the content of the search field is changed.
154 this.OnSearchFieldChange = function(evt)
156 if (this.keyTimeout) // kill running timer
158 clearTimeout(this.keyTimeout);
162 var e = (evt) ? evt : window.event; // for IE
163 if (e.keyCode==40 || e.keyCode==13)
167 this.OnSearchSelectShow();
168 var win=this.DOMSearchSelectWindow();
169 for (i=0;i<win.childNodes.length;i++)
171 var child = win.childNodes[i]; // get span within a
172 if (child.className=='SelectItem')
180 else if (window.frames.MSearchResults.searchResults)
182 var elem = window.frames.MSearchResults.searchResults.NavNext(0);
183 if (elem) elem.focus();
186 else if (e.keyCode==27) // Escape out of the search field
188 this.DOMSearchField().blur();
189 this.DOMPopupSearchResultsWindow().style.display = 'none';
190 this.DOMSearchClose().style.display = 'none';
191 this.lastSearchValue = '';
192 this.Activate(false);
197 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
199 if (searchValue != this.lastSearchValue) // search value has changed
201 if (searchValue != "") // non-empty search
203 // set timer for search update
204 this.keyTimeout = setTimeout(this.name + '.Search()',
205 this.keyTimeoutLength);
207 else // empty search field
209 this.DOMPopupSearchResultsWindow().style.display = 'none';
210 this.DOMSearchClose().style.display = 'none';
211 this.lastSearchValue = '';
216 this.SelectItemCount = function(id)
219 var win=this.DOMSearchSelectWindow();
220 for (i=0;i<win.childNodes.length;i++)
222 var child = win.childNodes[i]; // get span within a
223 if (child.className=='SelectItem')
231 this.SelectItemSet = function(id)
234 var win=this.DOMSearchSelectWindow();
235 for (i=0;i<win.childNodes.length;i++)
237 var child = win.childNodes[i]; // get span within a
238 if (child.className=='SelectItem')
240 var node = child.firstChild;
243 node.innerHTML='•';
247 node.innerHTML=' ';
254 // Called when an search filter selection is made.
255 // set item with index id as the active item
256 this.OnSelectItem = function(id)
258 this.searchIndex = id;
259 this.SelectItemSet(id);
260 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
261 if (searchValue!="" && this.searchActive) // something was found -> do a search
267 this.OnSearchSelectKey = function(evt)
269 var e = (evt) ? evt : window.event; // for IE
270 if (e.keyCode==40 && this.searchIndex<this.SelectItemCount()) // Down
273 this.OnSelectItem(this.searchIndex);
275 else if (e.keyCode==38 && this.searchIndex>0) // Up
278 this.OnSelectItem(this.searchIndex);
280 else if (e.keyCode==13 || e.keyCode==27)
282 this.OnSelectItem(this.searchIndex);
283 this.CloseSelectionWindow();
284 this.DOMSearchField().focus();
291 // Closes the results window.
292 this.CloseResultsWindow = function()
294 this.DOMPopupSearchResultsWindow().style.display = 'none';
295 this.DOMSearchClose().style.display = 'none';
296 this.Activate(false);
299 this.CloseSelectionWindow = function()
301 this.DOMSearchSelectWindow().style.display = 'none';
304 // Performs a search.
305 this.Search = function()
309 // strip leading whitespace
310 var searchValue = this.DOMSearchField().value.replace(/^ +/, "");
312 var code = searchValue.toLowerCase().charCodeAt(0);
316 hexCode="0"+code.toString(16);
320 hexCode=code.toString(16);
324 var resultsPageWithSearch;
327 if (indexSectionsWithContent[this.searchIndex].charAt(code) == '1')
329 resultsPage = this.resultsPath + '/' + indexSectionNames[this.searchIndex] + '_' + hexCode + '.html';
330 resultsPageWithSearch = resultsPage+'?'+escape(searchValue);
331 hasResultsPage = true;
333 else // nothing available for this search term
335 resultsPage = this.resultsPath + '/nomatches.html';
336 resultsPageWithSearch = resultsPage;
337 hasResultsPage = false;
340 window.frames.MSearchResults.location = resultsPageWithSearch;
341 var domPopupSearchResultsWindow = this.DOMPopupSearchResultsWindow();
343 if (domPopupSearchResultsWindow.style.display!='block')
345 var domSearchBox = this.DOMSearchBox();
346 this.DOMSearchClose().style.display = 'inline';
347 if (this.insideFrame)
349 var domPopupSearchResults = this.DOMPopupSearchResults();
350 domPopupSearchResultsWindow.style.position = 'relative';
351 domPopupSearchResultsWindow.style.display = 'block';
352 var width = document.body.clientWidth - 8; // the -8 is for IE :-(
353 domPopupSearchResultsWindow.style.width = width + 'px';
354 domPopupSearchResults.style.width = width + 'px';
358 var domPopupSearchResults = this.DOMPopupSearchResults();
359 var left = getXPos(domSearchBox) + 150; // domSearchBox.offsetWidth;
360 var top = getYPos(domSearchBox) + 20; // domSearchBox.offsetHeight + 1;
361 domPopupSearchResultsWindow.style.display = 'block';
362 left -= domPopupSearchResults.offsetWidth;
363 domPopupSearchResultsWindow.style.top = top + 'px';
364 domPopupSearchResultsWindow.style.left = left + 'px';
368 this.lastSearchValue = searchValue;
369 this.lastResultsPage = resultsPage;
372 // -------- Activation Functions
374 // Activates or deactivates the search panel, resetting things to
375 // their default values if necessary.
376 this.Activate = function(isActive)
378 if (isActive || // open it
379 this.DOMPopupSearchResultsWindow().style.display == 'block'
382 this.DOMSearchBox().className = 'MSearchBoxActive';
384 var searchField = this.DOMSearchField();
386 if (searchField.value == this.searchLabel) // clear "Search" term upon entry
388 searchField.value = '';
389 this.searchActive = true;
392 else if (!isActive) // directly remove the panel
394 this.DOMSearchBox().className = 'MSearchBoxInactive';
395 this.DOMSearchField().value = this.searchLabel;
396 this.searchActive = false;
397 this.lastSearchValue = ''
398 this.lastResultsPage = '';
403 // -----------------------------------------------------------------------
405 // The class that handles everything on the search results page.
406 function SearchResults(name)
408 // The number of matches from the last run of <Search()>.
409 this.lastMatchCount = 0;
411 this.repeatOn = false;
413 // Toggles the visibility of the passed element ID.
414 this.FindChildElement = function(id)
416 var parentElement = document.getElementById(id);
417 var element = parentElement.firstChild;
419 while (element && element!=parentElement)
421 if (element.nodeName == 'DIV' && element.className == 'SRChildren')
426 if (element.nodeName == 'DIV' && element.hasChildNodes())
428 element = element.firstChild;
430 else if (element.nextSibling)
432 element = element.nextSibling;
438 element = element.parentNode;
440 while (element && element!=parentElement && !element.nextSibling);
442 if (element && element!=parentElement)
444 element = element.nextSibling;
450 this.Toggle = function(id)
452 var element = this.FindChildElement(id);
455 if (element.style.display == 'block')
457 element.style.display = 'none';
461 element.style.display = 'block';
466 // Searches for the passed string. If there is no parameter,
467 // it takes it from the URL query.
469 // Always returns true, since other documents may try to call it
470 // and that may or may not be possible.
471 this.Search = function(search)
473 if (!search) // get search word from URL
475 search = window.location.search;
476 search = search.substring(1); // Remove the leading '?'
477 search = unescape(search);
480 search = search.replace(/^ +/, ""); // strip leading spaces
481 search = search.replace(/ +$/, ""); // strip trailing spaces
482 search = search.toLowerCase();
483 search = convertToId(search);
485 var resultRows = document.getElementsByTagName("div");
489 while (i < resultRows.length)
491 var row = resultRows.item(i);
492 if (row.className == "SRResult")
494 var rowMatchName = row.id.toLowerCase();
495 rowMatchName = rowMatchName.replace(/^sr\d*_/, ''); // strip 'sr123_'
497 if (search.length<=rowMatchName.length &&
498 rowMatchName.substr(0, search.length)==search)
500 row.style.display = 'block';
505 row.style.display = 'none';
510 document.getElementById("Searching").style.display='none';
511 if (matches == 0) // no results
513 document.getElementById("NoMatches").style.display='block';
515 else // at least one result
517 document.getElementById("NoMatches").style.display='none';
519 this.lastMatchCount = matches;
523 // return the first item with index index or higher that is visible
524 this.NavNext = function(index)
529 var focusName = 'Item'+index;
530 focusItem = document.getElementById(focusName);
531 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
535 else if (!focusItem) // last element
545 this.NavPrev = function(index)
550 var focusName = 'Item'+index;
551 focusItem = document.getElementById(focusName);
552 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
556 else if (!focusItem) // last element
566 this.ProcessKeys = function(e)
568 if (e.type == "keydown")
570 this.repeatOn = false;
571 this.lastKey = e.keyCode;
573 else if (e.type == "keypress")
577 if (this.lastKey) this.repeatOn = true;
578 return false; // ignore first keypress after keydown
581 else if (e.type == "keyup")
584 this.repeatOn = false;
586 return this.lastKey!=0;
589 this.Nav = function(evt,itemIndex)
591 var e = (evt) ? evt : window.event; // for IE
592 if (e.keyCode==13) return true;
593 if (!this.ProcessKeys(e)) return false;
595 if (this.lastKey==38) // Up
597 var newIndex = itemIndex-1;
598 var focusItem = this.NavPrev(newIndex);
601 var child = this.FindChildElement(focusItem.parentNode.parentNode.id);
602 if (child && child.style.display == 'block') // children visible
606 while (1) // search for last child
608 tmpElem = document.getElementById('Item'+newIndex+'_c'+n);
625 else // return focus to search field
627 parent.document.getElementById("MSearchField").focus();
630 else if (this.lastKey==40) // Down
632 var newIndex = itemIndex+1;
634 var item = document.getElementById('Item'+itemIndex);
635 var elem = this.FindChildElement(item.parentNode.parentNode.id);
636 if (elem && elem.style.display == 'block') // children visible
638 focusItem = document.getElementById('Item'+itemIndex+'_c0');
640 if (!focusItem) focusItem = this.NavNext(newIndex);
641 if (focusItem) focusItem.focus();
643 else if (this.lastKey==39) // Right
645 var item = document.getElementById('Item'+itemIndex);
646 var elem = this.FindChildElement(item.parentNode.parentNode.id);
647 if (elem) elem.style.display = 'block';
649 else if (this.lastKey==37) // Left
651 var item = document.getElementById('Item'+itemIndex);
652 var elem = this.FindChildElement(item.parentNode.parentNode.id);
653 if (elem) elem.style.display = 'none';
655 else if (this.lastKey==27) // Escape
657 parent.searchBox.CloseResultsWindow();
658 parent.document.getElementById("MSearchField").focus();
660 else if (this.lastKey==13) // Enter
667 this.NavChild = function(evt,itemIndex,childIndex)
669 var e = (evt) ? evt : window.event; // for IE
670 if (e.keyCode==13) return true;
671 if (!this.ProcessKeys(e)) return false;
673 if (this.lastKey==38) // Up
677 var newIndex = childIndex-1;
678 document.getElementById('Item'+itemIndex+'_c'+newIndex).focus();
680 else // already at first child, jump to parent
682 document.getElementById('Item'+itemIndex).focus();
685 else if (this.lastKey==40) // Down
687 var newIndex = childIndex+1;
688 var elem = document.getElementById('Item'+itemIndex+'_c'+newIndex);
689 if (!elem) // last child, jump to parent next parent
691 elem = this.NavNext(itemIndex+1);
698 else if (this.lastKey==27) // Escape
700 parent.searchBox.CloseResultsWindow();
701 parent.document.getElementById("MSearchField").focus();
703 else if (this.lastKey==13) // Enter
711 function setKeyActions(elem,action)
713 elem.setAttribute('onkeydown',action);
714 elem.setAttribute('onkeypress',action);
715 elem.setAttribute('onkeyup',action);
718 function setClassAttr(elem,attr)
720 elem.setAttribute('class',attr);
721 elem.setAttribute('className',attr);
724 function createResults()
726 var results = document.getElementById("SRResults");
727 for (var e=0; e<searchData.length; e++)
729 var id = searchData[e][0];
730 var srResult = document.createElement('div');
731 srResult.setAttribute('id','SR_'+id);
732 setClassAttr(srResult,'SRResult');
733 var srEntry = document.createElement('div');
734 setClassAttr(srEntry,'SREntry');
735 var srLink = document.createElement('a');
736 srLink.setAttribute('id','Item'+e);
737 setKeyActions(srLink,'return searchResults.Nav(event,'+e+')');
738 setClassAttr(srLink,'SRSymbol');
739 srLink.innerHTML = searchData[e][1][0];
740 srEntry.appendChild(srLink);
741 if (searchData[e][1].length==2) // single result
743 srLink.setAttribute('href',searchData[e][1][1][0]);
744 if (searchData[e][1][1][1])
746 srLink.setAttribute('target','_parent');
748 var srScope = document.createElement('span');
749 setClassAttr(srScope,'SRScope');
750 srScope.innerHTML = searchData[e][1][1][2];
751 srEntry.appendChild(srScope);
753 else // multiple results
755 srLink.setAttribute('href','javascript:searchResults.Toggle("SR_'+id+'")');
756 var srChildren = document.createElement('div');
757 setClassAttr(srChildren,'SRChildren');
758 for (var c=0; c<searchData[e][1].length-1; c++)
760 var srChild = document.createElement('a');
761 srChild.setAttribute('id','Item'+e+'_c'+c);
762 setKeyActions(srChild,'return searchResults.NavChild(event,'+e+','+c+')');
763 setClassAttr(srChild,'SRScope');
764 srChild.setAttribute('href',searchData[e][1][c+1][0]);
765 if (searchData[e][1][c+1][1])
767 srChild.setAttribute('target','_parent');
769 srChild.innerHTML = searchData[e][1][c+1][2];
770 srChildren.appendChild(srChild);
772 srEntry.appendChild(srChildren);
774 srResult.appendChild(srEntry);
775 results.appendChild(srResult);