function CSearchCombo(parent, popt)
{
	this.options = popt || {};
	this.css = this.options.css || {};
	
	this.customData = popt.customData || {};

	this.selitem = -1;
	this.curitem = -1;

	this.parent = (typeof parent == "string" ? document.getElementById(parent) : parent);
	this.list = document.createElement("div");;
	this.textbox = document.createElement("input");

	this.parent.style.position = "relative";

	
	this.textbox.type = "text";
	this.textbox.className = this.css.textBoxClassName;
	this.textbox.oldtext = null;
	this.textbox.csc_objid = CSearchCombo.objlist.length;
	this.textbox.setAttribute("maxlength", this.options.maxlength);

	this.list.style.display = "none";
	this.list.style.position = "absolute";
	this.list.className = this.css.listClassName;
	this.list.csc_objid = CSearchCombo.objlist.length;


	this.parent.appendChild(this.textbox);
	this.parent.appendChild(this.list);

	

	CSearchCombo.objlist.push(this);



	var ktext = "";
	this.textbox.onkeyup = function(event)
	{
		var ev = event || window.event;
		var textbox = ev.srcElement || ev.target;
		var scombo = CSearchCombo.getSComboObject(textbox);

		var keycode = ev.keyCode;
		if (keycode != 27 && keycode != 13 && keycode != 9 && keycode != 40 && keycode != 38)
		{
			ktext = textbox.value;
			window.setTimeout(function() { if (ktext == textbox.value) scombo.searchFill(); }, 300);
		}
	}

	this.textbox.onclick = function(event)
	{
		var ev = event || window.event;
		
		ev.cancelBubble = true;
		return false;
	}

	this.textbox.onfocus = function(event)
	{
		var ev = event || window.event;
		var textbox = ev.srcElement || ev.target;
		var scombo = CSearchCombo.getSComboObject(textbox);

		CSearchCombo.hideAll();
		
		var sel = scombo.getSelectedData();
		if (sel != null)
		{
			textbox.value = scombo.options.getItemSearchText(sel, scombo);
		}

		if (scombo.options.showOnFocus == true)
		{
			if (sel == null)
			{
				scombo.searchFill();
			}
			
			scombo.showList();
		}
		
	}
	
	this.textbox.onblur = function(event)
	{
		var ev = event || window.event;
		var textbox = ev.srcElement || ev.target;
		var scombo = CSearchCombo.getSComboObject(textbox);
		
		var sel = scombo.getSelectedData();
		if (sel != null && textbox.value != "")
		{
			textbox.value = scombo.options.getItemFullText(sel, scombo);
		}

		//scombo.hideList();
	}

	
	
	this.textbox.onkeydown = function(event)
	{
		var ev = event ? event : window.event;
		var textbox = ev.srcElement ? ev.srcElement : ev.target;
		var scombo = CSearchCombo.getSComboObject(textbox);
		
		scombo.handleKeyEvent(ev.keyCode);
	}
}


CSearchCombo.objlist = [];


CSearchCombo.getSComboObject = function(el)
{
	while (el)
	{
		if (el.csc_objid != null) return CSearchCombo.objlist[el.csc_objid];
		
		el = el.parentNode;
	}
	
	return null;
}

CSearchCombo.hideAll = function()
{
	var list = CSearchCombo.objlist;
	for (var i = 0, num = list.length; i < num; i++)
	{
		var c = list[i];
		if (c.list.style.display == "")
		{
			c.list.style.display = "none";
			break;
		}
	}
}

CUtils.addEventHandler(document.body, 'click',  CSearchCombo.hideAll);


CSearchCombo.prototype.searchFill = function()
{
	if (this.textbox.oldtext == this.textbox.value) return;

	this.textbox.oldtext = this.textbox.value;
	
	this.removeAll();
	this.options.searchFill(this, this.textbox.value);		

	if (this.list.childNodes.length == 0)
	{
		this.hideList();
		return;
	}

	
	this.setCurrentIndex(0);
	this.showList();
}


