/**
 * Pages tree scripts
 *
 * @copyright (c) 2004-2006, SWsoft
 */

var dragControl;
var editingNode;

dragControl = new DragControl();
editingNode = null;

function _onNodeEvent(node, method, event) {
	if (null != node) {
		if (node.isEnabled) {
			if (document.all) {
				event = window.event;
			}
			var result;
			eval('if (node.' + method + ') { result = node.' + method + '(event); }');
			return result;
		}
	}
}

function _onSbTreeEvent(tree, method, event) {
	if (null != tree) {
		if (document.all) {
			event = window.event;
		}
		var result;
		eval('if (tree.' + method + ') { result = tree.' + method + '(event); }');
		return result;
	}
}

function _onMouseMove(event) {
	dragControl.move(event);
}

function _onMouseUp(event) {
	dragControl.end(event);
}

function SbInitTree(treeId, isStatic, withLines, draggable, checkable, selectable, onSelect, onChange, onCheck, onAfterSelect) {
	var object;

	if (window.sbPagesContainer == null) {
		window.sbPagesContainer = new Array();
	}
	object = document.getElementById(treeId);
	SbTree(object, treeId, isStatic, withLines, draggable, checkable, selectable, onSelect, onChange, onCheck, onAfterSelect);
	window.sbPagesContainer[treeId] = object;
	if (object.isDragable) {
		window.ondragdrop = stopEvent;
		window.ondragstart = stopEvent;
		document.body.ondragstart = function () { return false; };
		object.onselectstart = function () { return false; };
		if (!document.all) {
			object.style.MozUserSelect = 'none';
		}
	}
}

function SbRefreshTree(treeId) {
	window.sbPagesContainer[treeId].refresh();
}

function SbPanelTreeChecksToggle(checkBoxId, treeId, toggleCheckbox) {
	var checkbox = document.getElementById(checkBoxId);
	if (checkbox) {
		if (toggleCheckbox == checkbox.checked) {
			window.sbPagesContainer[treeId].unCheckAllNodes();
		} else {
			window.sbPagesContainer[treeId].checkAllNodes();
		}
	}
}

function SbGetTreeXml(treeId) {
	return window.sbPagesContainer[treeId].converToXml();
}

function SbInitNode(treeId, nodeId, fName, type, selected, disabled, status) {
	SbTreeNode(window.sbPagesContainer[treeId], nodeId, fName, type, selected, disabled, status);
}

function DragControl() {
	this.entity = null;
	this.entityRoot = null;
	this.target = null;
	this.targetRoot = null;
	this.isInitStarted = false;
	this.isInited = false;
	this.isEndInited = false;
	this.isDragging = false;
	this.isEntityCloned = false;
	this.preparedNode = null;
	this.oldTree = null;
	this.oldParent = null;
	this.oldBefore = null;
	this.oldZIndex = null;

	this.beginX = 0;
	this.beginY = 0;

	addEvent(document, 'mouseup', _onMouseUp);
}

DragControl.prototype.resetAll = function() {
	this.entity = null;
	this.entityRoot = null;
	this.target = null;
	this.targetRoot = null;
	this.isInitStarted = false;
	this.isInited = false;
	this.isEndInited = false;
	this.isDragging = false;
	this.isEntityCloned = false;
	this.preparedNode = null;
	this.oldTree = null;
	this.oldParent = null;
	this.oldBefore = null;
	this.oldZIndex = null;

	this.beginX = 0;
	this.beginY = 0;
}

DragControl.prototype.prepareDrag = function(node, event) {
	if (null == editingNode) {
		if (!this.isInited) {
			this.isInitStarted = true;
			this.preparedNode = node;
			this.beginX = getScreenX(event);
			this.beginY = getScreenY(event);
			window.setTimeout(function () { dragControl.Init() }, 100);
		}
	}
}

DragControl.prototype.Init = function() {
	if (this.isInited || !this.isInitStarted || (null == this.preparedNode)) {
		this.resetAll();
	} else {
		addEvent(document, 'mousemove', _onMouseMove);

		this.isInited = true;
		this.isInitStarted = false;
	}
}

DragControl.prototype.start = function() {
	this.entityRoot = this.preparedNode.tree;
	this.oldTree = this.preparedNode.tree;
	this.oldParent = this.preparedNode.parentNode;
	this.oldBefore = this.preparedNode.tree.getNextNode(this.preparedNode);
	this.oldZIndex = this.preparedNode.style.zIndex;
	if (this.entityRoot.isStatic) {
		this.entity = this.preparedNode.cloneSbTreeNode();
		this.isEntityCloned = true;
	} else {
		this.entityRoot.unRegisterNode(this.preparedNode);
		this.entity = this.preparedNode.parentNode.removeChild(this.preparedNode);
		this.entity.tree = null;
		this.entityRoot.refresh();
	}
	document.body.appendChild(this.entity);
	this.entity.prepareForMoving();
	var childList = this.entity.getChildList();
	for(var i = 0; i < childList.length; i++) {
		childList[i].prepareForMoving(true);
	}
	this.entity.style.position = 'absolute';
	if (document.all) {
		this.entity.style.filter = "alpha(opacity='60')";
	} else {
		this.entity.style.MozOpacity = '0.60';
	}
	this.setPosition(this.beginX, this.beginY);
	this.entity.style.zIndex = 999;

	this.preparedNode = null;

	this.isInited = true;
	this.isDragging = true;
}

DragControl.prototype.insertChild = function() {
	var firstChild = null;
	var parent = null;
	if (this.target == null) {
		parent = this.targetRoot;
	} else {
		if (this.targetRoot.getLevel(this.target) == 2) {
			if (this.entity.hasChilds()) {
				parent = this.targetRoot;
				firstChild = this.targetRoot.getNextNode(this.target.parentNode);
			} else {
				parent = this.target.parentNode;
				firstChild = this.targetRoot.getNextNode(this.target);
			}
		} else {
			if (this.entity.hasChilds()) {
				parent = this.targetRoot;
				firstChild = parent.getNextNode(this.target);
			} else {
				parent = this.target;
				var list = this.target.getChildList();
				if (list.length > 0) {
					firstChild = list[0];
				}
			}
		}
	}
	return this.targetRoot.insertTreeNode(this.entity, parent, firstChild);
}

