listbox_scrollInterval = "";

function listbox_scrollUp(id){
	obj = document.getElementById(id);
	topp = parseInt(obj.style.top) + 5;
	if(topp > 0)
		topp = 0;
		
	listbox_setScrollPos(obj,topp);
		
	obj.style.top = topp+"px";
	
}

function listbox_scrollDown(id){
	obj = document.getElementById(id);
	topp = parseInt(obj.style.top) - 5;
	
	if(topp < (obj.parentNode.offsetHeight - obj.offsetHeight-20))
		topp = (obj.parentNode.offsetHeight - obj.offsetHeight-20)
	
	if(topp > 0)
		topp = 0;
		
	listbox_setScrollPos(obj,topp);

	obj.style.top = topp+"px";
}

function listbox_setScrollPos(obj,topp){
	scrollBalkenRaum = obj.parentNode.offsetHeight - 17 -17 -20-25 -4;
	maxScroll = obj.parentNode.offsetHeight - obj.offsetHeight-20;
	if(topp != 0)
		curScroll = topp / maxScroll;
	else	curScroll = 0;
	if(maxScroll > 0)
		curScroll = 0;
	scrollPos = curScroll * scrollBalkenRaum;
//	alert("pos: "+curScroll+" maxScroll: "+((-maxScroll) / 100)+" top:"+topp);
	document.getElementById("buttonScroll"+obj.parentNode.id).style.top = scrollPos+"px";
}

function listbox_setScrollUp(id){
	return setInterval("listbox_scrollUp('"+id+"')",50);
}

function listbox_setScrollDown(id){
	return setInterval("listbox_scrollDown('"+id+"')",50);
}

var listbox_deselect = false;
var listbox_lastHighlight = new Date().getTime();
function listbox_highlightItem(obj,listboxItemsName,deselect){
		
	if(deselect == true){
		items = document.getElementsByName(listboxItemsName);
		for(var i = 0; i < items.length; i++){
			items[i].style.backgroundColor = "";
		}
		listbox_deselect = true;
	}
	
	if(listbox_deselect == true)
		return false;
	
	if(( (new Date().getTime()) - listbox_lastHighlight) < 20){
		obj.getElementsByTagName("div")[0].style.backgroundColor = "#f0f6fe";
		return false;
	}
	//obj.style.border = "solid 1px red";


	items = document.getElementsByName(listboxItemsName);
	for(var i = 0; i < items.length; i++){
		items[i].style.backgroundColor = "";
	}
	
	obj.getElementsByTagName("div")[0].style.backgroundColor = "#b5d5ff";
	listbox_lastHighlight = new Date().getTime();
	return true;
}

var listbox_value = new Array();
function listbox_selectItem(obj,listboxId,value){

	if((( (new Date().getTime()) - listbox_lastHighlight) > 20)){
			listbox_deselect = false;
	}
	
	if(!listbox_highlightItem(obj,listboxId))
		return;
		
	if(listbox_value[listboxId]){
		if(listbox_value[listboxId]["selected"] == obj){
			listbox_highlightItem(obj,listboxId,true);
			listbox_value[listboxId]["value"] = "";
			listbox_value[listboxId]["selected"]= null;
			listbox_value[listboxId]["params"] = null;
			
			listbox = document.getElementById(listboxId);
			listbox.value = "";
			listbox.selected = null;
			listbox.params = null;
			
			if(listbox.onchange)
				listbox.onchange();
			return;
		}
	}
	
	
	
	listbox_value[listboxId] = new Array();
	listbox_value[listboxId]["value"] = value;
	listbox_value[listboxId]["selected"]= obj;
	listbox_value[listboxId]["params"] = new Array();

	for(var i = 3; i < arguments.length;++i){
		listbox_value[listboxId]["params"].push(arguments[i]);
	}
	
	listbox = document.getElementById(listboxId);
	listbox.value = value;
	listbox.selected = obj;
	listbox.params = listbox_value[listboxId]["params"];
	
	if(listbox.onchange)
		listbox.onchange();
}

function listbox_getSelectedValue(){
	return this.value;
}

function listbox_getSelectedItem(){
	return this.selected;
}

var listbox_oldInitFunc;

function listbox_init(){

	listboxes = document.getElementsByName("listbox");
						
			
	for(var listbox = 0; listbox < listboxes.length;listbox++){
		
		if(!listboxes[listbox].id)
			continue;
		listboxid = listboxes[listbox].id;
		down = document.getElementById("buttonDown"+listboxid);
		down.style.marginTop = (listboxes[listbox].offsetHeight-17-17-20-25-6)+"px";
		listboxes[listbox].getSelectedValue = listbox_getSelectedValue;
	}
	
	if(listbox_oldInitFunc)
		listbox_oldInitFunc();
}

listbox_oldInitFunc = window.onload;
window.onload = listbox_init;

function test(obj){
	div = document.getElementById("test").innerHTML = obj.value;
}
