var groupCount = 0;

function Attributes(element, attrName)
{
	Dispatcher.call(this, "onclick");

	this.elems = new Array;
	this.groupCount = 0;
	this.control = document.getElementById(attrName);

	this.initializeGroups(element, attrName);
}
Attributes.prototype = new Dispatcher;

Attributes.current = null;
Attributes.setup = function ()
{
	Attributes.cleanup();
	Attributes.current = new Object;

	var elems = dom.getElementsByClassName(document, "attributes|bitswitch", "div");
	for (var i = 0; i < elems.length; i++)
	{
		if (elems[i].className.match(/attrib_(\w+)/))
		{
			var attrName = RegExp.$1;
			var attributeControl = new Attributes(elems[i], attrName);

			Attributes.current[attrName] = attributeControl;
		}
	}
}
Attributes.cleanup = function ()
{
	for (var attrName in Attributes.current)
		Attributes.current[attrName].cleanup();
}

Attributes.prototype.cleanup = function ()
{
	this.control = null;
	for (var i = 0; i < this.elems.length; i++)
	{
		this.elems[i].check = null;
		this.elems[i].label = null;
		this.elems[i] = null;
	}
	while (this.elems.length)
		this.elems.shift();
}

Attributes.prototype.initializeGroups = function (node, attrName)
{
	var elems = node.getElementsByTagName("div");
	for (var i = 0; i < elems.length; i++)
		if (css.containsClassName(elems[i], "group"))
			this.initializeGroup(elems[i], attrName);
}

Attributes.prototype.initializeGroup = function (node, attrName)
{
	node.id = "group" + (++groupCount);

	var elems = node.getElementsByTagName("div");
	for (var i = 0; i < elems.length; i++)
		if (css.containsClassName(elems[i], "attribute"))
			this.initializeAttribute(elems[i], attrName, node.id);
}

Attributes.prototype.initializeAttribute = function (node, attrName, groupId)
{
	var value = 0;
	if (node.className.match(/attrib_(\d+)/))
		value = parseInt(RegExp.$1);

	var target = this.control;
	var current = target ? parseInt(target.value) : 0;
	var checked = (current & value) == value;

	if (value != 0 && checked == true)
		css.addClassName(node, "checked");
	else
		css.removeClassName(node, "checked");

	node.check = node.getElementsByTagName("div")[0];
	node.label = node.getElementsByTagName("label")[0];

	node.value = value;
	node.group = groupId;
	node.attrName = attrName;
	node.check.onclick = createCallback(this, "attribute_onclick", [node]);
	node.label.onclick = createCallback(this, "attribute_onclick", [node]);

	this.elems.push(node);
}

Attributes.prototype.setTargetControl = function (controlObj)
{
	this.control = controlObj;
	if (this.control != null)
		this.setValue(this.control.value);
	else
		this.setValue(0);
}

Attributes.prototype.setValue = function (value)
{
	if (this.control != null)
		this.control.value = value;

	var totalValue = parseInt(value);
	for (var i = 0; i < this.elems.length; i++)
	{
		var testValue = parseInt(this.elems[i].value);
		if ((totalValue & testValue) == testValue)
			this.attribute_setTrue(this.elems[i]);
		else
			this.attribute_setFalse(this.elems[i]);
	}
}

Attributes.prototype.attribute_disable = function (elem)
{
	log.message("Disable attribute: " + dom.innerText(elem));

	css.addClassName(elem, "disabled");
}

Attributes.prototype.attribute_enable = function (elem)
{
	css.removeClassName(elem, "disabled");
}

Attributes.prototype.attribute_setTrue = function (elem)
{
	css.addClassName(elem, "checked");
}

Attributes.prototype.attribute_setFalse = function (elem)
{
	css.removeClassName(elem, "checked");
}

Attributes.prototype.attribute_getChecked = function (elem)
{
	return css.containsClassName(elem, "checked");
}

Attributes.prototype.attribute_getDisabled = function (elem)
{
	return css.containsClassName(elem, "disabled");
}

Attributes.prototype.attribute_onclick = function (elem)
{
	if (this.attribute_getDisabled(elem) == true)
		return;

	var checked = !this.attribute_getChecked(elem);
	if (checked)
	{
		this.attribute_setTrue(elem);
		this.attribute_updateGroup(elem.attrName, elem.group, elem.value, true);
	}
	else
	{
		this.attribute_setFalse(elem);
		this.attribute_updateGroup(elem.attrName, elem.group, elem.value, false);
	}
}

Attributes.prototype.attribute_updateGroup = function (attrName, groupId, attrValue, attrPresent)
{
	var group = document.getElementById(groupId);
	var unique = css.containsClassName(group, "unique");
	var totalValue = 0;

	if (unique)
	{
		for (var i = 0; i < group.childNodes.length; i++)
		{
			var value = parseInt(group.childNodes[i].value);
			if (value != attrValue)
				this.attribute_setFalse(group.childNodes[i]);
			else if (attrPresent)
				this.attribute_setTrue(group.childNodes[i]);
			else
				this.attribute_setFalse(group.childNodes[i]);
		}
	}

	for (var i = 0; i < this.elems.length; i++)
	{
		var value = parseInt(this.elems[i].value);
		if (this.attribute_getChecked(this.elems[i]))
			totalValue += value;
	}
	if (this.control)
	{
		this.control.value = totalValue;
	}

	this.fireEvent("onclick", { name: attrName, value: totalValue });
}

evt.addHandler(window, "onready", Attributes.setup);
evt.addHandler(window, "onunload", Attributes.cleanup);
