// KJSLib SelectionBar
if (typeof __KJSLib_SelectionBar == "undefined")
{
var __KJSLib_SelectionBar = true;
//--- start of include guard

var kSelectionBar = {
	create : function(parent, id, separatorWidth, flags)
	{
		var doc = parent.ownerDocument;
		var bar = doc.createElement("table");
		bar.id = id;
		bar.border = "0";
		bar.cellPadding = "0";
		bar.cellSpacing = "0";
		var barBody = doc.createElement("tbody");
		bar.appendChild(barBody);
		var barTr = null;
		if (!(flags & kSelectionBar.VERTICAL))
		{
			barTr = doc.createElement("tr");
			barBody.appendChild(barTr);
		}
		bar.__private = {
			flags : flags,
			separatorWidth : separatorWidth,
			body : barBody,
			tr : barTr
		}
		kBindable.bind.call(this, bar);
		parent.appendChild(bar);
		return bar;
	},
	//  flag constants
	VERTICAL : 0x01, // horizontal if this bit is 0

	prototype : {
		appendItem : function(id, itemPrototype, bindArgs)
		{
			var doc = this.ownerDocument;
			var itemParent;
			var itemCount = this.getItems().length;
			if (itemCount > 0 && this.__private.separatorWidth > 0)
			{
				if (this.__private.flags & kSelectionBar.VERTICAL)
				{
					itemParent = doc.createElement("tr");
					this.__private.body.appendChild(itemParent);
				}
				else
				{
					itemParent = this.__private.tr;
				}
				var item = doc.createElement("td");
				item.id = this.id + "_sep_" + id;
				if (this.__private.flags & kSelectionBar.VERTICAL)
				{
					item.style.height = String(this.__private.separatorWidth) + "px";
				}
				else
				{
					item.style.width = String(this.__private.separatorWidth) + "px";
				}
				itemParent.appendChild(item);
			}

			if (this.__private.flags & kSelectionBar.VERTICAL)
			{
				itemParent = doc.createElement("tr");
				this.__private.body.appendChild(itemParent);
			}
			else
			{
				itemParent = this.__private.tr;
			}
			var item = doc.createElement("td");
			item.id = id;
			itemParent.appendChild(item);
			bindArgs.unshift(item);
			itemPrototype.bind.apply(itemPrototype, bindArgs);
			item.__private.owner = this;
			item.render();
			return item;
		},
		removeItem : function(id)
		{
			var childNodes;
			if (this.__private.flags & kSelectionBar.VERTICAL)
			{
				childNodes = this.__private.body.childNodes;
			}
			else
			{
				childNodes = this.__private.tr.childNodes;
			}
			var i, item;
			for (i = 0; i < childNodes.length; ++i)
			{
				if (this.__private.flags & kSelectionBar.VERTICAL)
				{
					item = childNodes[i].childNodes[0];
				}
				else
				{
					item = childNodes[i];
				}
				if (item.id == id)
				{
					var sepItem;
					if (this.__private.separatorWidth <= 0 || childNodes.length == 1)
					{
						sepItem = null;
					}
					else if (i == 0)
					{
						sepItem = childNodes[1];
					}
					else
					{
						sepItem = childNodes[i - 1];
					}
					if (item.onremove)
					{
						item.onremove();
					}
					this.__private.body.removeChild(childNodes[i]);
					if (sepItem != null)
					{
						this.__private.body.removeChild(sepItem);
					}
					item.__private.owner = null;
					break;
				}
			}
		},
		getItem : function(id)
		{
			var items = this.getItems();
			var i;
			for (i = 0; i < items.length; ++i)
			{
				if (items[i].id == id)
				{
					return items[i];
				}
			}
			return null;
		},
		getItems : function()
		{
			var items = [];
			var childNodes;
			if (this.__private.flags & kSelectionBar.VERTICAL)
			{
				childNodes = this.__private.body.childNodes;
			}
			else
			{
				childNodes = this.__private.tr.childNodes;
			}
			var prefix = this.id + "_sep_";
			var i, item;
			for (i = 0; i < childNodes.length; ++i)
			{
				if (this.__private.flags & kSelectionBar.VERTICAL)
				{
					item = childNodes[i].childNodes[0];
				}
				else
				{
					item = childNodes[i];
				}
				if (item.id && item.id.substr(0, prefix.length) != prefix)
				{
					items.push(item);
				}
			}
			return items;
		},

		onitemselect : function(item)
		{
			var items = this.getItems();
			var i;
			for (i = 0; i < items.length; ++i)
			{
				if (item.id != items[i].id && items[i].onotheritemselect)
				{
					items[i].onotheritemselect(item);
				}
			}
		}
	}
}

//--- end of include guard
}
