			var selected, searchbar, currentLanguage;
			var head_element = document.getElementsByTagName('head').item(0);
			var google_suggest;
		
			function windowLoad() {
				searchbar = document.getElementById('searchbar');
				google_suggest = document.getElementById('google_suggest');

 
				var hash = window.location.hash.substr(1);
				if (!hash || hash == "") hash = 'web';
				
				selected = document.getElementById(hash);

				cookieCurrentLanguage();
				
			}
			
			function setClass(el, className) {
				el.setAttribute('class', className);
				el.setAttribute('className', className);
			} 			
			
			function clickOption(el) {
				if (selected) {
					selected.setAttribute('class', ''); // unselect old
					selected.setAttribute('className', ''); // unselect old
				}
				selected = el;
				selected.setAttribute('class', 'selected');	// select new	
				selected.setAttribute('className', 'selected');	// select new
			}
			
			function submitSearch(el) {
				var query = escape(document.getElementById('searchbar').value).replace('+', '%2B');
				document.location = '/' + selected.id + '?q=' + query + '#p=1';	
			}
			
			function addOpenSearch() {
				var name = 'heapr_web', ext = 'ico', meth = 'g';
				if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
					window.external.AddSearchProvider("http://mycroft.mozdev.org/externalos.php/" + name + ".xml");
				} else {
					alert("Unfortunately Safari, Opera, and IE6 do not support OpenSearch, which is needed for this plugin. Support for your browser is coming soon though!");
				}
			}
			
			
			function startGoogleSuggest() {
				var src;
				if (currentLanguage == "all") src = 'http://google.com/complete/search?callback=finishGoogleSuggest';
				else src = 'http://google.com/complete/search?callback=finishGoogleSuggest&hl=' + currentLanguage;
				
				var hash = window.location.hash.substr(1);
				if (hash == "images") {
					src += '&client=img&ds=i&q='; 
				} else if (hash == "videos") {
					src += '&client=serp&ds=yt&q=';
				} else { // default to web
					src += '&client=serp&q='; 
				}
	
				var query = escape(searchbar.value);		
				var s = document.createElement('script');
				s.setAttribute('src', src + query);
				head_element.appendChild(s);
			}
 
			function finishGoogleSuggest(obj) {
				currentSuggest = 0;
				if (obj[1].length > 0) {
					var suggestHTML = '<li style="display: none" id="' + searchbar.value + '"></li>'; // first element
					google_suggest.style.width = "100";
	
					for (var i = 0; i < obj[1].length; i++) {
						var entry = obj[1][i][0];
						var id = entry.replace(/<b>|<\/b>/gi,""); // remove bolds
						suggestHTML += '<li onmouseover="highlightSuggest(this)" onclick="clickSuggest(this)" id="' + id + '">' + entry + '</li>';
					}
		
					google_suggest.innerHTML = suggestHTML;
					if (google_suggest.offsetWidth < searchbar.offsetWidth) { 
						google_suggest.style.width = searchbar.offsetWidth + "px";
					}
 
					google_suggest.style.visibility = 'visible';
				} else {
					google_suggest.style.visibility = 'hidden';
				}
			}
 
 
			function highlightSuggest(el) {
				if (currentSuggest >= 0) setClass(google_suggest.childNodes[currentSuggest], ""); // deselect old suggest
				setClass(el, "hovered");
				currentSuggest = getChildIndex(google_suggest,el);
			}
			
			function getChildIndex(parent, child) {
				for (var k = 0; k < parent.childNodes.length; k++ ){
					if (parent.childNodes[k] == child) {
						return k;
					}
				}
			}			
 
			function clickSuggest(el) {
				searchbar.value = el.id;
				submitSearch(document.getElementById("header_form"));
			}
 
			function bodyClick() {
				google_suggest.style.visibility = 'hidden';
			}
 
			function keydownSuggest(e) {
				switch (e.keyCode) {
					case 9: case 27: // escape
						google_suggest.style.visibility = 'hidden';
						break;
 
					case 38: // up
						if (google_suggest.style.visibility == "visible") {
							var highlightIndex = 0, totalChildren = google_suggest.childNodes.length;
							if (currentSuggest == 0) highlightIndex = totalChildren - 1; 
							else if (currentSuggest == totalChildren) highlightIndex = 0;
							else highlightIndex = (currentSuggest-1) % totalChildren;
							var toHighlight = google_suggest.childNodes[highlightIndex];
							highlightSuggest(toHighlight);
							searchbar.value = toHighlight.id;
						} else {
							startGoogleSuggest();
						}
						break;
			
					case 40: // down
						if (google_suggest.style.visibility == "visible") {		
							var toHighlight = google_suggest.childNodes[(currentSuggest+1) % google_suggest.childNodes.length];
							highlightSuggest(toHighlight);
							searchbar.value = toHighlight.id;				
						} else {
							startGoogleSuggest();
						}	
						break;
				}
			}
 
			function keyupSuggest(e) {
				if (e.keyCode >= 46 || e.keyCode == 8) { // don't do anything if down key
					startGoogleSuggest();
				}
			}
			
			
			
			// has to be called after window load
			function cookieCurrentLanguage() {
				var cookie = getCookie('lang');
				if (cookie != null && cookie != "") {
					currentLanguage = cookie; 
				} else {
					currentLanguage = "en"; // default language is en
				}
			}
 
 
			function getCookie(c_name) {
				if (document.cookie.length > 0) {
					c_start=document.cookie.indexOf(c_name + "=");
					if (c_start!=-1) {
						c_start=c_start + c_name.length+1;
						c_end=document.cookie.indexOf(";",c_start);
						if (c_end==-1) c_end=document.cookie.length;
						return unescape(document.cookie.substring(c_start,c_end));
					}
				}
				return "";
			}
			

			