
function MDrawCompoTree (id) {
	this.m_elementId = id;
	this.m_nodes = new Array();
	this.m_opened = '';
	this.m_icons = new Array();
	this.m_iconsRef = new Array();
	this.m_translations = new Array();
	this.m_urlDynamic;
	this.m_dataPrefix;
	this.m_dynamicNodeText = new Array();
	this.m_className;
	this.m_lock = false;
	this.m_checked = new Array();
	this.m_idpIdxs = new Array();
	this.m_hiddenPrefix;
	
	this.start = MDrawCompoTree_start;
	this.config = MDrawCompoTree_config;
	this.addIcons = MDrawCompoTree_addIcons;
	this.onDynamicAction = MDrawCompoTree_onDynamicAction;
	this.onExpand = MDrawCompoTree_onExpand;
	this._onExpand = _MDrawCompoTree_onExpand;
	this.onSet = MDrawCompoTree_onSet;
	this.addNode = MDrawCompoTree_addNode;
	this.checkNode = MDrawCompoTree_checkNode;
	this.addCheck = MDrawCompoTree_addCheck;
	this.onCheck = MDrawCompoTree_onCheck;
	this.serializeCheck = MDrawCompoTree_serializeCheck;
	
	this.onSelInit = MDrawCompoTree_onSelInit;
	this.onFilterTextInit = MDrawCompoTree_onFilterTextInit;
	this.onHideInit = MDrawCompoTree_onHideInit;
	
	this.dynamicSetPreviousText = MDrawCompoTree_dynamicSetPreviousText;
	
	this.showHide = MDrawCompoTree_showHide;
	
	this._manipulateNode = _MDrawCompoTree_manipulateNode;
	this._getParentNode = _MDrawCompoTree_getParentNode;
	this._getWidth = _MDrawCompoTree_getWidth;
	
}


function MDrawCompoTree_onSelInit (id) {
	
	var ref = document.getElementById(this.m_elementId + 'ref');
	var obj = document.getElementById(id + 'sd');
	
	obj.style.position = 'absolute';
	ref.style.position = 'absolute';
	
	var left = ref.offsetLeft;
	var width = obj.offsetLeft - ref.offsetLeft;
	
	ref.style.position = '';
	
	if (width <= 0) {
		width = 0;
		obj.style.display = 'none';
	}
	
	obj.style.posLeft = left;
	obj.style.width = width;
	obj.style.height = document.getElementById(id).offsetHeight;
	
}

function MDrawCompoTree_onFilterTextInit () {
	
	var td = document.getElementById(this.m_elementId + 'ftd');
	var div = document.getElementById(this.m_elementId + 'fdiv');
	
	td.style.height = div.offsetHeight + 5;
}


function MDrawCompoTree_start () {
	this.m_opened = document.all[this.m_elementId].value;
}


function MDrawCompoTree_config (property, value) {

	switch (property) {
		case "MTC_OPEN_ICONS":
			this.m_openIcons = unserialize(unescape(value));
			break;
		case "MTC_ICONS_STORE":
			this.m_icons = unserialize(unescape(value));
			break;
		case "MTC_TRANSLATIONS_STORE":
			this.m_translations = unserialize(unescape(value));
			break;
		case 'MTC_URL_DYNAMIC':
			this.m_urlDynamic = value;
			break;
		case 'MTC_DATA_PREFIX':
			this.m_drawPrefix = value;
		break;
		case 'MTC_HIDDEN_PREFIX':
			this.m_hiddenPrefix = value;
		break;
		default:
		break;
	}
}


function MDrawCompoTree_addIcons (newIconsSerialized) {
	
	newIcons = unserialize(newIconsSerialized);
	
	for (var i in this.m_icons) {
		if (newIcons[this.m_icons[i]] != null) {
			this.m_iconsRef[newIcons[this.m_icons[i]]] = i;
			newIcons[this.m_icons[i]] = -1;
		}
	}
	
	for (var icon in newIcons)
		if (newIcons[icon] >= 0) {
			var len = this.m_icons.length;
			this.m_icons[len] = icon;
			this.m_iconsRef[newIcons[icon]] = len;
		}
}


