// active combo
var __ka_ComboActive = null;

Combo = function(values)
{
	this.values = values;
	this.visible = false;
	
	this.createDiv = function(e)
	{
		if (this.div != null) return this.div;
		
		this.div = document.createElement('DIV');
		this.div.style.position = 'absolute';
		this.div.style.background = '#FFFFFF';
		this.div.style.overflow = 'hidden';
		this.div.style.display = 'none';
		this.div.style.left = '0px';
		this.div.style.top = '0px';
		this.div.style.width = '1px';
		this.div.style.zIndex = 1022;
		this.div.combo = this;
		this.div.displayParent = e;
		
		document.body.insertBefore(this.div, document.body.firstChild);
		
		this.div.onmouseover = function(e)
		{
			this.combo.isMouseIn = true;
		}
		this.div.onmouseout = function(e)
		{
			this.combo.isMouseIn = false;
		}
		
		buf = '<table class="ka-combo" border="0" cellpadding="0" cellspacing="0" style="width:100%">';
		for(var i=0;i<this.values.length;i++)
		{
			var val = this.values[i];
			if (val == '') val = '&nbsp;';
			buf += '<tr class="ka-combo" onmouseover="this.className=\'ka-combo-active\';" onmouseout="this.className=\'ka-combo\';" onclick=\"this.offsetParent.offsetParent.combo.select(this.cells[0].innerHTML);\"><td nowrap="nowrap">'+val+'</td></tr>';
		}
		buf += '</table>';
		this.div.innerHTML = buf;
		
		return this.div;
	}
	
	this.select = function(value)
	{
		if (value == '&nbsp;') value = '';
		if (this.onSelect != null)
			this.onSelect(value);
	}
}

Combo.prototype.hide = function()
{
	this.div.style.display = 'none';
	this.div.style.left = '0px';
	this.div.style.top = '0px';
	
	if (__ka_ComboActive == this) __ka_ComboActive = null;
	this.visible = false;
}

Combo.prototype.show = function()
{
	// hide other combo
	if (__ka_ComboActive != null && __ka_ComboActive != this)
		__ka_ComboActive.hide();
	
	// only if not self
	if (__ka_ComboActive == this) return;

	this.div.style.visibility = 'hidden';
	this.div.style.display = 'block';
	
	var tbl = this.div.displayParent;
	var eParent = tbl, x = 0, y = tbl.offsetHeight, cx = this.div.childNodes[0].offsetWidth;
	while (eParent != null)
	{
		x += eParent.offsetLeft;
		y += eParent.offsetTop;
		eParent = eParent.offsetParent;
	}
    
	cx = Math.max(cx, tbl.offsetWidth);

	// adjust to screen height
	var scroll_cy = 0;
	if (self.innerHeight) scroll_cy = self.innerHeight;
	else if (document.documentElement&&document.documentElement.clientHeight) scroll_cy = document.documentElement.clientHeight;
	else if (document.body) scroll_cy = document.body.scrollHeight;

	var scroll_y = 0;
	if (self.pageYOffset) scroll_y = self.pageYOffset;
	else if (document.documentElement&&document.documentElement.scrollTop) scroll_y = document.documentElement.scrollTop;
	else if (document.body) scroll_y = document.body.scrollTop;
    
	var cy = this.div.childNodes[0].offsetHeight;
	if (y + cy > scroll_y + scroll_cy) {
		var ny = y - (cy + tbl.offsetHeight);
		if (ny >= 0) y = ny;
		y++;
	} else
		y--;
  
	this.div.style.width = cx + 'px';
	this.div.style.left = x + 'px';
	this.div.style.top = y + 'px';	
	this.div.style.visibility = 'visible';
	
	// set active combo
	this.visible = true;	
	__ka_ComboActive = this;
}

Combo.addEvent = function(el, evname, func)
{
	if (el.attachEvent)
	{	// IE
		el["on" + evname] = func;
		//el.attachEvent("on" + evname, func);
	}
	else if (el.addEventListener)
	{	// Gecko / W3C
		el.addEventListener(evname, func, true);
	}
	else
	{	// other
		el["on" + evname] = func;
	}
}


Combo.setup = function(params)
{
	function param_default(pname, def) { if (typeof params[pname] == 'undefined') { params[pname] = def; } };
	
	param_default('inputField',	null);
	param_default('parent',		null);
	param_default('button',		null);
		
	if (typeof params.inputField == 'string')
		params.inputField = document.getElementById(params.inputField);	
	
	if (typeof params.button == 'string')
		params.button = document.getElementById(params.button);
		
	if (typeof params.parent == 'string')
		params.parent = document.getElementById(params.parent);
		
	if (params.button == null || params.values == null) return;
	
	// create new combo instance
	var combo = new Combo(params.values);
	
	if (params.parent != null) combo.createDiv(params.parent);
	else if (params.inputField != null) combo.createDiv(params.inputField);
	else combo.createDiv(params.button);
	
	if (params.inputField != null)
	{
		// no intellisense
		params.inputField.setAttribute('autocomplete','off');
		
		combo._if = params.inputField;
		combo.onSelect = function(value)
		{
			this.hide();
			this._if.value = value;
			try { this._if.focus(); }
			catch (e) { }
		}	
	}
	
	params.button.combo = combo;
	Combo.addEvent(
		params.button, 'click',
		function(e) {
			if (this.combo.visible)
				this.combo.hide();
			else
				this.combo.show();
		}
	);
	Combo.addEvent(params.button,'mouseover',function(e){this.combo.inButton = true;});
	Combo.addEvent(params.button,'mouseout',function(e){this.combo.inButton = false;});
}

// event for registration
function __ka_ComboActiveClick(e)
{
	if (__ka_ComboActive != null && !__ka_ComboActive.isMouseIn && !__ka_ComboActive.inButton)
		__ka_ComboActive.hide();
}

// this will register events
if (window.addEventListener) window.addEventListener('click', __ka_ComboActiveClick, false);
else if (document.attachEvent) document.attachEvent('onclick', __ka_ComboActiveClick);