DragControl.prototype.end = function(event) {
	if (!this.isEndInited) {
		if (this.isDragging) {
			this.isEndInited = true;
			if (this.isEntityCloned) {
				this.entity = document.body.removeChild(this.entity);
				if (!((null == this.targetRoot) || this.targetRoot.isStatic)) {
					if (this.insertChild()) {
						this.restore();
					}
					this.entity.unCheck();
				}
			} else {
				if (null == this.targetRoot) {
					if (this.target == null) {
						this.targetRoot = this.oldTree;
						if (null == this.oldBefore) {
							this.oldParent.appendChild(this.entity);
						} else {
							this.oldParent.insertBefore(this.entity, this.oldBefore);
						}
						this.oldParent.tree.registerNode(this.entity);
					}
					this.restore();
				} else {
					if (this.targetRoot.isStatic) {
						if (null != this.oldTree) {
							this.oldTree.removeTreeNode(this.entity);
						}
						this.entity = document.body.removeChild(this.entity);
					} else {
						if ('PageModule' == this.entity.type) {
							this.targetRoot = this.oldTree;
							if (null == this.oldBefore) {
								this.oldParent.appendChild(this.entity);
							} else {
								this.oldParent.insertBefore(this.entity, this.oldBefore);
							}
							this.oldParent.tree.registerNode(this.entity);
							this.restore();
						} else {
							this.entity = document.body.removeChild(this.entity);
							if (this.insertChild()) {
								this.restore();
							}
						}
					}
				}
			}
		}
		if (null != this.targetRoot) {
			this.unRegisterTargetRoot(this.targetRoot);
		}
		document.body.style.cursor = 'default';
		removeEvent(document, 'mousemove', _onMouseMove);
		this.resetAll();
	}
}

DragControl.prototype.restore = function() {
	this.entity.tree = this.targetRoot;
	this.entity.restoreAfterMoving((this.entity.tree.getLevel(this.entity) == 2));
	var childList = this.entity.getChildList();
	for(var i = 0; i < childList.length; i++) {
		childList[i].tree = this.targetRoot;
		childList[i].restoreAfterMoving(true);
	}
	this.entity.style.zIndex = this.oldZIndex;
	this.entity.style.left = null;
	this.entity.style.top = null;
	this.entity.style.position = 'static';
	this.entity.tree.refresh();
	if (document.all) {
		this.entity.style.filter = '';
	} else {
		this.entity.style.MozOpacity = '';
	}
	this.entity.style.cursor = 'default';
}

DragControl.prototype.setPosition = function(x, y) {
  this.entity.style.left = x + 8 + 'px';
  this.entity.style.top = y + 4 + 'px';
}

DragControl.prototype.move = function(event) {
	if (this.isInited) {
		var x = getScreenX(event);
		var y = getScreenY(event);
		if (this.isDragging) {
			this.setPosition(x, y);
			if (this.target == this.entity) {
				this.target = null;
				this.targetRoot = null;
			}
			if (null == this.target) {
				document.body.style.cursor = 'not-allowed';
				this.entity.style.cursor = 'not-allowed';
			} else {
				document.body.style.cursor = 'pointer';
				this.entity.style.cursor = 'pointer';
			}
		} else {
			if((x < this.beginX - 5) || (x > this.beginX + 5) || (y < this.beginY - 5) || (y > this.beginY + 5)) {
				this.start();
				this.setPosition(x, y);
			}
		}
	}
	
	stopEvent(event);
  }

DragControl.prototype.registerTarget = function(tree, node) {
	if (this.isDragging) {
		this.target = node;
		this.targetRoot = tree;
		if (this.targetRoot.isStatic && this.oldTree.isStatic) {
			this.target.textObject.style.cursor = 'not-allowed';
			this.target.iconObject.style.cursor = 'not-allowed';
			this.target = null;
			this.targetRoot = null;
		} else {
			this.target.style.cursor = 'default';
		}
	}
}

DragControl.prototype.unRegisterTarget = function(tree, node) {
	if (this.isDragging) {
		node.textObject.style.cursor = 'pointer';
		node.iconObject.style.cursor = 'pointer';
		this.target = null;
		this.targetRoot = null;
	}
}

DragControl.prototype.registerTargetRoot = function(tree) {
	if (this.isDragging) {
		this.targetRoot = tree;
		if (this.targetRoot.isStatic && 
			(this.oldTree.isStatic)) {
			this.targetRoot.style.cursor = 'not-allowed';
		} else {
			this.targetRoot.style.cursor = 'default';
		}
	}
}

DragControl.prototype.unRegisterTargetRoot = function(tree) {
	if (this.isDragging) {
		this.targetRoot = tree;
		this.targetRoot.style.cursor = 'default';
		this.targetRoot = null;
	}
}


