if (!Pure) var Pure={};

Pure.YearSelector = function(options) {
	this.options = options;
	this.any = $id(options.any);
	this.anyLabel = this.findLabel(this.any);
	this.specific = $id(options.specific);
	this.specificLabel = this.findLabel(this.specific);
	this.start = $id(options.start);
	this.end = $id(options.end);
	this.addBehavior();
	this.updateLabels();
}

Pure.YearSelector.prototype.addBehavior = function() {
	var self = this;
	this.any.onclick = this.specific.onclick = function() {
		self.radioDidChange(this);
	}
	this.start.onchange = this.end.onchange = function() {
		self.selectDidChange(this);
	}
}

Pure.YearSelector.prototype.updateLabels = function() {
	if (this.any.checked) {
		Atira.Element.addClassName(this.anyLabel,this.options.labelClass);
		Atira.Element.removeClassName(this.specificLabel,this.options.labelClass);
	} else {
		Atira.Element.addClassName(this.specificLabel,this.options.labelClass);
		Atira.Element.removeClassName(this.anyLabel,this.options.labelClass);
	}
}

Pure.YearSelector.prototype.radioDidChange = function(element) {
	if (this.any.checked) {
		this.start.selectedIndex=0;
		this.end.selectedIndex=0;
	}
	this.updateLabels();
}

Pure.YearSelector.prototype.selectDidChange = function(element) {
	this.specific.checked=true;
	this.updateLabels();
}


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