// MenuTree v1.00
MENUITEM_BUTTON = 0;
MENUITEM_ICON   = 1;
MENUITEM_TEXT   = 2;

ERR_MENUTREE_MENUITEM_UNDEFINED_CAPTION = 0x01;
ERR_MENUTREE_MENUITEM_INVALID_CAPTION   = 0x02;

Array.prototype.insertItem = function(item, idx)
	{
	for(var i = this.length; i > idx; i--)
		this[i] = this[i - 1];
	this[idx] = item;
	}


MenuTree = {
	items: new Object(),
	iconsPath: '',
	emptyMessage: '',
	create: function(htmlContainerID, attr)
		{
		this.items[htmlContainerID] = new menutree(htmlContainerID, attr);

		return this.items[htmlContainerID];
		},
	getItem: function(htmlContainerID)
		{
		if(this.items[htmlContainerID])
			return this.items[htmlContainerID];

		return null;
		},
	getMenuitemById: function(htmlContainerID, id)
		{
		if(this.items[htmlContainerID])
			return this.items[htmlContainerID].root.getMenuitemById(id);

		return null;
		}
}

function menutree(htmlContainerID, attr)
	{
	this.id = htmlContainerID;
	this.root = new menuitem(0, '', [], '', null, null, null, htmlContainerID);
	this.maxID = 0;
	this.lastError = 0;
	this.menubutton = true;
	this.menuicon = true;
	this.lastError = 0;

	if (typeof attr == 'string')
		{
		var attributes = attr.split(',');
		for(var i = 0; i < attributes.length; i++)
			{
			if(attributes[i].indexOf('=') != -1)
				{
				var key_value = attributes[i].split('=', 2);
				switch(typeof this[key_value[0]])
					{
					case 'boolean':
						this[key_value[0]] = key_value[1].match(/^(false|no|0)$/)? false: true;
						break;
					case 'number':
						if(key_value[1].match(/^[-+]?[0-9]+$/))
							this[key_value[0]] = parseInt(key_value[1], 10);
						else if(key_value[1].match(/^[+-]?[0-9]+\.[0-9]+$/))
							this[key_value[0]] = parseFloat(key_value[1]);
						break;
					}
				}
			}
		}

	this.isUniqueID = function(id)
		{
		if(id > this.maxID)
			this.maxID = id;
		return (this.root.getMenuitemById(id) == null);
		};
	this.addMenuitem = function(parentID, id, caption, icons, url, target)
		{
		var menuitem = null;

		if (typeof parentID == 'undefined')
			menuitem = this.root;
		else
			menuitem = this.root.getMenuitemById(parentID);

		if(menuitem == null)
			menuitem = this.root;

		var submenuitem = menuitem.addMenuitem(id, caption, icons, url, target);
		if(submenuitem == false)
			return false;

		return true;
		};
	return this;
	}

