/**
* @author Natalia Bazhenova www.fh54.de (c) 2005-2007
*/
	
/********************************/
/*******start config****************/
/*******************************/
//array with suggest texts

/*var opttext=[
	"Amsterdam","Berlin","Bern","Prag","Paris","Vienna","Warsaw","Rome","Riga","Tallinn","Vilnius","Lisbon",
	"Madrid","Athens","Sofia","London","Bucharest","Budapest","Bratislava","Ljublana","Beograd","Brussels",
	"Helsinki","Stockholm","Copenhagen","Oslo","Dublin","Vaduz"
]*/


//id of the textbox element
var opttext = new Array();
//max size of "selectbox"
var maxcount=50
/*******************************/
/**********end config***********/
/*******************************/


var scripting=false;

var SIs = new Array()//initially invisible select box
var SItxt = new Array() //the main input text element
var newdiv= new Array() //a visible pendant to select box
newdiv[0]=document.createElement("DIV") //a visible pendant to select box
newdiv[1]=document.createElement("DIV") //a visible pendant to select box
newdiv[2]=document.createElement("DIV") //a visible pendant to select box
newdiv[3]=document.createElement("DIV") //a visible pendant to select box
newdiv[4]=document.createElement("DIV") //a visible pendant to select box
newdiv[5]=document.createElement("DIV") //a visible pendant to select box
newdiv[6]=document.createElement("DIV") //a visible pendant to select box
var globalN=0; //how much options scrolled up
var globalFIRST=0; //if first selindex=0 or not
//for ie
if (document.attachEvent)
	document.attachEvent("onclick",hideSelect)
// for Mozilla
if (document.captureEvents) {
	document.captureEvents(Event.CLICK);
	document.onclick = hideSelect;
}
function hideSelect() {
	newdiv[0].style.display="none"
	newdiv[1].style.display="none"
	newdiv[2].style.display="none"
	newdiv[3].style.display="none"
	newdiv[4].style.display="none"
	newdiv[5].style.display="none"
	newdiv[6].style.display="none"
}
function suggestInput_init() {

	var inputID = new Array();
	inputID[0]="Menu_fo";
	inputID[6]="Menu_leves";
	inputID[2]="Menu_koret";
	inputID[3]="Menu_udito";
	inputID[4]="Menu_savany";
	inputID[5]="Menu_egyeb";
	inputID[1]="Menu_desszert";

	if (document.createElement("DIV")) { //otherwise nothing happens
		for (idnum = 0; idnum <= 6; idnum++) {
			var list = document.getElementById("Suggest"+inputID[idnum]).innerHTML;
			opttext[idnum]=list.split("$")

			opttext[idnum].sort();


			scripting=true;
			SIs[idnum]=document.createElement("SELECT")
			SIs[idnum].Name = idnum
			/*SIs[idnum].onkeyup=function(e){
				if(!e) e=event; setInputValue(this.selectedIndex,e,this.Name)
			}*/
			SIs[idnum].className="select_input"
			SIs[idnum].setAttribute("id","selectInput");
			SIs[idnum].style.position="absolute"
			SIs[idnum].style.top="-9999px"
			SIs[idnum].style.left="-9999px"
			SIs[idnum].style.visibility="hidden"
			document.body.appendChild(SIs[idnum])
			
			SItxt[idnum] =document.getElementById(inputID[idnum]);
			SItxt[idnum].Name = idnum
			SItxt[idnum].onfocus = hideSelect;
			SItxt[idnum].setAttribute("autocomplete","OFF")
			SItxt[idnum].onkeyup=function(e){
				if (document.getElementById("suggest_ch").checked) {
					if (!e) e=event; showSelection(this.value,e,this.Name)
				}
				if (this.value == '') hideSelect()
			}
			SItxt[idnum].focus()
			for (i=0;i<opttext[idnum].length;i++) {
				o=document.createElement("OPTION");;
				o.innerHTML=opttext[idnum][i];
				SIs[idnum].appendChild(o)
				SIs[idnum].style.visibility="visible"; // for Opera
			}
			elt=SItxt[idnum]
			//find coords where the suggest div will appear
			pos2=findPos(elt); pos2.push(elt.offsetHeight); pos2.push(elt.offsetWidth)

			newdiv[idnum].style.top=(pos2[1]+pos2[2])+'px';
			newdiv[idnum].style.left=pos2[0]+'px';
			newdiv[idnum].style.width=pos2[3]+'px';
			newdiv[idnum].className="suggestBoxContainer"
			newdiv[idnum].style.display="none"
			buildDiv(0,idnum)
			document.body.appendChild(newdiv[idnum])
		}
	 }
} 
 