CSearchCombo.prototype.showList = function()
{
	if (this.list.childNodes.length > 0)
	{
		this.list.style.top = (this.textbox.offsetTop + this.textbox.offsetHeight) + "px";
		this.list.style.left = (this.textbox.offsetLeft) + "px";
		this.list.style.width = this.textbox.offsetWidth + "px";
		this.list.style.display = "";
	}
}
	
	
CSearchCombo.prototype.hideList = function()
{
	this.list.style.display = "none";
}

	
CSearchCombo.prototype.selectCurrentItem = function()
{
	if (this.list.style.display == "")
	{
	}
	
	this.selitem = this.curitem;
	
	if (this.selitem >= 0 && this.selitem < this.list.childNodes.length)
	{
		var sel = this.list.childNodes[this.selitem].csc_itemdata;

		this.textbox.value = this.options.getItemFullText(sel, this);
	}
	
	this.hideList();
}


CSearchCombo.prototype.setText = function(text)
{
	this.textbox.value = text;
}


CSearchCombo.prototype.getText = function()
{
	return this.textbox.value;
}

CSearchCombo.prototype.getSelectedData = function()
{
	if (this.selitem >= 0 && this.selitem < this.list.childNodes.length && this.textbox.value != "")
	{
		return this.list.childNodes[this.selitem].csc_itemdata;
	}
	return null;
}


CSearchCombo.prototype.getSelectedIndex = function()
{
	if (this.textbox.value != "")
		return this.selitem;
	
	return -1;
}


CSearchCombo.prototype.getSelectedItem = function()
{
	if (this.selitem >= 0 && this.selitem < this.list.childNodes.length)
	{
		return this.list.childNodes[this.selitem];
	}
	return null;
}


CSearchCombo.prototype.setCurrentIndex = function(index)
{
	if (index >= 0 && index < this.list.childNodes.length && index != this.curitem)
	{
		// remove current selection
		if (this.curitem >= 0 && this.curitem < this.list.childNodes.length)
		{
			this.list.childNodes[this.curitem].className = this.css.itemClassName;
		}
		// set current selection
		this.list.childNodes[index].className = this.css.selectionClassName;
		
		this.curitem = index;
	}
}


CSearchCombo.prototype.setSelectedIndex = function(index)
{
	if (index >= 0 && index < this.list.childNodes.length && index != this.selitem)
	{
		this.selitem = index;
		
		this.setCurrentIndex(index);
	}
}	

	
CSearchCombo.prototype.removeAll = function()
{
	this.selitem = -1;
	this.curitem = -1;

	for (var i = 0, numrows = this.list.childNodes.length; i < numrows; i++)
	{
		var r = this.list.childNodes[0];
		r.onmouseover = null;
		r.onclick = null;
		this.list.removeChild(r);
	}
}


CSearchCombo.prototype.addItem = function(atext, data)
{
	var index = this.list.childNodes.length;
	var item = document.createElement("div");

	item.className = this.css.itemClassName;
	item.csc_itemdata = data;
	item.innerHTML = atext.join("");

	this.list.appendChild(item);

	var scombo = this;
	item.onmouseover = function(event)
	{
		scombo.setCurrentIndex(index);
	}
	
	item.onclick = function(event)
	{
		var ev = event ? event : window.event;
		
		ev.cancelBubble = true;
		scombo.selectCurrentItem();
		return false;
	}
}


CSearchCombo.prototype.getItemCount = function()
{
	return this.list.childNodes.length;
}


CSearchCombo.prototype.handleKeyEvent = function(keycode)
{		
	if (keycode == 13) // enter
	{
		this.selectCurrentItem();
	}
	if (keycode == 9) // tab
	{
		this.hideList();
	}
	else if (keycode == 27) // esc
	{
		this.hideList();
	}
	else if (keycode == 40) // down
	{
		if (this.list.style.display == 'none')
		{
			this.showList();
			return;
		}
		
		this.setCurrentIndex(this.curitem + 1);
		var item = this.list.childNodes[this.curitem];
		if (item.offsetTop + item.offsetHeight - this.list.offsetHeight > this.list.scrollTop)
			this.list.scrollTop += item.offsetHeight;
	}
	else if (keycode == 38) //up
	{
		if (this.list.style.display == '')
		{
			this.setCurrentIndex(this.curitem - 1);
			var item = this.list.childNodes[this.curitem];
			if (item.offsetTop < this.list.scrollTop)
				this.list.scrollTop = item.offsetTop;
		}
	}
	//TODO: pgup, pgdn
}
	