function MDrawCompoTree_addNode (id, idParent, dynamic, opened, empty, iClosed, iOpened, tClosed, tOpened, noSelect, checked, radioFlag, noIconClickFlag) {
	
	this.m_nodes[id] = new Array();
	this.m_nodes[id]['p'] = idParent;
	this.m_nodes[id]['d'] = (dynamic ? true : false);
	this.m_nodes[id]['e'] = (empty ? true : false);
	this.m_nodes[id]['o'] = (opened ? true : false);
	
	if (iClosed || iClosed == 0)
		this.m_nodes[id]['ic'] = iClosed;
	if (!empty && (iOpened || iOpened == 0))
		this.m_nodes[id]['io'] = iOpened;
	if (tClosed)
		this.m_nodes[id]['tc'] = tClosed;
	if (!empty && tOpened)
		this.m_nodes[id]['to'] = tOpened;
	if (noSelect)
		this.m_nodes[id]['ns'] = 1;
	if (checked) {
		this.m_nodes[id]['chck'] = 1;
	}
	if (radioFlag) {
		this.m_nodes[id]['rd'] = 1;
	}
	if (noIconClickFlag) {
		this.m_nodes[id]['nic'] = 1;
	}
}


function MDrawCompoTree_dynamicSetPreviousText (idNode) {
	
}


function _MDrawCompoTree_manipulateNode (idNode, flag) {
	var tmpArr = unserialize(unescape(document.getElementById(this.m_hiddenPrefix + '[node]').value));
	tmpArr[idNode] = flag;
	document.getElementById(this.m_hiddenPrefix + '[node]').value = escape(serialize(tmpArr)); 
}


function MDrawCompoTree_onDynamicAction (id) {
	
	if (this.m_nodes[id]["d"] != null && this.m_nodes[id]["d"] == true) {
		this._manipulateNode(id, 1);
		document.all[this.m_elementId + 'dynamic'].value += ':' + id + ':';
	}
	
	if (!this.m_nodes[id]["ns"]) {
		document.getElementById(this.m_hiddenPrefix + '[sel_node]').value = id;
	}
}


function _MDrawCompoTree_onExpand (obj) {

	if (this.m_nodes[obj.id] != null && this.m_nodes[obj.id]["d"]) {
		
		this.m_lock = true;
		var objDiv = document.all['d' + obj.id];
		
		this.m_dynamicNodeText[obj.id] = objDiv.innerText;
		this.m_nodes[obj.id]["d"] = false;
		obj.style.listStyleImage = 'url(' + this.m_icons[this.m_nodes[obj.id]["io"]] + ')';
		
		objDiv.replaceAdjacentText('beforeEnd', this.m_translations['loading_mt']);
		
		var tmp_arr = this.m_urlDynamic.split('##');
		eval(tmp_arr[0] + obj.id + tmp_arr[1]);
		
		this.m_nodes[obj.id]["o"] = true;
		
		this._manipulateNode(obj.id, 1);
	}
}

MDrawCompoTree.prototype.onTextExpand = function (obj) {
	
	event.cancelBubble = true;
	
	this.m_lock = true;
	
	if (this.m_nodes[obj.id]["o"] != null && this.m_nodes[obj.id]["o"] == true) {
				
		obj.style.listStyleImage = 'url(' + this.m_icons[this.m_nodes[obj.id]["ic"]] + ')';
				
		document.all['c' + obj.id].style.display = 'none';
		document.all['c' + obj.id].runtimeStyle.display = 'none';
				
		this.m_nodes[obj.id]["o"] = false;
		if (this.m_nodes[obj.id]["to"] != null) {
			obj.childNodes[0].innerText = ' ' +  this.m_nodes[obj.id]["t"];
		}
				
		this._manipulateNode(obj.id, 0);
	} else {
				
		obj.style.listStyleImage = 'url(' + this.m_icons[this.m_nodes[obj.id]["io"]] + ')';
				
		document.all['c' + obj.id].style.display = '';
		document.all['c' + obj.id].runtimeStyle.display = '';
				
		this.m_nodes[obj.id]["o"] = true;
		if (this.m_nodes[obj.id]["to"] != null) {
			obj.childNodes[0].innerText = ' ' +  this.m_nodes[obj.id]["to"];
		}
		
		this._manipulateNode(obj.id, 1);
	}
	
	this.m_lock = false;
}