function SbTree(treeObject, name, isStatic, withLines, draggable, checkable, selectable, onSelect, onChange, onCheck, onAfterSelect) {
	treeObject.identifier = name;
	treeObject.isInited = false;
	treeObject.isStatic = (isStatic.toLowerCase() == 'true');
	treeObject.withLines = (withLines.toLowerCase() == 'true');
	treeObject.isDragable = (draggable.toLowerCase() == 'true');
	treeObject.isCheckable = (checkable.toLowerCase() == 'true');
	treeObject.isSelectable = (selectable.toLowerCase() == 'true');
	treeObject.isRenamable = true;
	treeObject.nodesList = new Array();
	treeObject.selectedNode = null;
	treeObject.focusNode = null;
	treeObject.maxRootPages = -1;
	treeObject.maxPages = -1;
	treeObject.maxLevel = 2;
	treeObject.tree = treeObject;

	treeObject.onSelect = onSelect;
	treeObject.onAfterSelect = onAfterSelect;
	treeObject.onChange = onChange;
	treeObject.onCheck = onCheck;

	treeObject.getChildList = SbTreeNode.getChildList;
	treeObject.hasChilds = SbTreeNode.hasChilds;
	
	treeObject.addListeners = SbTree.addListeners;
	treeObject.onResize = SbTree.onResize;
	treeObject.setMaxLevel = SbTree.setMaxLevel;
	treeObject.setMaxPages = SbTree.setMaxPages;
	treeObject.setMaxRootPages = SbTree.setMaxRootPages;
	treeObject.registerNode = SbTree.registerNode;
	treeObject.unRegisterNode = SbTree.unRegisterNode;
	treeObject.registerChange = SbTree.registerChange;
	treeObject.checkAllNodes = SbTree.checkAllNodes;
	treeObject.unCheckAllNodes = SbTree.unCheckAllNodes;
	treeObject.selectNode = SbTree.selectNode;
	treeObject.forceSelectNode = SbTree.forceSelectNode;
	treeObject.unSelect = SbTree.unSelect;
	treeObject.registerFocusNode = SbTree.registerFocusNode;
	treeObject.unRegisterFocusNode = SbTree.unRegisterFocusNode;
	treeObject.drawLines = SbTree.drawLines;
	treeObject.drawNodeLines = SbTree.drawNodeLines;
	treeObject.getLevel = SbTree.getLevel;
	treeObject.getFirstNode = SbTree.getFirstNode;
	treeObject.getNextNode = SbTree.getNextNode;
	treeObject.getPreviousNode = SbTree.getPreviousNode;
	treeObject.updateLinesFirstLevelNode = SbTree.updateLinesFirstLevelNode;
	treeObject.refresh = SbTree.refresh;
	treeObject.mouseover = SbTree.mouseover;
	treeObject.mouseout = SbTree.mouseout;
	treeObject.converToXml = SbTree.converToXml;
	treeObject.insertTreeNode = SbTree.insertTreeNode;
	treeObject.removeTreeNode = SbTree.removeTreeNode;
	treeObject.getAllNodes = SbTree.getAllNodes;
	treeObject.getCheckedNodes = SbTree.getCheckedNodes;
	treeObject.isSomethingChecked = SbTree.isSomethingChecked;
	treeObject.isSomethingNotChecked = SbTree.isSomethingNotChecked;

	treeObject.addListeners();
}

SbTree.addListeners = function() {
	var treeObject = this;	
	this.onmouseover = function(event) { _onSbTreeEvent(treeObject, 'mouseover', event); };
	this.onmouseout = function(event) { _onSbTreeEvent(treeObject, 'mouseout', event); };
	if (('site' == this.identifier) || ('edit' == this.identifier)) {
		setObjectHeight(this, 70);
		addEvent(window, 'load', function(event) { treeObject.onResize(); });
		addEvent(window, 'resize', function(event) { treeObject.onResize(); });
	}
}

SbTree.onResize = function() {
	var height = getObjectHeight(this.parentNode);
	setObjectHeight(this, height);
}

SbTree.setMaxPages = function(value) {
	this.maxPages = value;
}

SbTree.setMaxRootPages = function(value) {
	this.maxRootPages = value;
}

SbTree.setMaxLevel = function(value) {
	this.maxLevel = value;
}

SbTree.isSomethingNotChecked = function() {
	var result = false;
	for (var i = 0; i < this.nodesList.length; i++) {
		if (this.nodesList[i].isEnabled && !this.nodesList[i].isChecked) {
			result = true;
			break;
		}
	}
	return result;
}

SbTree.isSomethingChecked = function() {
	var result = false;
	for (var i = 0; i < this.nodesList.length; i++) {
		if (this.nodesList[i].isChecked) {
			result = true;
			break;
		}
	}
	return result;
}

SbTree.getCheckedNodes = function(parent) {
	var result = new Array();
	if (null == parent) {
		parent = this;
	}
	var list = parent.getChildList();
	for (var i = 0; i < list.length; i++) {
		if (list[i].isChecked) {
			result[result.length] = list[i];
		}
		if (list[i].hasChilds()) {
			result = result.concat(this.getCheckedNodes(list[i]));
		}
	}
	return result;
}

SbTree.getAllNodes = function() {
	return this.nodesList;
}

SbTree.getFirstNode = function() {
	var list = this.getChildList();
	return (list.length > 0) ? list[0] : null;
}

SbTree.insertTreeNode = function(node, parent, firstChild) {
	if (parent != this) {
		if (1 == this.maxLevel) {
			firstChild = this.getNextNode(parent);
			parent = this;
		} else {
			if (('Simple' == parent.type) || ('Guestbook' == parent.type)) {
				switch(node.type){
					case 'Eshop':
					case 'PageModule':
					case 'Blogger':
					case 'ImageGallery':
					case 'Forum':
					case 'Login':
						firstChild = this.getNextNode(parent);
						parent = this;
						break;
					default:
						parent.expand();
						parent.stateObject.className = 'TreeNodeState';
				}
			} else {
				if ('FlashIntro' != node.type) {
					firstChild = this.getNextNode(parent);
					parent = this;
				}
			}
		}
	}
	if ('FlashIntro' == node.type) {
		parent = this;
		firstChild = this.getFirstNode();
	}
	if (parent == this) {
		var childList = this.getChildList();
		if ((this.maxRootPages > 0) && (childList.length >= this.maxRootPages)) {
			alert(SbGetLocaleByKey('rootPagesNumberExceed'));
			return false;
		}
	}
	var childList = node.getChildList();
	if ((this.maxPages > 0) && ((childList.length + this.nodesList.length) >= this.maxPages)) {
		alert(SbGetLocaleByKey('pagesNumberExceed'));
		return false;
	}
	if (firstChild == null) {
		parent.appendChild(node);
	} else {
		parent.insertBefore(node, firstChild);
	}
	this.registerNode(node);
	if (node.hasChilds()) {
		var childList = node.getChildList();
		for(var i = 0; i < childList.length; i++) {
			this.registerNode(childList[i]);
		}
		if (1 == this.maxLevel) {
			for(var i = 0; i < childList.length; i++) {
				var child = node.removeChild(childList[i]);
				if (null == firstChild) {
					this.appendChild(child);
				} else {
					this.insertBefore(child, firstChild);
				}
				child.unCheck();
				child.restoreAfterMoving();
			}
		}
		this.refresh();
	}
	this.registerChange();
	return true;
}

