function Code(_url, _label, _hasChildren) {
	this.url = _url;
	this.label = _label;
	this.hasChildren = _hasChildren;
	return this;
}

function getChildren(classyfication, code) {
	var params = new Array();
	params['GetChildrenDto_class'] = classyfication;
	params['GetChildrenDto_code'] = code;
	var jsCode = syncRequest('/index.php?module=main&kAction=getChildren', params);
	var data = new Array();
	eval(jsCode);	
	return data;
}

function collapse(obj, url) {
	obj.className = "tv_plus";
	obj.onclick = new Function("return expand(this);");
	var parent = obj.parentElement ? obj.parentElement : obj.parentNode;
	var nextDiv = getNextDiv(parent, obj);
	parent.removeChild(nextDiv);
	return false;
}

function getNextDiv(parent, child) {
	for (var i = 0; i < parent.childNodes.length; i++) {
		if (parent.childNodes[i] == child) {
			for (var j = i + 1; j < parent.childNodes.length; j++) {
				if (parent.childNodes[j].tagName == 'DIV') {
					return parent.childNodes[j];
				}
			}
		}
	}
	
	return null;
}

function expand(obj) {
	var block = true;
	try {
		var path = obj.href;	
		var params = path.match("drzewo_(.*)_(.*).html");	
		var children = getChildren(params[1], params[2]);
		obj.className = "tv_minus";
		obj.onclick = new Function("return collapse(this);");
		var div = document.createElement('div');
		div.className = "TreeView";
		div.style.marginLeft = '15px';
		var parent = obj.parentElement ? obj.parentElement : obj.parentNode;
		parent.insertBefore(div, obj.nextSibling);
		for (var i = 0; i < children.length; i++) {
			var code = children[i];
			var link = document.createElement('a');
			link.innerHTML = code.label;					
			if (code.hasChildren) {
				link.href = code.url;
				link.className = 'tv_plus';
				link.onclick = new Function("return expand(this);");
			} else {		
				if (i == children.length - 1) {
					link.className = 'tv_lastChild';
				} else {
					link.className = 'tv_child';
				}
			}
			div.appendChild(link);
		}
	} catch (e) {
		throw e;
		block = false;
	}
	return !block;
}
