var CaComboBox = (function()
{
	/* Declare private static variables here */
	function CaComboBoxConstructor(comboBoxId, xPos, yPos, width, height, backGroundImg, openUp, targetId)
	{
		var		target;
		var		div=document.createElement('div');
		var		dropDownDiv=document.createElement('div');
		var 	selectDiv=document.createElement('div');
		var		clickDiv=document.createElement('div');
		var		nodeID;
		var		style;


		if (document.getElementById(comboBoxId) != null)
		{
			alert('ComboBox \"'+comboBoxId+'\" already exists!');
			return;
		}

		if (typeof openUp == 'undefined')
			openUp = false;

		if (typeof targetId == 'undefined')
			target = document.getElementsByTagName('body')[0];
		else
			target = document.getElementById(targetId);

		// Membervariablen.
		this.id = comboBoxId;
		this.x = xPos;
		this.y = yPos;
		this.width = width;
		this.height = height;
		this.openUp = openUp;
		this.isOpen = false;

		this.selection = 0;
		this.options = new Array();

		// Methoden.
		this.addOption = CaComboBoxAddOption;
		this.openDropDown = CaComboBoxOpenDropDown;
		this.open = CaComboBoxOpen;
		this.toggle = CaComboBoxToggleOpen;
		this.updateSelection = CaComboBoxUpdateSelection;
		this.updateDropDown = CaComboBoxUpdateDropDown;

		// Initialisierung.
		nodeID = document.createAttribute('id');
		nodeID.nodeValue = comboBoxId + '_sel';
		selectDiv.setAttributeNode(nodeID);
		selectDiv.style.position = 'relative';
		selectDiv.style.width = this.width + 'px';
		selectDiv.style.height = '20px';
		selectDiv.onclick = this.toggle; // IE Fix!

		nodeID = document.createAttribute('id');
		nodeID.nodeValue = comboBoxId + '_dd';
		dropDownDiv.setAttributeNode(nodeID);
		dropDownDiv.style.position = 'absolute';
		dropDownDiv.style.left = '0px';
		dropDownDiv.style.top = ((this.openUp) ? 0 : 20) + 'px';
		dropDownDiv.style.width = (this.width-4) + 'px';
		//dropDownDiv.style.visibility = 'hidden';
		dropDownDiv.style.display = 'none';
		dropDownDiv.style.border = '2px solid black';

		nodeID = document.createAttribute('id');
		nodeID.nodeValue = comboBoxId + '_c';
		clickDiv.setAttributeNode(nodeID);
		clickDiv.style.position = 'absolute';
		clickDiv.style.left = '0px';
		clickDiv.style.top = '0px';
		clickDiv.style.width = this.width + 'px';
		clickDiv.style.height = this.height + 'px';
		clickDiv.onclick = this.toggle;

		nodeID = document.createAttribute('id');
		nodeID.nodeValue = comboBoxId;
		div.setAttributeNode(nodeID);
		div.appendChild(selectDiv);
		div.appendChild(dropDownDiv);
		div.appendChild(clickDiv);
		target.appendChild(div);

		div = document.getElementById(comboBoxId);

		style = div.style;
		style.position = 'absolute';
		style.left = this.x + 'px';
		style.top = this.y + 'px';
		style.width = this.width  + 'px';
		style.height = this.height  + 'px';
		style.background = 'url('+backGroundImg+')';
		style.cursor = 'pointer';

		CaComboBoxConstructor.cbArray[CaComboBoxConstructor.cbArray.length] = this;
	}

	CaComboBoxConstructor.cbArray = new Array();
	CaComboBoxConstructor.selectItem = CaComboBoxSelectItem;

	return CaComboBoxConstructor;
})();

function CaComboBoxAddOption(optionName, value)
{
	var			opt=new Object();


	opt.name = optionName;
	opt.value = value;

	this.options[this.options.length] = opt;

	if (this.openUp)
	{
		var			ddDiv=document.getElementById(this.id+'_dd');


		ddDiv.style.top = '-' + (this.options.length * 20) + 'px';
	}

//	this.updateDropDown();
}

function CaComboBoxToggleOpen()
{
	var			idArr=this.id.split('_');
	var			id=idArr[0];
	var			style=document.getElementById(id+'_dd').style;
	var			open=(style.display == 'none');
	var			other;
	var			i;


	if (open)
	{
		// Alle anderen CBs schließen.
		for (i=0;i<CaComboBox.cbArray.length;i++)
		{
			other = document.getElementById(CaComboBox.cbArray[i].id+'_dd').style;

			if (other != style)
				other.display = 'none';
		}
	}

	style.display = (open ? 'block' : 'none');
}

