var adrDropDownInterface = 
{
	selectedKey : -1,
 	DropDown : null,
 	List : null,
	ListCache : null,
	filterCache : null,
	ClearHTMLValue : true,
	HighlightValue : true,
	DropDownClassName : "adrDropDown",
	previewValue : "",
	timer : null,
	request : null,
 					
 					GetLeft : function()
 					{
 						var X = this.offsetLeft;
 						var parent = this.offsetParent;
 						while (parent)
 						{
							if (parent.offsetLeft)
 								X = X + parent.offsetLeft;
 							parent = parent.offsetParent;
 						}
 						return X;
 					},
 					
 					GetTop : function()
 					{
 						var Y = this.offsetTop;
 						var parent = this.offsetParent;
 						while (parent)
 						{
							if (parent.offsetTop)
 							Y = Y + parent.offsetTop;
 							parent = parent.offsetParent;
 						}
 						return Y;
 					},
 					
					CreateDropDown : function()
					{
						this.DropDown = document.createElement("DIV");
						this.DropDown.Bind = this;
						this.DropDown.className = this.DropDownClassName;
						this.DropDown.style.position="fixed";
						this.DropDown.style.display = "none";
						this.DropDown.style.overflow = "hidden";
						this.parentNode.appendChild(this.DropDown);
					},
					
					FixPosition : function()
					{
						this.DropDown.style.left = (this.GetLeft())+"px";
						this.DropDown.style.top = (this.GetTop()+this.offsetHeight)+"px";
						this.DropDown.style.width = (this.offsetWidth) + "px";
					},
					
 					ShowDropDown : function()
 					{
 						var aList = null;
						aList = this.FillDropDown(this.value);
						if (aList && aList.length)
 							this.DropDown.style.display = "block";
						else
 							this.DropDown.style.display = "none";
 					},
 					
 					HideDropDown : function()
 					{
 						this.DropDown.style.display = "none";
 					},
					
 					Update : function(List)
					{
						this.ListCache = List;
						var filter = this.value;
 						var sHTML = "";
						sHTML+="<"+"ul>"; 						
 						for (var i=0;i<List.length;i++)
 						{
 							if (filter && this.HighlightValue)
 								sValue = "<b>"+List[i].substr(0,filter.length)+"</b>"+List[i].substr(filter.length);
 							else
 								sValue = List[i];
							if (this.selectedKey==i)
								sFocus = "style=\"background-color: blue\"";
							else
								sFocus = "";
 							sHTML+= "<li "+sFocus+" value='"+i+"' onclick='this.parentNode.parentNode.Bind.Select(this)' onmouseover='this.className=\"selected\"' onmouseout='this.className=\"\"';>"+sValue+"</li>";
 						}
						sHTML+="</ul>"; 						
 						this.DropDown.innerHTML=sHTML;
						var Element = null;
						var ListItems = this.DropDown.getElementsByTagName("LI");
						for (var i=0;i<ListItems.length;i++)
							if (ListItems[i].getAttribute('value')==this.selectedKey)
							{ Element = ListItems[i]; break;}
						if (Element)
						{
							var ElementTop = Element.offsetTop;
							var ElementBottom =  Element.offsetTop + Element.offsetHeight;
							var ViewBottom = this.DropDown.offsetHeight + this.DropDown.scrollTop;
							var ViewTop = this.DropDown.scrollTop;
							if (ElementBottom > ViewBottom) this.DropDown.scrollTop = ElementBottom-this.DropDown.offsetHeight;
							if (ElementTop < ViewTop) this.DropDown.scrollTop = ElementTop;
						} else this.DropDown.scrollTop = 0;
 						this.FixPosition();
						this.DropDown.style.display = List.length?"block":"none";
					},
					
 					FillDropDown : function(filter)
 					{
 						var aList = this.GetList(filter);
						if (!aList) return null;
						this.Update(aList);
						return aList;
 					},
 					
 					Select : function(item)
 					{
 						this.selectedKey=item.value;
						if (this.ClearHTMLValue)
 							this.value = item.textContent?item.textContent:item.innerText;
						else
							this.value = item.innerHTML; 
 						this.HideDropDown();
 					},
 					
 					GetList : function (filter)
 					{
						if (this.filterCache && this.filterCache==filter)
							return this.ListCache;
						var result;
 						switch(typeof(this.List))
 						{
 							case "object":
								result = this.List; break;
 							case "function":
								result = this.List(this, filter);break;
 							default:
 								throw "Undefined list";
						}
						this.selectedKey = -1;
						this.ListCache = result;
						this.filterCache = filter;
						return result;
 					},
 					SelectNext : function()
					{
						if (this.selectedKey+1<this.ListCache.length)
							this.selectedKey+=1;
						this.FillDropDown(this.value);
					},
 					SelectPrev : function()
					{
						if (this.selectedKey>0)	this.selectedKey-=1;
						this.FillDropDown(this.value);
					},
					SelectItem : function()
					{
						if (this.selectedKey != -1)
						if (this.ListCache[this.selectedKey])
						{
							this.value = this.ListCache[this.selectedKey];
 							this.HideDropDown();
						}
					},
 					_onkeyup : function (event)
 					{
						if (window.event) event = window.event;
						if (event.keyCode)
						{
							if (event.keyCode == 40)
							{
								this.SelectNext();
								event.cancelBubble = true;
							}
							if (event.keyCode == 38)
							{
								this.SelectPrev();
								event.cancelBubble = true;
							}	
							if (event.keyCode == 13 && this.DropDown.style.display=="block")
							{
								this.SelectItem();
								event.cancelBubble = true;
							}
						}
						if (event.cancelBubble) return ;						
						if (this.__onkeyup) eval(this.__onkeyup).call(this,event);
						this.ShowDropDown();
						//if (this.previewValue != this.value)
						//this.FillDropDown(this.value);
						//this.previewValue = this.value;
						event.cancelBubble = true;
 					},
 					_onfocus : function (event)
 					{
 						this.ShowDropDown();
 					},
 					_onblur : function (event)
 					{
 						window.setTimeout("document.getElementById('"+this.id+"').HideDropDown();",200);
 					},
 					Init : function()
 					{
 						if (!this.DropDown) 
 							this.CreateDropDown();
						
						this.__onkeyup = this.onkeyup;
						this.__onfocus = this.onfocus;
						this.__onblur = this.onblur;
						
						if (this.attachEvent)
						{
							this.attachEvent("onfocus",function() {event.srcElement._onfocus()});	
							this.attachEvent("onblur",function() {event.srcElement._onblur()});	
							//this.attachEvent("onkeyup",function() {event.srcElement._onkeyup()});	
							this.onkeyup = this._onkeyup;
						} else {
							this.addEventListener("focus",function(event){ event.target._onfocus(event);},false);
							this.addEventListener("blur",function(event){event.target._onblur(event);},false);
							//this.addEventListener("keyup", function(event){event.target._onkeyup(event);},false);
							this.onkeyup = this._onkeyup;
						}
						
 					}
 }
 function adrDropDown(element, remoteData)
 {
 	var Elem = Object.extend(element, adrDropDownInterface, true);
 	Elem.List = remoteData;
 }

