var maxcount=5; 

var scripting=false;

$(document).ready(function(){
	init();						   
});

var elementSelect;
var elementTextfield;
var newdiv=document.createElement("DIV");
var globalN=0;

//for ie
if (document.attachEvent)
	document.attachEvent("onclick",hideSelect)

// for Mozilla
if (document.captureEvents) {
	document.captureEvents(Event.CLICK);
	document.onclick = hideSelect;
}
function hideSelect() {
	newdiv.style.display="none"
}
function init() {
	if (document.createElement("DIV")) { 
		scripting=true;
		elementSelect=document.createElement("SELECT");
		elementSelect.onkeyup=function(e){
			if(!e) e=event; setInputValue(this.selectedIndex,e)
		};
		elementSelect.className="select_input";
		elementSelect.setAttribute("id","selectInput");
		elementSelect.style.position="absolute";
		elementSelect.style.top="-9999px";
		elementSelect.style.left="-9999px";
		elementSelect.style.visibility="hidden";
		document.body.appendChild(elementSelect);
		elementTextfield = document.getElementById("strasse");
		elementTextfield.setAttribute("autocomplete","OFF");
		elementTextfield.onkeyup=function(e){
			if (!e) e=event; showSelection(this.value,e)
		};
		elementTextfield.focus();
		for (i=0;i<strassenliste.length;i++) {
			o=document.createElement("OPTION");
			o.innerHTML=strassenliste[i];
			elementSelect.appendChild(o);
			elementSelect.style.visibility="visible";
		}

		// suche textfeld koordinaten
		pos2=findPos(elementTextfield); 
		pos2.push(elementTextfield.offsetHeight); 
		pos2.push(elementTextfield.offsetWidth)
		
		newdiv.style.top=(pos2[1]+pos2[2])+'px';
		newdiv.style.left=pos2[0]+'px';
		newdiv.style.width=pos2[3]+'px';
		newdiv.className="suchBoxRahmen";
		newdiv.style.display="none",
		buildDiv(0);
		document.body.appendChild(newdiv);
	 }
} 
 
function buildDiv(n) {
	if (n>elementSelect.childNodes.length) {
		return false; 
	}
	for (i=0;i<newdiv.childNodes.length;i++) {
		newdiv.removeChild(newdiv.childNodes[i]);i--
	}
	if (n>0) {
		d1=document.createElement("DIV");
		d1.id="lessDiv"
		d1.style.width="100%"
		d1.style.fontSize="0.6em"
		d.style.textAlign="left";
		d1.onmouseover=function() {this.className="mouse_over";}
		d1.onmouseout=function() {this.className="suchBox"}
		d1.onclick=function() {
			buildDiv(n-1);
			d1.className="mouse_over"
		} 
		d1.innerHTML="......"; 
		newdiv.appendChild(d1); 
	}
	m=(maxcount<elementSelect.childNodes.length)?(maxcount):(elementSelect.childNodes.length)
	for(i=0;i<m;i++) {
		d=document.createElement("DIV")
		d.style.width="100%"
		d.style.fontSize="0.6em"
		d.style.textAlign="left";
		d.onmouseover=function() {
			this.className="mouse_over";
			elementTextfield.value=this.innerHTML
		}
		d.onmouseout=function() {
			this.className='suchBox'
		}
		d.onclick=function() {
			elementTextfield.value=this.innerHTML;
			newdiv.style.display="none"
		}
		try {
			d.innerHTML=elementSelect.childNodes[i+n].innerHTML;
		}
		catch(err) {}
		newdiv.appendChild(d)
	};
	globalN=n;
	
	// Abfrage wenn anzahl der gefundenen strassen groesser ist als das anzeige div
	/*
	if (elementSelect.childNodes.length-n>maxcount) {
		d2=document.createElement("DIV");
		d2.id="moreDiv"
		d2.style.width="100%"
		d2.style.fontSize="0.8em"
		d2.onmouseover=function() {this.className="mouse_over";}
		d2.onmouseout=function() {this.className="suggestBox";}
		d2.onclick=function() {
			buildDiv(n+1); 
			d2.className="mouse_over";
		}
		d2.innerHTML="......"; 
		d2.className="suggestBox";
		newdiv.appendChild(d2) 
	 }
	 */
 }
 
function setInputValue(m,ev) {
	if (!scripting) return;
	isLess=(document.getElementById("lessDiv"))?(1):(0)
	if (m>globalN+maxcount+isLess+1) {
		m=globalN+maxcount;elementSelect.selectedIndex=m
	}
	if (m<isLess) {
		m=globalN-1;elementSelect.selectedIndex=globalN-1
	}
	a=elementSelect.childNodes[m].innerHTML
	elementTextfield.value=a;
	try {
		if (newdiv.childNodes[m-globalN+isLess]) {  
			if (newdiv.childNodes[m-globalN+isLess].id=="moreDiv") { 
				buildDiv(globalN+1);  
				newdiv.childNodes[maxcount].className="mouse_over";;
				return
			}
		}
	} catch (err) {}
	try {
		if (newdiv.childNodes[m-globalN+isLess]) {  
			if (newdiv.childNodes[m-globalN+isLess].id=="lessDiv") {  
				buildDiv(globalN-1);
				isLess_new=(document.getElementById("lessDiv"))?(1):(0)
		 		newdiv.childNodes[isLess_new].className="mouse_over";
		 		return
			}
		}
	} catch (err) {}
	try {
		for (i=0;i<newdiv.childNodes.length;i++)
			newdiv.childNodes[i].className="suggestBox";
		 newdiv.childNodes[m-globalN+isLess].className="mouse_over";
	} catch(err) {}; 
	if ((ev.keyCode!=40) && (ev.keyCode!=38) && (ev.keyCode!=0)) { // if not arrow down, arrow up or mouseclick  
		newdiv.style.display="none"
		elementTextfield.focus();
	}
}
 
function showSelection(t,ev) {
	if (!scripting) return;
	if (ev.keyCode==40) { // by arrow down comes into suggestion select
		 if (elementSelect.childNodes.length>0) {
			  newdiv.childNodes[0].className="mouse_over";
			  elementTextfield.value=elementSelect.childNodes[0].innerHTML; 
			  try {
			  	elementSelect.focus();
			  } catch(err){}
			  elementSelect.childNodes[0].selected=true;
		 }
		 return            
	}
	if (t=="") 
		return ;
	t=t.toLowerCase();
	l=t.length; 
	for (i=0;i<elementSelect.childNodes.length;i++) {
		elementSelect.removeChild(elementSelect.childNodes[i]);
		i--
	}
	for(i=0;i<strassenliste.length;i++) {
		 if (strassenliste[i].substr(0,l).toLowerCase()==t) {
		  	oOption = document.createElement("OPTION");
		  	elementSelect.appendChild(oOption)
		 	 oOption.innerHTML = strassenliste[i];
		 }
	}
	if (elementSelect.childNodes.length>0)  {
		newdiv.style.display=""
		buildDiv(0)
	} 
	else 
		newdiv.style.display="none";
	elementTextfield.focus()
}

// schnelle Methode zum ermitteln der Position des Parent Elements
 function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	} 
	return [curleft,curtop+15];
} 