SbTree.removeTreeNode = function(node) {
	this.unRegisterNode(node);
	this.registerChange();
}

SbTree.converToXml = function() {
	var result = '';
	for(var i = 0; i < this.childNodes.length; i++) {
		if(this.childNodes[i].tagName == 'DIV') {
			if (this.childNodes[i].identifier) {
				result += this.childNodes[i].converToXml();
			}
		}
	}
	return result;
}

SbTree.refresh = function() {
log (this.withLines, this.nodesList.length);
	if (this.withLines && (this.nodesList.length > 1)) {
		this.drawLines();
	} else {
		for(var i = 0; i < this.nodesList.length; i++) {
			this.updateLinesFirstLevelNode(this.nodesList[i], '/images/tree/expanded.gif', '/images/tree/collapsed.gif', '/images/blank.gif');
		}
	}
	this.isInited = true;
}

SbTree.updateLinesFirstLevelNode = function(node, expandedImage, collapsedImage, simpleImage) {
	if (node.hasChilds()) {
		if (node.isExpanded) {
			node.stateObject.src = expandedImage;
		} else {
			node.stateObject.src = collapsedImage;
		}
	} else {
		node.stateObject.src = simpleImage;
	}
}

SbTree.drawLines = function() {
	for(var i = 0; i < this.nodesList.length; i++) {
		this.drawNodeLines(this.nodesList[i]);
	}
}

SbTree.drawNodeLines = function(node) {
	if (this.getLevel(node) == 1) {
		if (null == this.getPreviousNode(node)) {
			if (null == this.getNextNode(node)) {
				this.updateLinesFirstLevelNode(node, '/images/tree/expanded.gif', '/images/tree/collapsed.gif', '/images/blank.gif');
			} else {
				this.updateLinesFirstLevelNode(node, '/images/tree/topExpanded.gif', '/images/tree/topCollapsed.gif', '/images/tree/top.gif');
			}
		} else {
			if (null == this.getNextNode(node)) {
				this.updateLinesFirstLevelNode(node, '/images/tree/bottomExpanded.gif', '/images/tree/bottomCollapsed.gif', '/images/tree/bottom.gif');
			} else {
				this.updateLinesFirstLevelNode(node, '/images/tree/middleExpanded.gif', '/images/tree/middleCollapsed.gif', '/images/tree/middleNext.gif');
			}
		}
	} else {
		if (null == this.getNextNode(node.parentNode)) {
			node.stateObject.src = '/images/blank.gif';
		} else {
			node.stateObject.src = '/images/tree/middle.gif';
		}
		if (null == this.getNextNode(node)) {
			node.lineObject.src = '/images/tree/bottom.gif';
		} else {
			node.lineObject.src = '/images/tree/middleNext.gif';
		}
	}
}

SbTree.getPreviousNode = function(node) {
	var result = null;
	var list = node.parentNode.getChildList();

	for(var i = 0; i < list.length; i++) {
		if (node == list[i]) {
			break;
		} else {
			result = list[i];
		}
	}
	return result;
}

SbTree.getNextNode = function(node) {
	var result = null;
	var list = node.parentNode.getChildList();

	for(var i = 0; i < list.length; i++) {
		if ((node == list[i]) && ((i + 1) < list.length)) {
			result = list[i + 1];
			break;
		}
	}
	return result;
}

SbTree.getLevel = function(node) {
	var result = 1;
	if (node.parentNode.parentNode == this) {
		result = 2;
	}
	return result;
}

SbTree.registerNode = function(node) {
	this.nodesList[this.nodesList.length] = node;
	node.tree = this;
}

SbTree.unRegisterNode = function(node) {
	var result = new Array();
	for (var i = 0; i < this.nodesList.length; i++) {
		if (node != this.nodesList[i]) {
			result[result.length] = this.nodesList[i];
		}
	}
	this.nodesList = result;
	var list = node.getChildList();
	for(var i = 0; i < list.length; i++) {
		this.unRegisterNode(list[i]);
	}
}

SbTree.registerChange = function() {
	if (this.onChange) {
		this.onChange();
	}
}

SbTree.checkAllNodes = function() {
	for(var i = 0; i < this.nodesList.length; i++) {
		this.nodesList[i].check();
	}
	if (this.onCheck) {
		this.onCheck();
	}
}

SbTree.unCheckAllNodes = function() {
	for(var i = 0; i < this.nodesList.length; i++) {
		this.nodesList[i].unCheck();
	}
	if (this.onCheck) {
		this.onCheck();
	}
}

SbTree.selectNode = function(node) {
	var result = true;
	if (this.isInited && this.onSelect) {
		result = this.onSelect(node.identifier, node.type);
	}
	if (result) {
		this.forceSelectNode(node);
	}
	
	if (this.isInited && this.onAfterSelect) {
		result = this.onAfterSelect(node.identifier, node.type);
	}
}
SbTree.forceSelectNode = function(node) {
	this.selectedNode = node;
	for(var i = 0; i < this.nodesList.length; i++) {
		if (this.selectedNode != this.nodesList[i]) {
			this.nodesList[i].unSelect();
		}
	}
	this.selectedNode.select();
}

