//================================================
//===	Wildgroup 2001 Oleg Myaskovsky
//===	Abstract tree data structure
//===	ver. 1.8
//================================================

var trees = new Array();

//=============================================================
//===	search for Tree instance by "extID" in trees
//=============================================================
function findTree(extID) {
	var tree = null;
	for (var i=0; i<trees.length; i++)
		if (trees[i].extID == extID) {
			tree = trees[i];
			break;
		}
	return tree;
}

//=============================================================
//===	get Tree instance by "extID"
//=============================================================
function getTree(extID) {
	var tree = findTree(extID);
	if (tree == null) {
		tree = new Tree(extID);
		tree.init();
	}
	return tree;
}

//=============================================================
//===	Tree class: START
//=============================================================
function Tree(extID) {
	this.extID = extID || "";			// external ID

	this.id = -1;						// internal ID
	this.init = function () {
		this.id = trees.length;
		trees[this.id] = this;
	}
	
	this.treeview = new Array();		// treeview components which use this tree
	
	this.items = new Array();			// items
	
	this.getRoots = function () {
		var roots = new Array();
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null)
				if (this.items[i].parent == null)
					roots[roots.length] = this.items[i];
		return roots;
	}
	
	//	search for Item instanse in items
	this.findItem = function (extID) {	
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null)
				if (this.items[i].extID == extID)
					return this.items[i];
		return null;
	}
	
	this.findItems = function (extID) {	
		var result = new Array();
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null)
				if (this.items[i].extID == extID)
					result[result.length] = this.items[i];
		return result;
	} 

	this.findItemByID = function (id) {	
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null)
				if (this.items[i].id == id)
					return this.items[i];
		return null;
	}
	
	//	get Item instanse
	this.getItem = function (extID, parent, force) {
		var item = (force)? null : this.findItem(extID);
		if (item == null) {
			item = new Item(extID, this, parent);
			item.init();
			this.recentItem = item;
		}
		else {
			if (parent != null)
				if (item.parent != parent)
					parent.items[parent.items.length] = item;
		}
		return item;
	}
	
	//	remove Item instanse in items
	this.removeItem = function (extID) {
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null)
				if (this.items[i].extID == extID) {
					this.items[i] = null;
					break;
				}
	}

	//	recent Item, is assigned in getItem
	this.recentItem = null;		
}
//=============================================================
//===	Tree class: STOP
//=============================================================

//=============================================================
//===	Item class: START
//=============================================================
function Item(extID, tree, parent) {
	this.extID = extID || "";			//	external ID
	this.id = -1;						//	internal ID
	this.init = function () {
		this.id = this.tree.items.length;
		this.tree.items[this.id] = this;
		if (this.parent != null)
			this.parent.items[this.parent.items.length] = this;
	}

	this.tree = tree;					//	tree
	this.parent = parent;				//	parent item
	this.isOpen = (parent == null)? true:false;	//	item state
	this.setIsOpen = function(isOpen) {
		this.isOpen = isOpen;
		if (!this.isOpen) {
			this.close();			
		}
	}	
	this.close = function () {
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null) {
				this.items[i].isOpen = false;
				this.items[i].close();
			}
	}

	this.items = new Array();				//	child items
	
	//	search for Item instanse in items
	this.findItem = function (extID) {
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null)
				if (this.items[i].extID == extID)
					return this.items[i];
		return null;
	}
	
	//	get Item instanse
	this.getItem = function (extID, force) {
		return this.tree.getItem(extID, this, force);
	}
	
	//	remove Item instanse in items
	this.removeItems = function () {
		for (var i=0; i<this.items.length; i++)
			if (this.items[i] != null)
				this.tree.removeItem(this.items[i].extID);
		this.items = new Array();
	}

	this.obj = new Array();				//	obj[i] is item layer handle where i is treeview.id
	this.text = 'Loading...';
	this.hasItems = null;				//	true if items.lemgth > 0
	this.imgClosed = null;				//	icons
	this.imgOpened = null;
	this.menuItems = null;				//	menu items
	this.url = null;					//	url
	this.type = null;					//	type (used with filters)
}
//=============================================================
//===	Item class: STOP
//=============================================================

//=============================================================
//===	get MenuItem instance
//=============================================================
function getMenuItem(text, url, enabled) {
	return new MenuItem(text, url, enabled);
}

//=============================================================
//===	MenuItem class: START
//=============================================================
function MenuItem(text, url, enabled) {
  this.text = text;
  this.url = url;
  this.enabled = enabled;
}
//=============================================================
//===	Item class: STOP
//=============================================================

