   /**
    * GOLLUM the wikipedia browser
    *
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GPL.
    *
    * Copyright(c) 2005 by Harald Hanek. All rights reserved.
    *
    * To contact the author write to: harald.hanek@easycp.de
    * The latest version of Wikipedia Browser can be obtained from: http://www.gollum-browser.com
    */
	
	function getPosition(el, at)
	{
		var p = 0;
		while(el)
		{
			p += el[at];
			el = el.offsetParent;
		}
		return p;
	}
	

	function trim(str)
	{
		return str.replace(/^(\s+)?(\S*)(\s+)?$/, '$2');
	}


	function ltrim(str)
	{
		return str.replace(/^\s*/, '');
	}


	function rtrim(str)
	{
		return str.replace(/\s*$/, '');
	}


	function delay(milliseconds)
	{
		var then, now;
		then = new Date().getTime();
		now = then;
		while((now - then) < milliseconds)
		{
			now = new Date().getTime();
		}
	}


 // Get Keycode
	function gkc(e)
	{
		if(document.layers)
			return e.which;
		else if(document.all)
			return event.keyCode;
		else if(document.getElementById)
			return e.keyCode;
		return 0;
	}
	
	
	
	
	
	
	
	
	
	
	
	
/*
Suggest for Gollum
*/




	function ajaxac_createXMLHttp()
	{
		var r = null;
		try {
			r = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {
			try {
				r = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (ee) {
				r = null;
			}
		}
		if(!r && typeof XMLHttpRequest != 'undefined')
			r = new XMLHttpRequest();
	
		return r;
	}


	function ajaxac_attachWidget(hook, id)
	{
		if(hook.length > 0 && id.length > 0)
		{
			evalStr = hook + " = document.getElementById('" + id + "');";
			eval(evalStr);
		}
	}
	
	function ajaxac_receivejsarray(code)
	{
		eval('var ret = ' + code);
		return ret;
	}
	
	function ajaxac_countdowntimer(cmd, ms)
	{
		this.cmd = cmd;
		this.ms = ms;
		this.tp = 0;
	}
	
	ajaxac_countdowntimer.prototype.start = function()
	{
		if (this.tp > 0)
			this.reset();
		this.tp = window.setTimeout(this.cmd, this.ms);
	}
	
	ajaxac_countdowntimer.prototype.reset = function()
	{
		if (this.tp > 0)
			window.clearTimeout(this.tp);
		this.tp = 0;
	}










/*
Suggest for Gollum - Teil 2
*/




	ajaxac_attachWidget('__l', 'cse');					// Label
	ajaxac_attachWidget('__a', 'search');				// Suchfeld
	ajaxac_attachWidget('__r', 'search-results');		// Result DIV


	var g_basicmatch = /[a-z0-9]/i;


	function g_getquery(elt, q)
	{
		q = ltrim(q);
		q = q.replace('\s+', ' ');
		if(q.length == 0 || !g_basicmatch.test(q))
		{
			g_emptyresults(elt);
			return '';
		}
	
		if(elt.currentQuery && (elt.currentQuery == q || elt.tempQuery == q))
			return '';
	
		elt.currentQuery = q;
		return q;
	}


	function g_hide()
	{
		if(__r) __r.style.display = 'none';
	}


	function g_ishidden()
	{
		return __r.style.display == 'none';
	}


	function g_show()
	{
		if(__r)
		{
			g_pos();
			__r.style.display = 'block';
		}
	}


	function g_pos()
	{
		if(__r)
		{
			__r.style.width = (navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1)?__l.offsetWidth-2+"px":__l.offsetWidth+"px";
			__r.style.top 	= getPosition(__l, "offsetTop") + __l.offsetHeight - 1 + "px";
			__r.style.left 	= getPosition(__l, "offsetLeft");
		}
	}


	function g_emptyresults(elt)
	{
		if (!__r) return;
		__r.innerHTML = '';
		__r.numResults = 0;
		__r.selectedIndex = 0;
		__r.results = [];
		g_hide();
	}


	function g_addresult(elt, qElt, q, c, sel)
	{
		if(!elt) return;
	
		if(sel) elt.selectedIndex = elt.numResults;
	
		idx = elt.numResults;
		elt.results[elt.numResults++] = q;
	
		var _res = '';
		_res += '<div class="' + (sel ? 'srs' : 'sr') + '"'
			 +  ' onmouseover="g_mouseover(\'' + elt.id + '\', \'' + qElt.id + '\', ' + idx + ')"'
			 +  ' onmouseout="g_mouseout(\'' + elt.id + '\', ' + idx + ')"'
			 +  ' onclick="g_mouseclick(\'' + elt.id + '\', \'' + qElt.id + '\', ' + idx + ')">';
		_res += '<span class="srt">' + q + '</span>';
		if (c.length > 0)
			_res += '<span class="src">' + c + '</span>';
		_res += '</div>';
	
		elt.innerHTML += _res;
	}


	function g_mouseover(id, qId, idx)
	{
		elt = document.getElementById(id);
		elt.selectedIndex = idx;
		qElt = document.getElementById(qId);
		qElt.focus();
	
		g_highlightsel(elt);
	}


	function g_mouseout(id, idx)
	{
		elt = document.getElementById(id);
		elt.selectedIndex = -1;
	
		g_highlightsel(elt);
	}


	function g_mouseclick(id, qId, idx)
	{
		elt = document.getElementById(id);
		qElt = document.getElementById(qId);
	
		qElt.value = elt.results[idx];
		//qElt.form.submit();
		wb.w._search('',true);
		g_hide();
	}


	function g_handleup(elt, qElt)
	{
		if(__r.numResults > 0 && g_ishidden())
		{
			g_show();
			return;
		}
	
		if (elt.selectedIndex == 0)
			return;
		else if (elt.selectedIndex < 0)
			elt.selectedIndex = elt.numResults - 1;
		else
			elt.selectedIndex--;
		g_highlightsel(elt, qElt);
	}


	function g_handledown(elt, qElt)
	{
		if(__r.numResults > 0 && g_ishidden())
		{
			g_show();
			return;
		}
	
		if (__r.selectedIndex == __r.numResults - 1)
			return;
		else if (__r.selectedIndex < 0)
			__r.selectedIndex = 0;
		else
			__r.selectedIndex++;
		g_highlightsel(__r, qElt);
	}


	function g_highlightsel(elt, qElt)
	{
		divs = elt.getElementsByTagName('div');
	
		for(var i = 0; i < divs.length; i++)
		{
			if(i == elt.selectedIndex)
			{
				divs[i].className = 'srs';
				elt.tempQuery = elt.results[i];
	
				if(qElt)
				{
					qElt.value = elt.results[i];
					if(qElt.createTextRange)
					{
						r = qElt.createTextRange();
						r.moveStart('character', elt.currentQuery.length);
						r.moveEnd('character', qElt.value.length);
						r.select();
					}
				}
			}
			else
				divs[i].className = 'sr';
		}
	}


	__a.onblur = function(e)
	{
		if(!e && window.event) e = window.event;
		if(g_ishidden()) return;
		setTimeout("g_hide();", 100);
		return;
	}


	__a.onkeydown = function(e)
	{
		key = gkc(e);
		switch(key)
		{
			case 27: // escape
				g_hide();
				return false;
				break;
			case 38: // up arrow
				g_handleup(__r, __a);
				return false;
				break;
			case 40: // down arrow
				g_handledown(__r, __a);
				return false;
				break;
		default:
			__gsctimer.start();
		}
		return true;
	}


	__r.onload = function() { g_emptyresults(this); }
	__r.onload();
	__gsctimer = new ajaxac_countdowntimer('__gsctimer.ontimerexpire()', 350);

	__gsctimer.ontimerexpire = function()
	{
		_q = g_getquery(__r, __a.value);
		if(_q.length == 0) return false;
		try
		{
			__gscfetch = ajaxac_createXMLHttp();
			__gscfetch.open('GET', x_fetchuri + '&wl=' + config.wikilang + '&q=' + encodeURIComponent(_q), true);
			__gscfetch_xmlhttpsuccess = function()
			{
				_data = ajaxac_receivejsarray(__gscfetch.responseText);
				g_emptyresults(__r);
				if(_data.length > 0)
				{
					for(i = 0; i < _data.length; i++)
					{
						g_addresult(__r, __a, _data[i][0], _data[i][1], i == 0);
					}
					g_show();
				}
			}
	
			__gscfetch_onreadystatechange = function()
			{
				if(__gscfetch.readyState == 4 && __gscfetch.status == 200 && __gscfetch_xmlhttpsuccess)
				{
					__gscfetch_xmlhttpsuccess();
				}
			}
	
			__gscfetch.onreadystatechange = __gscfetch_onreadystatechange;
			__gscfetch.send(null);
		}
		catch(e){ }
		return false;
	}
	
window.onresize = g_pos;
