var DescendandSelector = function(box_id, name, container)
{
	this.box_id = box_id;
	this.box = $(box_id);
	this.name = name;
	this.container = container;
	this.el_cnt = 1;
	this.selected_id = 0;
	this.prefix = 'selector_' + box_id + '_';
}

DescendandSelector.prototype.class_name = 'descendant_selector';

DescendandSelector.prototype.addSelect = function()
{
	var div = document.createElement('div');
	div.id = this.prefix + this.el_cnt;
	div.className = this.class_name;
	var select_el = document.createElement('select');
	select_el.name = this.prefix + this.el_cnt;
	var self = this;
	select_el.onchange = function () { self.change(select_el); }
	this.populate(select_el);
	div.appendChild(document.createTextNode(' \xA0>>\xA0 ')); // A0 is "&nbsp;"
	div.appendChild(select_el);
	this.box.appendChild(div);
	++this.el_cnt;
}

DescendandSelector.prototype.populate = function(el)
{	
	var opt, pos;
	opt = new Option(' ', 0, true);
	selAdd(opt, el, null);
	for (var i = 0; i < this.container['id_' + this.selected_id].length; ++i) {
		pos = arraySearch(this.container['id_' + this.selected_id][i], this.container.ids);
		opt = new Option(this.container.names[pos], this.container['id_' + this.selected_id][i]);
		selAdd(opt, el, null);
	}		
}

DescendandSelector.prototype.change = function(el)
{
	this.selected_id = el[el.selectedIndex].value;
	var el_num;
	if (!(el_num = parseInt(el.parentNode.id.substring(this.prefix.length))))
		el_num = 0;
	this.delSelects(++el_num);
	this.el_cnt = el_num;
	var value = parseInt(el.value);
	if (!value) {
		var el;
		if (this.el_cnt > 2) {
			el = $(this.prefix + (this.el_cnt - 1)).getElementsByTagName('select')[0];
		}
		else {
			el = this.box.getElementsByTagName('select')[0];
		}
		value = parseInt(el[el.selectedIndex].value);
		if (isNaN(value))
			value = '';
	}
    try {
        $(this.name).value = value;
    }
    catch (e) {
        alert('Error: can\'t find field "' + this.name + '"');
	}
	if (!this.selected_id || typeof(this.container['id_' + this.selected_id]) == 'undefined')
		return false;
	this.addSelect();	
}

DescendandSelector.prototype.delSelects = function(first_num)
{
	var el;
	for (var i = first_num; i < this.el_cnt; ++i) {
		if (el = $(this.prefix + i))
			this.box.removeChild(el);
	}
}

DescendandSelector.prototype.expand = function(el, id)
{
	this.el_cnt = 1;
	this.delSelects(1);
	$(this.name).value = id;
	var num;
	var ids_chain = [id];
	var founded = false;
	var proccesedProperties = [];
	do {
		founded = false;
		for (var prop in this.container) {
			num = prop.substring(3); // cut off "id_"
			if (!num || !(num = parseInt(num)) || arraySearch(num, proccesedProperties) != -1)
				continue;
			if (arraySearch(id, this.container[prop]) != -1) {
				ids_chain.unshift(num);
				founded = true;
				id = num;
				proccesedProperties.push(num);
				break;
			}
		}
	}
	while (founded);
	for (var i = 0; i < ids_chain.length; ++i) {
		if (!i) {
			this.selectValue(el, ids_chain[i]);
			this.change(el);
		}
		else {
			this.selectValue($(this.prefix + i).getElementsByTagName('select')[0], ids_chain[i]);
			this.change($(this.prefix + i).getElementsByTagName('select')[0]);
		}
	}		
}

DescendandSelector.prototype.selectValue = function(el, id)
{
	for (var i = 0; el.options.length; ++i) {
		if (el.options[i].value == id) {
			el.selectedIndex = i;
			break;
		}
	}
}