function buildDiv(n,idnum) {
	if (n>SIs[idnum].childNodes.length) 
		return false; 
	for (i=0;i<newdiv[idnum].childNodes.length;i++) {
		newdiv[idnum].removeChild(newdiv[idnum].childNodes[i]);i--
	}
	if (n>0) {// insert top "..." - div
		d1=document.createElement("DIV");
		d1.id="lessDiv"
		d1.style.width="100%"
		d1.style.fontSize="0.8em"
		d1.onmouseover=function() {this.className="mouse_over";}
		d1.onmouseout=function() {this.className="suggestBox"}
		d1.onclick=function() {
			buildDiv(n-1,idnum);
			d1.className="mouse_over"
		} 
		d1.innerHTML="......"; 
		newdiv[idnum].appendChild(d1); 
	}
	m=(maxcount<SIs[idnum].childNodes.length)?(maxcount):(SIs[idnum].childNodes.length)
	for(i=0;i<m;i++) {
		d=document.createElement("DIV")
		d.style.cursor="pointer"
		d.style.width="100%"
		d.style.fontSize="0.8em"
		d.onmouseover=function() {
			this.className="mouse_over";
			//SItxt[idnum].value=this.innerHTML
		}
		d.onmouseout=function() {
			this.className='suggestBox'
		}
		d.onclick=function() {
			SItxt[idnum].value=this.innerHTML;
			newdiv[idnum].style.display="none"
		}
		try {
			d.innerHTML=SIs[idnum].childNodes[i+n].innerHTML;
		}
		catch(err) {}
		newdiv[idnum].appendChild(d)
	};
	globalN=n;
	if (SIs[idnum].childNodes.length-n>maxcount) {// insert bottom "..." - div
		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,idnum); 
			d2.className="mouse_over";
		}
		d2.innerHTML="......"; 
		d2.className="suggestBox";
		newdiv[idnum].appendChild(d2) 
	 } 
 }
 
function setInputValue(m,ev,idnum) {
	if (!scripting) return;
	isLess=(document.getElementById("lessDiv"))?(1):(0)
	if (m>globalN+maxcount+isLess+1) {
		m=globalN+maxcount;SIs[idnum].selectedIndex=m
	}
	if (m<isLess) {
		m=globalN-1;SIs[idnum].selectedIndex=globalN-1
	}
	a=SIs[idnum].childNodes[m].innerHTML
	SItxt[idnum].value=a;
	try {
		if (newdiv[idnum].childNodes[m-globalN+isLess]) {  
			if (newdiv[idnum].childNodes[m-globalN+isLess].id=="moreDiv") { 
				buildDiv(globalN+1,idnum);  
				newdiv[idnum].childNodes[maxcount].className="mouse_over";;
				return
			}
		}
	} catch (err) {}
	try {
		if (newdiv[idnum].childNodes[m-globalN+isLess]) {  
			if (newdiv[idnum].childNodes[m-globalN+isLess].id=="lessDiv") {  
				buildDiv(globalN-1,idnum);
				isLess_new=(document.getElementById("lessDiv"))?(1):(0)
		 		newdiv[idnum].childNodes[isLess_new].className="mouse_over";
		 		return
			}
		}
	} catch (err) {}
	try {
		for (i=0;i<newdiv[idnum].childNodes.length;i++)
			newdiv[idnum].childNodes[i].className="suggestBox";
		 newdiv[idnum].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[idnum].style.display="none"
		SItxt[idnum].focus();
	}
}
 
function showSelection(t,ev,idnum) {
	if (!scripting) return;
	if (ev.keyCode==40) { // by arrow down comes into suggestion select
		 if (SIs[idnum].childNodes.length>0) {
		 	  for (var j = 0; j < SIs[idnum].childNodes.length; j++) { newdiv[idnum].childNodes[j].className="mouse_notover"; }

			  var selindex = SIs[idnum].selectedIndex;
			  if (globalFIRST == 1) selindex += 1;

			  if (selindex > SIs[idnum].childNodes.length - 1) { selindex = SIs[idnum].childNodes.length - 1; }
			  SIs[idnum].selectedIndex = selindex;


		 	  if (globalFIRST == 0) globalFIRST = 1;

			  
			  newdiv[idnum].childNodes[selindex].className="mouse_over";
			  SItxt[idnum].value=SIs[idnum].childNodes[selindex].innerHTML; 
			  /*try {
			  	SIs[idnum].focus();
			  } catch(err){}
			  */
			  SIs[idnum].childNodes[selindex].selected=true;
		 }
		 return            
	}
	if (ev.keyCode==38) { // by arrow up comes into suggestion select
		 if (SIs[idnum].childNodes.length>0) {
		 	  for (var j = 0; j < SIs[idnum].childNodes.length; j++) { newdiv[idnum].childNodes[j].className="mouse_notover"; }

			  var selindex = SIs[idnum].selectedIndex;

			  selindex -= 1;

			  if (selindex < 0) { selindex = 0; }
			  SIs[idnum].selectedIndex = selindex;
			  
			  newdiv[idnum].childNodes[selindex].className="mouse_over";
			  SItxt[idnum].value=SIs[idnum].childNodes[selindex].innerHTML; 
			  /*try {
			  	SIs[idnum].focus();
			  } catch(err){}
			  */
			  SIs[idnum].childNodes[selindex].selected=true;
		 }
		 return            
	}
	globalFIRST = 0;
	if (t=="") 
		return ;
	t=t.toLowerCase();
	l=t.length; 
	for (i=0; i < SIs[idnum].childNodes.length; i++) {
		SIs[idnum].removeChild(SIs[idnum].childNodes[i]);
		i--
	}
	for(i=0;i<opttext[idnum].length;i++) {
		 if (opttext[idnum][i].substr(0,l).toLowerCase()==t) {
		  	oOption = document.createElement("OPTION");
		  	SIs[idnum].appendChild(oOption)
		 	 oOption.innerHTML = opttext[idnum][i];
		 }
	}
	if (SIs[idnum].childNodes.length>0)  {
		newdiv[idnum].style.display=""
		buildDiv(0,idnum)
	} 
	else 
		newdiv[idnum].style.display="none";
	SItxt[idnum].focus()
}

/** Source: http://www.quirksmode.org/js/findpos.html - is better than my own**/
 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];
} 