function CaComboBoxOpen(open)
{
	var			style=document.getElementById(this.id+'_dd').style;
	var			other;
	var			i;


	if (open)
	{
		// Alle anderen CBs schließen.
		for (i=0;i<CaComboBox.cbArray.length;i++)
		{
			other = document.getElementById(CaComboBox.cbArray[i].id+'_dd').style;

			if (other != style)
				other.display = 'none';
		}
	}

	style.display = (open ? 'block' : 'none');
}

function CaComboBoxOpenDropDown(blnAlwaysClose)
{
	var			style=document.getElementById(this.id+'_dd').style;
	var			other;
	var			i;


	for (i=0;i<CaComboBox.cbArray.length;i++)
	{
		other = document.getElementById(CaComboBox.cbArray[i].id+'_dd').style;

		if (other != style)
			other.display = 'none';
	}

	if (style.visibility == 'hidden')
		style.display = 'block';
	else
		style.display = 'none';

	if (typeof blnAlwaysClose != 'undefined' && typeof blnAlwaysClose == 'boolean')
		style.display = 'none';
}

function CaComboBoxUpdateSelection()
{
	var			div=document.getElementById(this.id+'_sel');
	var			strOut='';


	if (this.selection < this.options.length)
	{
		strOut += '<table border="0" cellspacing="0" cellpadding="0" height="100%" class="SelectActive">';
		strOut += '  <tr>';
		strOut += '    <td>';
		strOut += this.options[this.selection].name;
		strOut += '    </td>';
		strOut += '  </tr>';
		strOut += '</table>';
	}

	div.innerHTML = strOut;

	if (typeof this.onchange == 'string')
		eval(this.onchange);
}

function CaComboBoxUpdateDropDown()
{
	var			div=document.getElementById(this.id+'_dd');
	var			strOut='';
	var			optId='';
	var			i;
	var			width=(this.width-10);


	if (this.options.length > 19)
	{
		strOut += '<div style="width:'+(this.width-4)+'px;overflow-y:auto;overflow-x:none;height:300px;">';
		width -= 16;
	}

	strOut += '<table border="0" cellspacing="0" cellpadding="0" width="'+width+'" class="FilterTableBase" valign="top">';
	strOut += '  <tr>';
	strOut += '    <td valign="top">';
	strOut += '      <table border="0" cellspacing="0" cellpadding="0" style="margin:3px;" width="'+width+'">';

	for (i=0;i<this.options.length;i++)
	{
		optId = this.id + '_option_' + i;

		strOut += '        <tr class="SelectOff" id="'+optId+'">';
		strOut += '          <td width="15" height="18" align="center"><a href="#" onMouseOver="document.getElementById(\''+optId+'\').className = \'SelectOn\';" onMouseOut="document.getElementById(\''+optId+'\').className = \'SelectOff\';" onClick="CaComboBox.selectItem(\''+this.id+'\', '+i+');return false;"><div><img src="../Data/Img/Over/symbol_dot.gif"></div></a></td>';
		strOut += '          <td><a href="#" onMouseOver="document.getElementById(\''+optId+'\').className = \'SelectOn\';" onMouseOut="document.getElementById(\''+optId+'\').className = \'SelectOff\';" onClick="CaComboBox.selectItem(\''+this.id+'\', '+i+');return false;"><div style=width:'+(width-15)+'px;color:#000000;">' + this.options[i].name + '</div></a></td>';
		strOut += '        </tr>';
	}

	strOut += '      </table>';
	strOut += '    </td>';
	strOut += '  </tr>';
	strOut += '</table>';

	if (this.options.length > 19)
		strOut += '</div>';

	div.innerHTML = strOut;
}

function CaComboBoxSelectItem(filterId, filterIdx)
{
	var			filter=null;
	var			i;


	for (i=0;i<CaComboBox.cbArray.length;i++)
	{
		if (CaComboBox.cbArray[i].id == filterId)
		{
			filter = CaComboBox.cbArray[i];
			break;
		}
	}

	if (!filter)
	{
		alert('Unknown filter \"' + filterId + '\"');
		return;
	}

	if (filterIdx >= filter.options.length || filterIdx < 0)
	{
		alert('Invalid filter index \"' + filterIdx + '\"');
		return;
	}

	if (filter.selection != filterIdx)
	{
		var		changeFunc=filter.onchange;


		//filter.onchange = '';
		filter.selection = filterIdx;
		filter.updateSelection();
		filter.updateDropDown();
		//filter.onchange = changeFunc;
	}
	else
		filter.open(false);
}