function menuitem(id, caption, icons, url, target, parentMenuitem, htmlObj, htmlContainerID)
	{
	var htmlContainer = document.getElementById(htmlContainerID);
	if (typeof htmlContainer == 'undefined')
		return null;

	this.id = id;
	this.caption = caption;
	this.url = url;
	this.icons = icons;
	this.target = target;
	this.parentMenuitem = parentMenuitem;
	this.htmlElement = htmlObj;
	this.htmlContainer = htmlContainer;
	this.selected = false;
	this.submenuitems = new Array();
	this.isRoot = (parentMenuitem == null);
	if(this.isRoot)
		htmlContainer.innerHTML = MenuTree.emptyMessage;
	this.level = function()
		{
		var level = 0;
		var f = this;
		while(!f.isRoot)
			{
			f = f.parentMenuitem;
			level++;
			}

		return level;
		};
	this.isCollapsed = function()
		{
		return (this.htmlContainer.style.display == 'none');
		};
	this.previousVisibleNode = function()
		{
		if(this.isRoot)
			return null;

		var node = this.previousSibling();
		if(node == null)
			{
			if(this.parentMenuitem.isRoot)
				return null;
			return this.parentMenuitem;
			}

		while(!node.isCollapsed())
			node = node.lastChild();

		return node;
		};
	this.nextVisibleNode = function()
		{
		if(!this.isCollapsed())
			return this.firstChild();

		var f = this;
		while(!f.isRoot && ((node = f.nextSibling()) == null))
			f = f.parentMenuitem;

		return node;
		};
	this.lastVisibleNode = function()
		{
		if((this.submenuitems.length > 0) && !this.isCollapsed())
			return this.submenuitems[this.submenuitems.length - 1].lastVisibleNode();
		return this;
		};
	this.previousSibling = function()
		{
		if(this.isRoot)
			return null;
		var idx = this.parentMenuitem.getChildIndexById(this.id);
		if((idx != -1) && this.parentMenuitem.submenuitems[idx - 1])
			return this.parentMenuitem.submenuitems[idx - 1];
		return null;
		};
	this.nextSibling = function()
		{
		if(this.isRoot)
			return null;
		var idx = this.parentMenuitem.getChildIndexById(this.id);
		if((idx != -1) && this.parentMenuitem.submenuitems[idx + 1])
			return this.parentMenuitem.submenuitems[idx + 1];
		return null;
		};
	this.firstChild = function()
		{
		if(this.submenuitems.length > 0)
			return this.submenuitems[0];
		return null;
		};
	this.lastChild = function()
		{
		if(this.submenuitems.length > 0)
			return this.submenuitems[this.submenuitems.length - 1];
		return null;
		};
	this.collapse = function()
		{
		this.htmlContainer.style.display = 'none';
		if(this.htmlElement != null)
			{
			if(this.getMenutree().menubutton)
				this.htmlElement.childNodes[MENUITEM_BUTTON].innerHTML = '<a href="javascript:MenuTree.getMenuitemById(\'' + this.getRoot().htmlContainer.id + '\', \'' + this.id + '\').expand();"><img src="' + MenuTree.iconsPath + 'button_plus.png" border=0></a>';
			if(this.getMenutree().menuicon)
				{
				var iconfilename = 'http://www.eurostaff.com/en/folder_close.png';
				if(this.icons.length >= 1)
					iconfilename = this.icons[0];
				this.htmlElement.childNodes[MENUITEM_ICON].innerHTML = '<img src="' + MenuTree.iconsPath + iconfilename + '" border=0>';
				}
			var url = this.url;
			if(!url)
				url = 'javascript:MenuTree.getMenuitemById(\'' + this.getRoot().htmlContainer.id + '\', \'' + this.id + '\').expand();';
			this.htmlElement.childNodes[MENUITEM_TEXT].innerHTML = '<a class="menuitemtextbox_' + (this.selected? 'sel': 'unsel') + ' menuitemtextbox_' + this.id + '_' + (this.selected? 'sel': 'unsel') + '" href="' + url + '"' + (this.target? ' target="' + this.target + '"': '') + '>' + this.caption + '</a>';
			}
		};
	this.expand = function()
		{
		this.htmlContainer.style.display = 'block';
		if(this.htmlElement != null)
			{
			if(this.getMenutree().menubutton)
				this.htmlElement.childNodes[MENUITEM_BUTTON].innerHTML = '<a href="javascript:MenuTree.getMenuitemById(\'' + this.getRoot().htmlContainer.id + '\', \'' + this.id + '\').collapse();"><img src="' + MenuTree.iconsPath + 'button_minus.png" border=0></a>';
			if(this.getMenutree().menuicon)
				{
				var iconfilename = 'http://www.eurostaff.com/en/folder_open.png';
				if(this.icons.length >= 2)
					iconfilename = this.icons[1];
				else if(this.icons.length >= 1)
					iconfilename = this.icons[0];
				this.htmlElement.childNodes[MENUITEM_ICON].innerHTML = '<img src="' + MenuTree.iconsPath + iconfilename + '" border=0>';
				}
			var url = this.url;
			if(!url)
				url = 'javascript:MenuTree.getMenuitemById(\'' + this.getRoot().htmlContainer.id + '\', \'' + this.id + '\').collapse();';
			this.htmlElement.childNodes[MENUITEM_TEXT].innerHTML = '<a class="menuitemtextbox_' + (this.selected? 'sel': 'unsel') + ' menuitemtextbox_' + this.id + '_' + (this.selected? 'sel': 'unsel') + '" href="' + url + '"' + (this.target? ' target="' + this.target + '"': '') + '>' + this.caption + '</a>';
			}
		};
	this.addChild = function(menuitem)
		{
		this.htmlContainer.insertBefore(menuitem.htmlElement, null);

		var f = this;
		do
			f.expand();
		while(!f.isRoot && (f = f.parentMenuitem));

		this.htmlContainer.insertBefore(menuitem.htmlContainer, null);
		menuitem.parentMenuitem = this;
		return this.submenuitems[this.submenuitems.length] = menuitem;
		};
	this.insertBefore = function(menuitem)
		{
		if(this.isRoot)
			return null;
		this.parentMenuitem.htmlContainer.insertBefore(menuitem.htmlElement, this.htmlElement);
		this.parentMenuitem.htmlContainer.insertBefore(menuitem.htmlContainer, this.htmlElement);
		menuitem.parentMenuitem = this.parentMenuitem;
		return this.parentMenuitem.submenuitems.insertItem(menuitem, this.parentMenuitem.getChildIndexById(this.id));
		};
	this.addMenuitem = function(id, caption, icons, url, target)
		{
		if (typeof id == 'undefined')
			id = ++this.getMenutree().maxID;

		if (typeof caption == 'undefined')
			{
			this.getMenutree().lastError = ERR_MENUTREE_MENUITEM_UNDEFINED_CAPTION;
			return false;
			}
		caption = caption.replace(/^\s*/g, '');
		caption = caption.replace(/\s*$/g, '');
		if((caption == '') || (caption.search(/[\\\/]/g) != -1))
			{
			this.getMenutree().lastError = ERR_MENUTREE_MENUITEM_INVALID_CAPTION;
			return false;
			}

		var root = this.getRoot();
		if(root.submenuitems.length == 0)
			root.htmlContainer.innerHTML = '';

		var rootContainerID = root.htmlContainer.id;
		var newMenuitem = document.createElement('div');
		newMenuitem.id = 'menutree_' + rootContainerID + '_f' + id;
		var btnID = 'menutree_' + rootContainerID + '_fBtn' + id;
		var icoID = 'menutree_' + rootContainerID + '_fIco' + id;
		var txtID = 'menutree_' + rootContainerID + '_fTxt' + id;
		if(this.getMenutree().menuicon)
			{
			var iconfilename = 'http://www.eurostaff.com/en/folder_close.png';
			if(icons.length >= 1)
				iconfilename = icons[0];
			}
		newMenuitem.className = 'menuitembox';
		newMenuitem.innerHTML = '<span id="' + btnID + '" class="menuitemimgbox">' + (this.getMenutree().menubutton? '<img src="' + MenuTree.iconsPath + 'button_bullet.png" border=0>': '') + '</span>' +
		                        '<span id="' + icoID + '" class="menuitemimgbox">' + (this.getMenutree().menuicon? '<img src="' + MenuTree.iconsPath + iconfilename + '" border=0>': '') + '</span>' +
		                        '<span id="' + txtID + '" class="menuitemtextbox menuitemtextbox_unsel menuitemtextbox_' + id + '_unsel" title="' + caption + '">' +
		                        (url? '<a class="menuitemtextbox_unsel menuitemtextbox_' + id + '_unsel" href="' + url + '"' + (target? ' target="' + target + '"': '') + '>' + caption + '</a>': caption) +
		                        '</span>';
		this.htmlContainer.insertBefore(newMenuitem, null);
		var mustExpand = false;
		var f = this;
		do
			{
			if(f.isRoot || mustExpand || !f.isCollapsed())
				{
				mustExpand = true;
				f.expand();
				}
			else
				f.collapse();
			}
		while(!f.isRoot && (f = f.parentMenuitem));

		var newContainer = document.createElement('div');
		newContainer.id = 'menutree_' + rootContainerID + '_c' + id;
		newContainer.className = 'containerbox';
		newContainer.style.display = 'none';
		this.htmlContainer.insertBefore(newContainer, null);

		return this.submenuitems[this.submenuitems.length] = new menuitem(id, caption, icons, url, target, this, newMenuitem, newContainer.id);
		};
	this.unselect = function()
		{
		this.selected = false;
		var f = this;
		do
			{
			if(f.htmlElement != null)
				{
				f.htmlElement.childNodes[MENUITEM_TEXT].className = 'menuitemtextbox menuitemtextbox_unsel menuitemtextbox_' + f.id + '_unsel';
				if(f.htmlElement.childNodes[MENUITEM_TEXT].firstChild)
					f.htmlElement.childNodes[MENUITEM_TEXT].firstChild.className = 'menuitemtextbox_unsel menuitemtextbox_' + f.id + '_unsel';
				}
			}
		while((f = f.parentMenuitem) && !f.isRoot);
		};
	this.select = function(donotexpand)
		{
		if (typeof donotexpand == 'undefined')
			donotexpand = false;
		if(!donotexpand)
			{
			var f = this;
			do
				{
				if(f.isCollapsed() && (f.submenuitems.length > 0))
					f.expand();
				}
			while(!f.isRoot && (f = f.parentMenuitem));
			}
		if(!this.selected)
			{
			var oldSelected = this.getRoot().findSelected();
			if(oldSelected != null)
				oldSelected.unselect();
			this.selected = true;
			}
		var f = this;
		do
			{
			if(f.htmlElement != null)
				{
				var suffix = (f == this? '_sel': '_softsel');
				f.htmlElement.childNodes[MENUITEM_TEXT].className = 'menuitemtextbox menuitemtextbox' + suffix + ' menuitemtextbox_' + f.id + suffix;
				if(f.htmlElement.childNodes[MENUITEM_TEXT].firstChild)
					f.htmlElement.childNodes[MENUITEM_TEXT].firstChild.className = 'menuitemtextbox' + suffix + ' menuitemtextbox_' + f.id + suffix;
				}
			}
		while((f = f.parentMenuitem) && !f.isRoot);
		};
	this.getMenuitemById = function(id)
		{
		if(id == this.id)
			return this;
		for(var i = 0; i < this.submenuitems.length; i++)
			{
			var f = this.submenuitems[i].getMenuitemById(id);
			if(f != null)
				return f;
			}
		return null;
		};
	this.findSelected = function()
		{
		if(this.selected)
			return this;
		for(var i = 0; i < this.submenuitems.length; i++)
			{
			var f = this.submenuitems[i].findSelected();
			if(f)
				return f;
			}
		return null;
		};
	this.getChildIndexById = function(id)
		{
		for(var i = 0; i < this.submenuitems.length; i++)
			{
			if(this.submenuitems[i].id == id)
				return i;
			}
		return -1;
		};
	this.getRoot = function()
		{
		var f = this;
		while(!f.isRoot)
			f = f.parentMenuitem;

		return f;
		};
	this.getMenutree = function()
		{
		if(this.isRoot)
			return MenuTree.getItem(this.htmlContainer.id);

		return this.parentMenuitem.getMenutree();
		};
	return this;
	}