SbTree.unSelect = function() {
	if (null != this.selectedNode) {
		this.selectedNode.unSelect();
	}
	this.selectedNode = null;
}

SbTree.registerFocusNode = function(node) {
	this.focusNode = node;
	dragControl.registerTarget(this, this.focusNode);
}

SbTree.unRegisterFocusNode = function(node) {
	this.focusNode = null;
	dragControl.unRegisterTarget(this, node);
}

SbTree.mouseover = function(event) {
	dragControl.registerTargetRoot(this);
}

SbTree.mouseout = function(event) {
	dragControl.unRegisterTargetRoot(this);
}

function SbTreeNode(tree, nodeId, fName, type, selected, enabled, status) {
	var item;
	var checkbox;

	item = document.getElementById(nodeId);
	item.fName = fName;
	item.status = status;
	item.tree = tree;
	item.identifier = nodeId;
	item.originalIdentifier = nodeId;
	item.oldText = '';
	if ((null == type) || ('' == type)) {
		type = 'Simple';
	}
	item.type = type;
	item.isEnabled = (enabled.toLowerCase() != 'false');
	item.isExpanded = true;
	item.isSelected = (selected.toLowerCase() == 'true');
	item.isNewNode = false;
	item.isEditing = false;
	item.isEndEditing = false;

	item.checkbox = document.getElementById(nodeId + 'Check');
	item.isChecked = item.checkbox.checked;
	item.textObject = document.getElementById(nodeId + 'Span');
	item.iconObject = document.getElementById(nodeId + 'Icon');
	item.stateObject = document.getElementById(nodeId + 'State');
	item.lineObject = document.getElementById(nodeId + 'Line');
	
	if (item.isSelected) {
		item.textObject.className = 'TreeNodeSelect';
	} else {
		item.textObject.className = 'TreeNode';
	}
	item.iconObject.className = 'TreeNodeIcon';
	item.checkbox.className = 'TreeNodeCheckbox';

	item.setTitle = SbTreeNode.setTitle;
	item.check = SbTreeNode.check;
	item.unCheck = SbTreeNode.unCheck;
	item.select = SbTreeNode.select;
	item.unSelect = SbTreeNode.unSelect;
	item.enable = SbTreeNode.enable;
	item.disable = SbTreeNode.disable;
	item.addListeners = SbTreeNode.addListeners;
	item.removeListeners = SbTreeNode.removeListeners;
	item.expand = SbTreeNode.expand;
	item.collapse = SbTreeNode.collapse;
	item.hasChilds = SbTreeNode.hasChilds;
	item.getChildList = SbTreeNode.getChildList;
	item.cloneSbTreeNode = SbTreeNode.cloneSbTreeNode;
	item.prepareForMoving = SbTreeNode.prepareForMoving;
	item.restoreAfterMoving = SbTreeNode.restoreAfterMoving;
	item.setVisibleLevel = SbTreeNode.setVisibleLevel;
	item.updateVisibility = SbTreeNode.updateVisibility;
	item.startEdit = SbTreeNode.startEdit;
	item.endEdit = SbTreeNode.endEdit;
	item.keyPress = SbTreeNode.keyPress;
	item.converToXml = SbTreeNode.converToXml;
	item.isSomeChildsChecked = SbTreeNode.isSomeChildsChecked;
	item.isSomeChildsNotChecked = SbTreeNode.isSomeChildsNotChecked;

	item.addListeners();
	if (!item.isEnabled) {
		item.disable();
	}
	if (item.hasChilds()) {
		item.stateObject.className = 'TreeNodeState';
		item.expand();
	}
	if (item.parentNode == item.tree) {
		item.lineObject.style.visibility = 'hidden';
		item.lineObject.style.display = 'none';
	}
	if (!tree.isCheckable) {
		item.checkbox.style.visibility = 'hidden';
		item.checkbox.style.display = 'none';
	}
	tree.registerNode(item);
	if (item.isSelected) {
		tree.selectNode(item);
	}
}

SbTreeNode.cloneSbTreeNode = function() {
	var result;
	var tempList;

	result = this.cloneNode(true);
	
	result.tree = null;
	result.identifier = true;
	result.originalIdentifier = this.identifier;
	
	result.oldText = '';
	result.type = this.type;
	result.isEnabled = this.isEnabled;
	result.isExpanded = true;
	result.isSelected = false;
	result.isNewNode = true;
	result.isEditing = false;
	result.isEndEditing = false;
	result.lineObject = null;
	result.checkbox = null;
	result.iconObject = null;
	result.stateObject = null;
	result.lineObject = null;
	result.textObject = null;

	result.setTitle = SbTreeNode.setTitle;
	result.check = SbTreeNode.check;
	result.unCheck = SbTreeNode.unCheck;
	result.select = SbTreeNode.select;
	result.unSelect = SbTreeNode.unSelect;
	result.enable = SbTreeNode.enable;
	result.disable = SbTreeNode.disable;
	result.addListeners = SbTreeNode.addListeners;
	result.removeListeners = SbTreeNode.removeListeners;
	result.expand = SbTreeNode.expand;
	result.collapse = SbTreeNode.collapse;
	result.hasChilds = SbTreeNode.hasChilds;
	result.getChildList = SbTreeNode.getChildList;
	result.prepareForMoving = SbTreeNode.prepareForMoving;
	result.restoreAfterMoving = SbTreeNode.restoreAfterMoving;
	result.setVisibleLevel = SbTreeNode.setVisibleLevel;
	result.updateVisibility = SbTreeNode.updateVisibility;
	result.startEdit = SbTreeNode.startEdit;
	result.endEdit = SbTreeNode.endEdit;
	result.keyPress = SbTreeNode.keyPress;
	result.converToXml = SbTreeNode.converToXml;
	result.isSomeChildsChecked = SbTreeNode.isSomeChildsChecked;
	result.isSomeChildsNotChecked = SbTreeNode.isSomeChildsNotChecked;

	if (this.hasChilds()) {
		var childList = result.getChildList();
		var originalChildList = this.getChildList();
		for (var i = 0; i < originalChildList.length; i++) {
			if (i < childList.length) {
				result.replaceChild(originalChildList[i].cloneSbTreeNode(), childList[i]);
			}
		}
	}

	tempList = result.getElementsByTagName('INPUT');
	if (tempList.length > 0) {
		result.checkbox = tempList[0];
		result.checkbox.id = '';
		result.isChecked = this.isChecked;
		result.checkbox.checked = this.checkbox.checked;
	}
	tempList = result.getElementsByTagName('SPAN');
	if (tempList.length > 0) {
		result.textObject = tempList[0];
		result.textObject.id = '';
	}
	tempList = result.getElementsByTagName('IMG');
	for (var i = 0; i < tempList.length; i++) {
		if ((null != this.iconObject) && (tempList[i].id == this.iconObject.id)) {
			result.iconObject = tempList[i];
			result.iconObject.id = '';
			continue;
		}
		if ((null != this.stateObject) && (tempList[i].id == this.stateObject.id)) {
			result.stateObject = tempList[i];
			result.stateObject.id = '';
			continue;
		}
		if ((null != this.lineObject) && (tempList[i].id == this.lineObject.id)) {
			result.lineObject = tempList[i];
			result.lineObject.id = '';
		}
	}
	return result;
}


