/*----------------------------------------------------------
  DropDownList Class

  Adds Dropdown list functionality to an Input box
  
  Basic function:


  @package NaviciAjaxApi
  @author Arnd Beyer, arnd.beyer@wmdata.fi
  @copyright WM-data Novo 2006
  @version $Id: DropDownList.js,v 1.0 2005/07/26 12:44:42 arnd Exp $

  ---------------------------------------------------------*/
  
function DropDownList(inputObject,callbackFunction, css_base){
	
	var ioKeydownEvent=null;
	var ioBlurEvent = null;
	var hideTimerId = null;
	
	var options = new Array();
	var selectItems = new Array();
	var selectedId = null;
	var div = null;
	
	createDiv();
	setEvents();
	
	this.show = show;
	this.hide = hide;
	
	function createDiv(){
		div = document.createElement("div");
		div.className = css_base;
		inputObject.parentNode.appendChild(div);
		div.style.left = "0px";
		div.style.top = "0px";
		var pos0 = util.objectXY(div);
		var pos = util.objectXY(inputObject);
		div.style.left = (pos.x - pos0.x) + "px";
		div.style.top  = (pos.y - pos0.y + inputObject.offsetHeight ) + "px";
	}

	function setEvents(){
		ioBlurEvent = inputObject.onblur;
		inputObject.onblur = inputLeaveEvent;
		ioKeydownEvent = inputObject.onkeydown
		inputObject.onkeydown = keyDownHandling;
	}
	
	function inputLeaveEvent(event){
		//Delay the hiding of the 
		hideTimerId = setTimeout(hide,100);
		if(ioBlurEvent != null)
			ioBlurEvent(event);
	}
	
	function keyDownHandling(event){
	    var keyCode = null;
	    var ev = util.getEvent(event);
    
	    if(ev.keyCode)
	      keyCode = ev.keyCode;
	    else if(ev.which)
	      keyCode = ev.which;
	    if(div.style.visibility == "visible"){
			if(keyCode == 13 ){ //Enter
				if(selectedId != null)
					select(selectedId);
			}else if(keyCode==40 && (selectedId < options.length -1)){ //down
				highlight(selectedId+1);
			}else if(keyCode==38 && selectedId >0){ //up
				highlight(selectedId-1);
			}
		}
		if(ioKeydownEvent != null)
			ioKeydownEvent(event);
	}

	function show(selectList){
		empty();
		var start = options.length;
		for(var i=0; i <selectList.length; i++){
			options[start+i] = getOptionDiv(selectList[i], start+i);
			selectItems[start+i] = selectList[i];
			div.appendChild(options[start+i]);
		}
		selectedId = start;
		options[selectedId].className = css_base +"_item_select";
		div.style.visibility = "visible";
	}
	
	function hide(){
		if(hideTimerId!= null)
			clearTimeout(hideTimerId);
		div.style.visibility = "hidden";
	}
	
	function getOptionDiv(selectItem, arrayId){
		var option = document.createElement("div");
		option.appendChild(document.createTextNode(selectItem.caption));
		option.className = css_base +"_item";
		option.onmouseover=function(){ return highlight(arrayId);};
		option.onclick=function(){ return select(arrayId);};		
		return option;
	}
	
	function empty(){
		for(var i=0; i <options.length; i++){
			div.removeChild(options[i]);
		}
		options = new Array();
	}
	
	function highlight(arrayId){
		options[selectedId].className = css_base +"_item";
		selectedId = arrayId;
		options[selectedId].className = css_base +"_item_select";
	}

	
	function select(arrayId){
		hide();
		callbackFunction(selectItems[arrayId].callbackArgument);
	}
	
	
}