
SelList = function()
	{
		this.Obj			= null;
		this.keyTimeOut		= null;
		this.selectValue	= null;
		
		this.Get = function()
			{
				this.Get.AJAX		= new AjaxLite();
				var AJAX			= this.Get.AJAX;
				AJAX.URL			= 'data.php/q/' + this.Obj.Q + '/';
				AJAX.Post['q']		= this.Obj.Q;
				AJAX.Post['value']	= this.Value();
				AJAX.Post['multi']	= (this.Obj.Multiple) ? 1 : 0;
				AJAX.ORSC			= function() { $SelList.Get.AJAX.doCheck(); };
				AJAX.doOnChange		= function()
					{
						if(this.Re.length > 1)
							{
								var oneSelected	= false;
								$('SelListID').innerHTML = '';
								for(var $i = 1; $i < this.Re.length; $i++)
									{
										var Span			= document.createElement('span');
										Span.value			= $HTML(this.Re[$i]);
										Span.className		= ($SelList.selectValue == Span.value) ? 'Selected' : '';
										Span.onmouseover	= function() { $SelList.Select(this); };
										Span.onclick		= function() { $SelList.doSelect(); };
										Span.innerHTML		= this.Re[$i];
										$('SelListID').appendChild(Span);

										if(Span.className != '')
											oneSelected = true;
									};

								if(!oneSelected)
									$('SelListID').childNodes[0].onmouseover();

								$SelList.Show();
							}
						else
							{
								$SelList.doHide();
							}
					};
				AJAX.doRequest();
			}

		this.Select = function(Obj)
			{
				this.selectValue	= null;
				var Span			= $('SelListID').getElementsByTagName('span');

				for(var $i = 0; $i < Span.length; $i++)
					{
						Span[$i].className = '';
						if(Obj == Span[$i])
							{
								Span[$i].className	= 'Selected';
								this.selectValue	= Obj.value;
								
								var Bottom	= ($('SelListID').scrollTop + $('SelListID').offsetHeight - Obj.offsetHeight);
								if(Bottom < Obj.offsetTop)
									$('SelListID').scrollTop = (Obj.offsetTop + Obj.offsetHeight) - $('SelListID').clientHeight;

								if($('SelListID').scrollTop > Obj.offsetTop)
									$('SelListID').scrollTop = Obj.offsetTop;
							}
					}
			}
		
		this.doSelect = function()
			{
				if(this.selectValue != null)
					{
						this.Obj.focus();
						var Cur = this.Cursor();
						
						this.Obj.value = this.Obj.value.substring(0, Cur[0]) + ((Cur[0] > 0) ? ' ' : '') + this.selectValue + this.Obj.value.substring(Cur[1]);
						if(typeof(this.Obj.selectionStart) != 'undefined')
							{
								if(Cur[0] > 0)
									Cur[0]++;

								this.Obj.selectionStart		= Cur[0];
								this.Obj.selectionEnd		= Cur[0] + this.selectValue.length;
							}
						else if(this.Obj.createTextRange)
							{
								
							}
						
						return false;
					}
				return true;
			}

		this.Walk = function(Wat)
			{
				if(this.selectValue != null)
					{
						var Span		= $('SelListID').getElementsByTagName('span');
						for(var $i = 0; $i < Span.length; $i++)
							{
								if(Span[$i].className == 'Selected' && ($i + Wat) >= 0 && ($i + Wat) < Span.length)
									{
										Span[$i+Wat].onmouseover();
										break;
									}
							}
					}
			}
		
		this.Change = function(Sel)
			{
				Sel.focus();
				//Sel.selectedIndex = -1;
				//this.Hide();
				this.Get();
			}

		this.Do = function() { this.Get(); }

		this.Show = function()
			{
				clearTimeout(this.hideTimeout);
				if($('SelListID').style.display != '')
					{
						$('SelListID').style.left	= getPos(this.Obj, 'left') + 'px';
						$('SelListID').style.top	= (getPos(this.Obj, 'top') + this.Obj.offsetHeight) + 'px';
						$('SelListID').style.width	= (this.Obj.offsetWidth + 17) + 'px';
					}

				$('SelListID').style.display = '';
			}

		this.Hide = function()
			{
				clearTimeout(this.keyTimeOut);
				this.hideTimeout = setTimeout('$SelList.doHide();', 800);
			}

		this.doHide = function()
			{
				clearTimeout(this.keyTimeOut);
				this.selectValue = null;
				$('SelListID').style.display = 'none';
			}

		this.keyPress = function(Obj, e)
			{
				if($SelList.Obj != Obj)
					$SelList.doHide();

				this.Obj	= Obj;
				var e		= (window.event) ? window.event : e;
				var Code	= e.keyCode;

				if(Code == 13)
					{
						return this.doSelect();
					}
				else if(Code == 40 || Code == 38)
					{
						this.Walk((Code == 40) ? 1 : -1);
					}
				else
					{
						clearTimeout(this.keyTimeOut);
						this.keyTimeOut = setTimeout('$SelList.Do();', 800);
					}
			}

		this.Cursor = function()
			{
				if(this.Obj.createTextRange)
					var SelectieStart	= Math.abs(document.selection.createRange().moveStart("character", -1000000));
				else if(this.Obj.selectionStart || this.Obj.selectionStart == 0)
					var SelectieStart	= this.Obj.selectionStart;

				var Value			= this.Obj.value;
				var Begin			= Value.substring(0, SelectieStart).lastIndexOf(",");
				Begin				= (Begin > -1) ? Begin + 1 : 0;
				var Eind			= Value.substring(SelectieStart).indexOf(",");
				Eind				= (Eind > -1) ? SelectieStart + Eind : Value.length;
				
				return [Begin, Eind];
			}

		this.Value = function()
			{
				if(this.Obj.Multiple)
					{
						var Cur = this.Cursor();

						return this.Obj.value.substring(Cur[0], Cur[1]).replace(/^\s+/, '').replace(/\s+$/, '');
					}
				return this.Obj.value;
			}
		
		this.createInput = function(Name, Value, Q, Width, Multiple)
			{
				var Div						= document.createElement('div');
				
				var Input					= document.createElement('input');
				Input.name					= Name;
				Input.className				= 'Text ForPointer';
				Input.value					= Value;
				Input.Q						= Q;
				Input.Multiple				= (typeof(Multiple) == 'boolean') ? Multiple : false;
				if(!isNaN(parseInt(Width)))
					Input.style.width		= (Width - 20) + 'px';
				Input.onfocus				= function(e) { clearTimeout($SelList.hideTimeout); this.onkeydown(e); };
				Input.onblur				= function() { $SelList.Hide(); };
				Input.onkeydown				= function(e) { return $SelList.keyPress(this, e); };
				Input.onkeypress			= function(e)
					{
						var e = (window.event) ? window.event : e;
						if($SelList.selectValue != null && e.keyCode == 13)
							return false;
					};
				Div.appendChild(Input);
				
				var Input					= document.createElement('input');
				Input.type					= 'text';
				Input.className				= 'Text Pointer';
				Input.onfocus				= function() { this.onclick(); this.blur(); };
				Input.onkeypress			= function() { return false; };
				Input.oncontextmenu			= function() { return false; };
				Input.onclick				= function() { this.parentNode.childNodes[0].focus(); };
				Input.readOnly				= true;
				Div.appendChild(Input);
				
				return Div;
			}

		this.Init = function()
			{
				/*$('SelListID').onchange			= function() { $SelList.Change(this); };
				$('SelListID').onfocus			= function() { clearTimeout($SelList.hideTimeout); };
				$('SelListID').onblur			= function() { $SelList.Hide(); };
				$('SelListID').onkeypress		= function(e)
					{
						var e = (window.event) ? window.event : e;
						if(e.keyCode == 13 || (e.keyCode == 38 && !this.selectedIndex))
							{
								this.blur();
								//this.selectedIndex = -1;
								$SelList.Obj.select();
							};
					}*/
			}
	}

$SelList = new SelList;