SbTreeNode.isSomeChildsChecked = function() {
	var result = false;
	var list = this.getChildList();
	for (var i = 0; i < list.length; i++) {
		if (list[i].isChecked) {
			result = true;
			break;
		}
	}
	return result;
}

SbTreeNode.isSomeChildsNotChecked = function() {
	var result = false;
	var list = this.getChildList();
	for (var i = 0; i < list.length; i++) {
		if (!list[i].isChecked) {
			result = true;
			break;
		}
	}
	return result;
}

SbTreeNode.converToXml = function() {
	var result = '<PAGE';
	if ('Simple' == this.type) {
		result += _getXmlAttribute('TYPE', this.type);
	} else {
		result += _getXmlAttribute('TYPE', 'Module');
		result += _getXmlAttribute('NAME', this.type);
	}
	if ('boolean' != typeof(this.identifier)) {
		result += _getXmlAttribute('ID', this.identifier);
	}
	if ('boolean' != typeof(this.originalIdentifier)) {
		result += _getXmlAttribute('ORIGINAL-ID', this.originalIdentifier);
	}
	result += _getXmlAttribute('TITLE', _unHtmlSpecialChars(this.textObject.innerHTML));
	result += _getXmlAttribute('SHOW-IN-NAVIGATION', this.status);
	
	if (this.isSelected) {
		result += _getXmlAttribute('SELECTED', '1');
	}
	
	if (this.hasChilds()) {
		result += '>';
		for(var i = 0; i < this.childNodes.length; i++) {
			if(this.childNodes[i].tagName == 'DIV') {
				if (this.childNodes[i].identifier) {
					result += this.childNodes[i].converToXml();
				}
			}
		}
		result += '</PAGE>';
	} else {
		result += '/>';
	}
	return result;
}

SbTreeNode.prepareForMoving = function(isChildren) {
	this.removeListeners();
	if (this.hasChilds()) {
		this.expand();
	}
	if (this.isEnabled) {
		this.textObject.className = 'TreeNode';
		this.iconObject.className = 'TreeNodeIcon';
		this.textObject.style.textDecoration = 'none';
		this.checkbox.className = 'TreeNodeCheckbox';
	}
	if (null != this.stateObject) {
		this.stateObject.style.visibility = 'hidden';
		this.stateObject.style.display = 'none';
	}
	if (null != this.lineObject) {
		if (isChildren) {
			this.lineObject.src = '/images/blank.gif';
		} else {
			this.lineObject.style.visibility = 'hidden';
			this.lineObject.style.display = 'none';
		}
	}
	if (null != this.checkbox) {
		this.checkbox.style.visibility = 'hidden';
		this.checkbox.style.display = 'none';
	}
}

SbTreeNode.restoreAfterMoving = function(isChildren) {
	this.addListeners();
	if (this.hasChilds()) {
		this.expand();
	}
	if (null != this.stateObject) {
		this.stateObject.style.visibility = 'visible';
		this.stateObject.style.display = 'inline';
	}
	this.setVisibleLevel(isChildren);
	if (null != this.checkbox && this.tree.isCheckable) {
		this.checkbox.style.visibility = 'visible';
		this.checkbox.style.display = 'inline';
	}
	if (this.isEnabled) {
		this.textObject.className = 'TreeNode';
		this.iconObject.className = 'TreeNodeIcon';
		this.checkbox.className = 'TreeNodeCheckbox';
	}
}

SbTreeNode.updateVisibility = function(isChildren) {
	if ((!isChildren) || this.parentNode.isExpanded) {
		this.style.visibility = 'visible';
		this.style.display = 'block';
	} else {
		this.style.visibility = 'hidden';
		this.style.display = 'none';
	}
}

SbTreeNode.setVisibleLevel = function(isChildren) {
	if (null != this.lineObject) {
		if (isChildren) {
			this.lineObject.src = '/images/blank.gif';
			this.lineObject.style.visibility = 'visible';
			this.lineObject.style.display = 'inline';
		} else {
			this.lineObject.style.visibility = 'hidden';
			this.lineObject.style.display = 'none';
		}
	}
}

SbTreeNode.hasChilds = function() {
	var result = false;
	var childList = this.getChildList();

	if (childList.length > 0) {
		result = true;
	}
	return result;
}