function MDrawCompoTree_onExpand () {

	var obj = event.srcElement;
	event.cancelBubble = true;
	
	if (obj.tagName == 'LI') {
		
		if (this.m_lock || this.m_nodes[obj.id]["nic"]) {
			return;
		}
		this.m_lock = true;
		
		if (this.m_nodes[obj.id]["d"]) {
			this._onExpand (obj);
		} else if (!this.m_nodes[obj.id]["e"]) {
			
			if (this.m_nodes[obj.id]["o"] != null && this.m_nodes[obj.id]["o"] == true) {
				
				obj.style.listStyleImage = 'url(' + this.m_icons[this.m_nodes[obj.id]["ic"]] + ')';
				
				document.all['c' + obj.id].style.display = 'none';
				document.all['c' + obj.id].runtimeStyle.display = 'none';
				
				this.m_nodes[obj.id]["o"] = false;
				if (this.m_nodes[obj.id]["to"] != null) {
					obj.childNodes[0].innerText = ' ' +  this.m_nodes[obj.id]["t"];
				}
				
				this._manipulateNode(obj.id, 0);
			} else {
				
				obj.style.listStyleImage = 'url(' + this.m_icons[this.m_nodes[obj.id]["io"]] + ')';
				
				document.all['c' + obj.id].style.display = '';
				document.all['c' + obj.id].runtimeStyle.display = '';
				
				this.m_nodes[obj.id]["o"] = true;
				if (this.m_nodes[obj.id]["to"] != null) {
					obj.childNodes[0].innerText = ' ' +  this.m_nodes[obj.id]["to"];
				}
				
				this._manipulateNode(obj.id, 1);
			}
			
			
			this.m_lock = false;
		} else
			this.m_lock = false;
	}
}


function MDrawCompoTree_onSet () {
	var parent = this._getParentNode(event.srcElement, 'li');
	var id_node = parent.id;
	
	if (!this.m_nodes[id_node]["ns"]) {
		document.getElementById(this.m_hiddenPrefix + '[sel_node]').value = event.srcElement.id.substring(1);
	}
}


function MDrawCompoTree_showHide (obj) {
	
	var dTree = document.all[this.m_elementId + 'td'];
	var closedHidden = document.all[this.m_elementId + 'tc'];
	var widthHidden = document.all[this.m_elementId + 'tcw'];
	
	var objResized = this._getParentNode(dTree.parentNode, 'td');
	
	if (obj.tagName == "TD") {
		obj = obj.childNodes[0];
	}
	
	if (obj.src == this.m_openIcons["close"]) {
		var objRef = this._getParentNode(obj, 'td');
		
		closedHidden.value = this._getWidth(objResized);
		objResized.style.width = objRef.offsetWidth;
		
		widthHidden.value = document.all[this.m_elementId + 'tbl'].style.width;
		
		document.all[this.m_elementId + 'tbl'].style.width = '';
		
		dTree.style.display = 'none';
		obj.src = this.m_openIcons["open"];
		obj.alt = this.m_openIcons["open_alt"];
		
	} else {
		dTree.style.display = '';

		document.all[this.m_elementId + 'tbl'].style.width = widthHidden.value;
		
		objResized.style.width = closedHidden.value;
		closedHidden.value = 0;
		
		obj.src = this.m_openIcons["close"];
		obj.alt = this.m_openIcons["close_alt"];
	}
	
	event.cancelBubble = true;
}


function MDrawCompoTree_onHideInit (obj, objImg) {
	
	var objResized = this._getParentNode(obj.parentNode, 'td');
	var objRef = this._getParentNode(objImg, 'td');
	var closedHidden = document.all[this.m_elementId + 'tc'];
	
	document.all[this.m_elementId + 'tcw'].value = document.all[this.m_elementId + 'tbl'].style.width;
	
	document.all[this.m_elementId + 'tbl'].style.width = '';
	
	closedHidden.value = objResized.offsetWidth;
	objResized.style.width = objRef.offsetWidth;
	
	obj.style.display = 'none';
}


function _MDrawCompoTree_getParentNode (parent, tagName) {
	
	while (parent.tagName.toLowerCase() != tagName) {
		parent = parent.parentNode;
	}
	return parent;
}


