if (!Pure) var Pure={};

Pure.CheckboxGroup = function(options) {
	this.options = options;
	this.allCheckbox = $id(options.allId);
	this.checkboxes = $class(options.othersClass);
	this.addBehavior();
	this.ignite();
}

Pure.CheckboxGroup.prototype.ignite = function() {
	var found = false;
	for (var i=0; i < this.checkboxes.length; i++) {
		if (this.checkboxes[i].checked) found=true;
		this.changeCheckbox(this.checkboxes[i],this.checkboxes[i].checked);
	};
	this.changeCheckbox(this.allCheckbox,!found);
}

Pure.CheckboxGroup.prototype.addBehavior = function() {
	var self = this;
	for (var i=0; i < this.checkboxes.length; i++) {
		this.checkboxes[i].onclick = function() {
			self.checkboxHasChanged(this);
		}
	};
	this.allCheckbox.onclick = function() {
		self.allCheckboxHasChanged(this);
	}
}

Pure.CheckboxGroup.prototype.checkboxHasChanged = function(checkbox) {
	this.ignite();
}

Pure.CheckboxGroup.prototype.allCheckboxHasChanged = function(checkbox) {
	if (checkbox.checked) {
		this.changeCheckbox(checkbox,true);
		for (var i=0; i < this.checkboxes.length; i++) {
			this.changeCheckbox(this.checkboxes[i],false);
		};
	} else {
		this.changeCheckbox(checkbox,true);
	}
}

Pure.CheckboxGroup.prototype.changeCheckbox = function(checkbox,checked) {
	checkbox.checked=checked;
	var label = this.findCheckboxLabel(checkbox);
	if (label) {
		if (checked) {
			Atira.Element.addClassName(label,this.options.labelClass);
		} else {
			Atira.Element.removeClassName(label,this.options.labelClass);
		}
	}
}

Pure.CheckboxGroup.prototype.findCheckboxLabel = function(checkbox) {
	if (checkbox.id=='') {
		return;
	}
	var labels = document.getElementsByTagName('label');
	for (var i=0; i < labels.length; i++) {
		if (labels[i].getAttribute('for')==checkbox.id) {
			return labels[i];
		} else if (labels[i].attributes['for'].nodeValue==checkbox.id) {
			return labels[i];
		}
	};
	return null;
}