SbTreeNode.getChildList = function() {
	var result = Array();
	var list = this.getElementsByTagName('DIV');
	for (var i = 0; i < list.length; i++) {
		if (list[i].parentNode == this) {
			result[result.length] = list[i];
		}
	}
	return result;
}

SbTreeNode.addListeners = function() {
	if (this.isEnabled) {
		var node = this;

		this.mouseclick = SbTreeNode.mouseclick;
		this.textObject.onclick = function(event) { _onNodeEvent(node, 'mouseclick', event); };
		this.iconObject.onclick = function(event) { _onNodeEvent(node, 'mouseclick', event); };
		this.textObject.ondblclick = function(event) { _onNodeEvent(node, 'mouseclick', event); _onNodeEvent(node, 'mouseclick', event); };
		this.iconObject.ondblclick = function(event) { _onNodeEvent(node, 'mouseclick', event); _onNodeEvent(node, 'mouseclick', event); };
		
		this.checkboxClick = SbTreeNode.checkboxClick;
		this.checkbox.onclick = function(event) { _onNodeEvent(node, 'checkboxClick', event); };

		this.stateClick = SbTreeNode.stateClick;
		this.stateObject.onclick = function(event) { _onNodeEvent(node, 'stateClick', event); };

		this.mouseover = SbTreeNode.mouseover;
		this.textObject.onmouseover = function(event) { _onNodeEvent(node, 'mouseover', event); };
		this.iconObject.onmouseover = function(event) { _onNodeEvent(node, 'mouseover', event); };

		this.mouseout = SbTreeNode.mouseout;
		this.textObject.onmouseout = function(event) { _onNodeEvent(node, 'mouseout', event); };
		this.iconObject.onmouseout = function(event) { _onNodeEvent(node, 'mouseout', event); };

		if (this.tree && this.tree.isDragable) {
			this.mousedown = SbTreeNode.mousedown;
			this.textObject.onmousedown = function(event) { _onNodeEvent(node, 'mousedown', event); };
			this.iconObject.onmousedown = function(event) { _onNodeEvent(node, 'mousedown', event); };
		}
	}
}

SbTreeNode.removeListeners = function() {
	var node = this;

	this.mouseclick = null;
	this.textObject.onclick = null;
	this.iconObject.onclick = null;
	this.textObject.ondblclick = null;
	this.iconObject.ondblclick = null;
	
	this.checkboxClick = null;
	this.checkbox.onclick = null;

	this.stateClick = null;
	this.stateObject.onclick = null;

	this.mouseover = null;
	this.textObject.onmouseover = null;
	this.iconObject.onmouseover = null;

	this.mouseout = null;
	this.textObject.onmouseout = null;
	this.iconObject.onmouseout = null;

	this.mousedown = null;
	this.textObject.onmousedown = null;
	this.iconObject.onmousedown = null;
}

SbTreeNode.stateClick = function(event) {
	if (this.isExpanded) {
		this.collapse();
	} else {
		this.expand();
	}
}

SbTreeNode.checkboxClick = function(event) {
	if (this.isChecked) {
		this.unCheck();
	} else {
		this.check();
	}
	if (this.tree.onCheck) {
		this.tree.onCheck();
	}
}

SbTreeNode.expand = function() {
	var childList = this.getChildList();
	for(var i = 0; i < childList.length; i++) {
		childList[i].style.display = 'block';
		childList[i].style.visibility = 'visible';
	}
	if (childList.length > 0) {
		this.isExpanded = true;
		if ((null != this.tree) && this.tree.withLines) {
			this.tree.drawNodeLines(this);
		} else {
			this.stateObject.src = '/images/tree/expanded.gif';
		}
	}
}

SbTreeNode.collapse = function() {
	var childList = this.getChildList();
	for(var i = 0; i < childList.length; i++) {
		childList[i].style.display = 'none';
		childList[i].style.visibility = 'hidden';
	}
	if (childList.length > 0) {
		this.isExpanded = false;
		if ((null != this.tree) && this.tree.withLines) {
			this.tree.drawNodeLines(this);
		} else {
			this.stateObject.src = '/images/tree/collapsed.gif';
		}
	}
}

SbTreeNode.check = function() {
	if (this.isEnabled) {
		this.checkbox.checked = true;
		this.isChecked = true;

		if (!this.isSomeChildsChecked()) {
			var childList = this.getChildList();
			for(var i = 0; i < childList.length; i++) {
				childList[i].check();
			}
		}
	}
}

SbTreeNode.unCheck = function() {
	this.checkbox.checked = false;
	this.isChecked = false;
	if (!this.isSomeChildsNotChecked()) {
		var childList = this.getChildList();
		for(var i = 0; i < childList.length; i++) {
			childList[i].unCheck();
		}
	}
}

SbTreeNode.select = function() {
	if (this.isEnabled) {
		if (this.tree.isSelectable) {
			this.textObject.className = 'TreeNodeSelect';
		}
		this.isSelected = true;
	}
}

SbTreeNode.unSelect = function() {
	if (this.isEnabled) {
		if (this.tree.isSelectable) {
			this.textObject.className = 'TreeNode';
		}
		this.isSelected = false;
	}
}