function _MDrawCompoTree_getWidth (element) {
	
	var width = 0;
	if (element.tagName) {
		var curStyle = element.currentStyle;
		
		width += element.offsetWidth;
		width -= (isNaN (parseInt (curStyle.paddingLeft))) ? 0 : parseInt (curStyle.paddingLeft);
		width -= (isNaN (parseInt (curStyle.paddingRight))) ? 0 : parseInt (curStyle.paddingRight);
		width -= (isNaN (parseInt (curStyle.borderLeftWidth))) ? 0 : parseInt (curStyle.borderLeftWidth);
		width -= (isNaN (parseInt (curStyle.borderRightWidth))) ? 0 : parseInt (curStyle.borderRightWidth);
	}
	return width;
}


function MDrawCompoTree_checkNode (idNode, idParentNode, value, setFlag, depth, idpIdxs) {

	value = (value ? 1 : 0);
	var tmpArr = idParentNode.split('_');
	var pidx = tmpArr[2];
	tmpArr = idNode.split('_');
	
	if (!idpIdxs[tmpArr[1]]) {
		idpIdxs[tmpArr[1]] = new Array();
	}
	if (!idpIdxs[tmpArr[1]][pidx]) {
		idpIdxs[tmpArr[1]][pidx] = new Array();
	}
	
	idpIdxs[tmpArr[1]][pidx][tmpArr[2]] = (value ? true : false);
	this.m_nodes[idNode]['chck'] = value;
	
	if (setFlag) {
		var obj = document.all['d' + idNode];
		i = 0;
		while (obj.childNodes[i].tagName != 'INPUT') {
			i++;
		}
		obj.childNodes[i].checked = value
	} 
	
	if (depth != 0 && this.m_checked[idNode] != null) {
		for (var id in this.m_checked[idNode]) {
			if (!this.m_nodes[id]['rd']) {
				idpIdxs = this.checkNode (id, idNode, value, true, depth - 1, idpIdxs);
			}
		}
	}
	return idpIdxs;
}


function MDrawCompoTree_addCheck (idNode, idParentNode, idp, value) {

	if (!idParentNode)
		idParentNode = "__-1__";
	
	var tmpArr = idParentNode.split('_');
	var pidx = tmpArr[2];
	var tmpArr = idNode.split('_');
	
	if (this.m_checked[idParentNode] == null) {
		this.m_checked[idParentNode] = new Array();
	}

	this.m_checked[idParentNode][idNode] = idp;

	if (!this.m_idpIdxs[idp]) {
		this.m_idpIdxs[idp] = new Array();
	}		

	if (!this.m_idpIdxs[idp][pidx]) {
		this.m_idpIdxs[idp][pidx] = new Array();
	}		

	this.m_idpIdxs[idp][pidx][tmpArr[2]] = (value ? true : false);
}


function MDrawCompoTree_onCheck (idParentNode) {

	var parent = this._getParentNode(event.srcElement, 'li');
	if (!idParentNode)
		idParentNode = "__-1__";

	if (this.m_nodes[parent.id]['rd']) {
		if (this.m_nodes[parent.id]['chck']) {
			event.srcElement.checked = false;
		} else {
			for (var id in this.m_checked[idParentNode]) {
				if (id != parent.id) {
					this.m_nodes[id]['chck'] = 0;
					this.m_idpIdxs = this.checkNode (id, idParentNode, false, false, -1, this.m_idpIdxs);
				}	
			}
		}
	}
	
	this.m_idpIdxs = this.checkNode (parent.id, idParentNode, event.srcElement.checked, false, -1, this.m_idpIdxs);
	
	this.serializeCheck();
}


function MDrawCompoTree_serializeCheck () {
	
	for (var idp in this.m_idpIdxs) {
		if (document.all[this.m_drawPrefix + '[' + idp + ']'] == null) {
			document.all[this.m_elementId + 'tbl'].insertAdjacentHTML('afterEnd', '<INPUT type="hidden" name="' + this.m_drawPrefix + '[' + idp + ']" value="">');
		}
		document.all[this.m_drawPrefix + '[' + idp + ']'].value = escape(serialize(this.m_idpIdxs[idp]));
	}
}

