/********************************************************************************************
* BlueShoes Framework; This file is part of the php application framework.
* NOTE: This code is stripped (obfuscated). To get the clean documented code goto 
*       www.blueshoes.org and register for the free open source *DEVELOPER* version or 
*       buy the commercial version.
*       
*       In case you've already got the developer version, then this is one of the few 
*       packages/classes that is only available to *PAYING* customers.
*       To get it go to www.blueshoes.org and buy a commercial version.
* 
* @copyright www.blueshoes.org
* @author    Samuel Blume <sam at blueshoes dot org>
* @author    Andrej Arn <andrej at blueshoes dot org>
*/
function Bs_Tree(instanceName) {
	this.autoCollapse = true;
	this.lookAhead = 5;
	this.captionBgColor    = "yellow";
	this.linkStyle;
	this.divStyle = 'font-family: Arial, Verdana, Tahoma, sans-serif;width:170px;height:16px;padding-left:27px;margin-top:14px;font-weight:bold;font-size:11px;'
	this.showPseudoElement = false;
	this.useCheckboxSystem = false;
	this.checkboxSystemWalkTree = 3;
	this.checkboxSystemIfPartlyThenFull = true;
	this.checkboxSystemImgDir;
	this.checkboxSystemGuiNochange;
	this.useRadioButton = false;
	this.radioButtonName;
	this.imageDir = '/img_layout/menu/';
	this.imageHeight = 13;
	this.imageWidth = 16;
	this.useFolderIcon = false;
	this.useLeaf = true;
	this.walkTree = true;
	this.useAutoSequence = true;
	this.draggable = false;
	this._clearingHouse = new Array;
	this._instanceName;
	this._pseudoElement;
	this._currentActiveElement;
	this._elementSequence = 0;
	this._errorArray;

	this.initByArray = function(arr) {
		for (var i=0; i<arr.length; i++) {
			var e = this._createTreeElement(arr[i], 1);
			if (e == false) {
				return false;
			}
			this._pseudoElement.addChild(e);
		}
	return true;
	}

	this.getActiveElement = function() {
		if (typeof(this._currentActiveElement) != 'undefined') return this._currentActiveElement;
		return false;
	}

	this.setActiveElement = function(treeElement) {
		this._currentActiveElement = treeElement;
	}

	this._createTreeElement = function(arr, level) {
		if (typeof(level) == 'undefined') level = 1;
		var e = new Bs_TreeElement();
		var status = e.initByArray(arr, this, level);
		if (!status) {
			this._addError(e.getLastError());
			return false;
		}
		this._clearingHouse[e.id] = e;
		if (arr['children']) {
			for (var i=0; i<arr['children'].length; i++) {
				var newE = this._createTreeElement(arr['children'][i], level +1);
				if (!newE) return false;
				e.addChild(newE);
			}
		}
		return e;
	}

	this.getElement = function(elementId) {
		if (elementId == 0) return this._pseudoElement;
		if (typeof(this._clearingHouse[elementId]) == 'object') {
			return this._clearingHouse[elementId];
		} else {
			return false;
		}
	}

	this.removeElement = function(elementId) {
		if (typeof(this._clearingHouse[elementId]) == 'undefined') return false;
		var elm = this._clearingHouse[elementId];
		if ((typeof(elm.parent) == 'object') && (typeof(elm.parent._children) == 'object')) {
			for (var i=0; i<elm.parent._children.length; i++) {
				if (elm.parent._children[i].id == elementId) {
					elm.parent._children.deleteItem(i);
					break;
				}
			}
		}
		this._clearingHouse.deleteItemHash(elementId);
		for (var i=0; i<elm._children.length; i++) {
			this._clearingHouse.deleteItemHash(elm._children[i].id);
		}
		if ((typeof(elm.parent) == 'object') && (elm.parent._isOutrendered)) {
			elm.parent.render(true, true);
		}
		return true;
	}

	this.draw = function() {
		var content = this._pseudoElement.render();
		document.writeln(content[0]);
		eval(content[1]);
	}

	this.toHtml = function() {
		return this._pseudoElement.render();
	}

	this.executeOnElement = function(id, func, params) {
		if (this._clearingHouse[id]) {
			if (this._clearingHouse[id][func]) {
				if (params) {
					switch (params.length) {
						case 1:
							return this._clearingHouse[id][func](params[0]);
							break;
						case 2:
							return this._clearingHouse[id][func](params[0], params[1]);
							break;
						case 3:
							return this._clearingHouse[id][func](params[0], params[1], params[2]);
							break;
						case 4:
							return this._clearingHouse[id][func](params[0], params[1], params[2], params[3]);
							break;
					}
				} else {
					return this._clearingHouse[id][func]();
				}
			}
		}
		return;
	}

	this.getJavascriptCode = function() {
		return this._pseudoElement.getJavascriptCode('a', true);
	}

	this.elementToggleOpenClose = function(id) {
		this._clearingHouse[id].toggleOpenClose();
	}

	this.elementOpenWalkUp = function(id) {
		if (typeof(this._clearingHouse[id]) != 'undefined') {
			var elm = this._clearingHouse[id];
			elm.open(true);
			if (typeof(elm.parent) != 'undefined') this.elementOpenWalkUp(elm.parent.id);
		} else {
			return false;
		}
		return true;
	}

	this.elementCloseWalkUp = function(id) {
		if (typeof(this._clearingHouse[id]) != 'undefined') {
			var elm = this._clearingHouse[id];
			elm.close(true);
			if (typeof(elm.parent) != 'undefined') this.elementCloseWalkUp(elm.parent.id);
		} else {
			return false;
		}
	return true;
	}

	this.elementCloseWalkDown = function(id) {
		if (typeof(id) == 'undefined') {
			var elm = this._pseudoElement;
		} else if (typeof(this._clearingHouse[id]) != 'undefined') {
			var elm = this._clearingHouse[id];
			elm.close(true);
		} else {
			return false;
		}
		if (typeof(elm._children) != 'undefined') {
			for (var i=0; i<elm._children.length; i++) {
				this.elementCloseWalkDown(elm._children[i].id);
			}
		}
		return true;
	}

	this.elementOpen = function(id) {
		if (typeof(this._clearingHouse[id]) != 'undefined') {
			this._clearingHouse[id].open();
		}
	}

	this.elementClose = function(id) {
		this._clearingHouse[id].close();
	}

	this.openPath = function(data, valueType) {
		var elm = this.getElementByCaptionPath(data);
		if (elm == false) return false;
		this.elementOpenWalkUp(elm.id);
		return true;
	}

	this.getElementByCaptionPath = function(data) {
		var elm = this._pseudoElement;
		for (var i=0; i<data.length; i++) {
			var newElm = null;
			for (var j=0; j<elm._children.length; j++) {
				if (elm._children[j].caption == data[i]) {
					newElm = elm._children[j];
					elm = newElm;
					break;
				}
			}
			if (newElm == null) return false;
		}
		return newElm;
	}

	this.elementCheckboxEvent = function(id, value) {
		this._clearingHouse[id].checkboxEvent(value);
	}

	this.debugDumpTree = function(elm, indent) {
		if (typeof(elm) == 'undefined') {
			elm    = this._pseudoElement;
			indent = '';
			var firstCall = true;
		}
		var ret = '';
		if (typeof(elm._children) == 'object') {
			for (var i=0; i<elm._children.length; i++) {
				ret += indent + i + ': ' + elm._children[i].navItemID + ': ' + elm._children[i].id + ': ' + elm._children[i].caption + "\n";
				ret += this.debugDumpTree(elm._children[i], indent + '  ');
			}
		}
		if (firstCall) {
			alert(ret);
		} else {
			return ret;
		}
	}

	this._addError = function(str) {
		if (typeof(this._errorArray) == 'undefined') {
			this._errorArray = new Array(str);
		} else {
			this._errorArray[this._errorArray.length] = str;
		}
	}


	this.getLastError = function() {
		if (typeof(this._errorArray) != 'undefined') {
			if (this._errorArray.length > 0) {
				return this._errorArray[this._errorArray.length -1];
			}
		}
	return false;
	}


	this.old_drawInto = function(id) {
		var content = this._pseudoElement.render();
		var e       = document.getElementById(id);
		if (e) {
			e.innerHTML = content[0];
			if ('' != content[1]) eval(content[1]);
		}
	}

	this._imgPreload = function() {
		var id = this.globalId;
		var e  = document.getElementById(id);
		var ii = 0;
		var outTemp = new Array();
		outTemp[ii++] = '<img src="' + this.imageDir + 'line1.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'line2.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'line3.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'minus1.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'minus2.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'minus3.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'plus1.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'plus2.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'plus3.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'line3.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'empty.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'leaf.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'folderClosed.gif" border="0" style="display:none;">';
		outTemp[ii++] = '<img src="' + this.imageDir + 'folderOpen.gif" border="0" style="display:none;">';
		if (e) e.innerHTML = outTemp.join('');
	}

	this._afterImgPreload = function() {
		var id = this.globalId;
		var content = this._pseudoElement.render();
		var e       = document.getElementById(id);
		if (e) {
			e.innerHTML = content[0];
			if ('' != content[1]) eval(content[1]);
		}
	}

	this.drawInto = function(id) {
		this.globalId = id;
		setTimeout(this._instanceName + '._imgPreload()', 0);
		setTimeout(this._instanceName + '._afterImgPreload()',500);
	}

	if (!instanceName) {
		instanceName = 't';
	}

	this._instanceName = instanceName;
	var a = new Array;
	a['id'] = 'pseudoElement001';
	a['caption'] = "root";
	a['url'] = "";
	a['target'] = "";
	a['isOpen'] = true;
	this._pseudoElement = this._createTreeElement(a, 0);

//eigen functie om de tree uit te schrijven in een separated string
//deze wordt gepost naar navitemsupdate.asp en vervolgens in de database opgeslagen

	this.listTreeItems = function(elm, indent) {
		if (typeof(elm) == 'undefined') {
			elm    = this._pseudoElement;
			indent = '';
			var firstCall = true;
		}
		var ret = '';
		if (typeof(elm._children) == 'object') {
			for (var i=0; i<elm._children.length; i++) {
				ret += elm._children[i].navItemID + '#:#' 
				if (typeof(elm._children[i].parent.navItemID) == 'undefined'){
					ret += '0'
				}else{
					ret += elm._children[i].parent.navItemID
				}
				ret += '#:#' + elm._children[i].url + '#:#' + elm._children[i].caption + '#:#' + elm._children[i].id + "#;#";
				ret += this.listTreeItems(elm._children[i], indent + '   ');
			}
		}
		return ret;
	}
}