SbTreeNode.startEdit = function() {
	var node = this;
	if (document.all) {
		this.tree.onselectstart = null;
	} else {
		this.tree.style.MozUserSelect = '';
	}
	if (null != editingNode) {
		window.setTimeout(function () { node.startEdit() }, 100);
		return false;
	}
	var text = this.textObject.innerHTML;
	this.oldText = text;
	this.input = document.createElement('input');
	this.input.setAttribute('type', 'text');
	this.input.setAttribute('autocomplete', 'OFF'); //https://bugzilla.mozilla.org/show_bug.cgi?id=236791
	this.input.size = text.length + 5;
	this.input.value = _unHtmlSpecialChars(text);
	this.input.className = 'TreeNodeEdit';
	this.textObject.style.display = 'none';
	this.textObject.style.visibility = 'hidden';
	this.input = this.insertBefore(this.input, this.textObject);

	this.input.onblur = function(event) { return _onNodeEvent(node, 'endEdit', event); };
	this.input.onchange = function(event) { return _onNodeEvent(node, 'endEdit', event); };
	this.input.onkeypress = function(event) { return _onNodeEvent(node, 'keyPress', event); };
	this.input.onsubmit = stopEvent;

	if (this.input.createTextRange) {
		var range = this.input.createTextRange();
		range.moveStart('character', 0);
		range.moveEnd('character', text.length);
		range.select();
	} else {
		this.input.select();
	}

	this.input.focus();
	editingNode = this;
	this.isEditing = true;
}

SbTreeNode.keyPress = function(event) {
	if (event.keyCode == 13) {
		stopEvent(event);
		this.endEdit(event);
		return false;
	}
	if (event.keyCode == 27) {
		this.input.value = this.textObject.innerHTML;
		this.endEdit(event);
	}
	return true;
}

SbTreeNode.setTitle = function(title) {
	this.textObject.innerHTML = _htmlSpecialChars(title);
}

SbTreeNode.endEdit = function(event) {
	if (!this.isEndEditing) {
		this.isEndEditing = true;
		if (this.isEditing && (null != this.input)) {
			var inputValue = this.input.value.replace(/^\s+|\s+$/g, '');
			if (inputValue.length == 0) {
				alert(SbGetLocaleByKey('emptyPageTitleWarning'));
				this.removeChild(this.input);
				this.input = null;
				this.isEditing = false;
				this.isEndEditing = false;
				editingNode = null;
				this.startEdit();
				return;
			}
			this.setTitle(this.input.value);
			this.removeChild(this.input);
			if (this.oldText != this.textObject.innerHTML) {
				this.tree.registerChange();
			}
		}
		this.textObject.style.display = 'inline';
		this.textObject.style.visibility = 'visible';
		this.input = null;
		if (document.all) {
			this.tree.onselectstart = function () { return false; };
		} else {
			this.tree.style.MozUserSelect = 'none';
		}
		editingNode = null;
		this.isEditing = false;
		this.isEndEditing = false;
	}
}

SbTreeNode.disable = function() {
	this.unCheck();
	if (this.tree.onCheck) {
		this.tree.onCheck();
	}
	this.tree.unSelect();
	this.removeListeners();
	this.textObject.className = 'TreeNodeDisabled';
	this.iconObject.className = 'TreeNodeIconDisabled';
	this.textObject.style.cursor = 'not-allowed';
	this.iconObject.style.cursor = 'not-allowed';
	this.checkbox.className = 'TreeNodeCheckboxDisabled';
	this.checkbox.disabled = true;

	for(var i = 0; i < this.childNodes.length; i++) {
		if(this.childNodes[i].identifier) {
			this.childNodes[i].disable();
		}
	}
	this.isEnabled = false;
}

SbTreeNode.enable = function() {
	this.checkbox.disabled = false;
	this.textObject.className = 'TreeNode';
	this.iconObject.className = 'TreeNodeIcon';
	this.textObject.style.cursor = 'pointer';
	this.iconObject.style.cursor = 'pointer';
	this.checkbox.className = 'TreeNodeCheckbox';
	this.isEnabled = true;
	this.addListeners();
}

SbTreeNode.mouseclick = function(event) {
	if (null != editingNode) {
		editingNode.endEdit(event);
	}
	if (this.tree.selectedNode == this) {
		if (!this.tree.isStatic && this.tree.isRenamable) {
			this.startEdit();
		}
	} else {
		if (null != this.tree.selectedNode) {
			this.tree.selectedNode.textObject.style.textDecoration = 'none';
		}
		return this.tree.selectNode(this);
	}
}

SbTreeNode.mouseover = function(event) {

	if (!(this.isDisabled || this.isSelected || this.isEditing)) {
		this.textObject.style.textDecoration = 'underline';
	}
	this.tree.registerFocusNode(this);
}

SbTreeNode.mouseout = function(event) {
	if (!(this.isDisabled || this.isEditing)) {
		this.textObject.style.textDecoration = 'none';
	}
	this.tree.unRegisterFocusNode(this);
}

SbTreeNode.mousedown = function(event) {
	if (null != editingNode) {
		editingNode.endEdit(event);
	}
	if (this.tree.isDragable && !this.isEditing) {
		if (this.parentNode == this.tree) {
			var firstLevelNodeList = this.tree.getChildList();
			if (this.tree.isStatic || (firstLevelNodeList.length > 1)) {
				dragControl.prepareDrag(this, event);
			}
		} else {
			dragControl.prepareDrag(this, event);
		}
	}
	stopEvent(event);
}







function _htmlSpecialChars(str) {
	try {
		str = str.replace(/&/ig, '&amp;');
		str = str.replace(/\?/ig, '&#63;');
		str = str.replace(/</ig, '&lt;');
		str = str.replace(/>/ig, '&gt;');
		str = str.replace(/\'/ig, '&#39;');
		str = str.replace(/\"/ig, '&#34;');
	} catch(e){ }

	return str;
}

function _unHtmlSpecialChars(str) {
	try {
		str = str.replace(/&#34;/ig, '\"');
		str = str.replace(/&#39;/ig, "'");
		str = str.replace(/&gt;/ig,'>');
		str = str.replace(/&lt;/ig,'<');
		str = str.replace(/&#63;/ig,'?');
		str = str.replace(/&amp;/ig, '&');
	} catch(e){ }

	return str;
}

function _getXmlAttribute(name, value, force) {
	if (!force) {
		value = _htmlSpecialChars(value);
	}
	return ' ' + name.toUpperCase() + '="' + value + '"';
}