/*
RadioBox.js

License:
	MIT-style license.
	
Written for 2Po.eu
*/
var RadioBox = new Class({
	options: {
		mediaRoot: '',
		radioImageTrue: '/theme/i/checkbox_true.gif',
		radioImageFalse: '/theme/i/checkbox_false.gif',
		display: -1,
		noHide: true
	},
	initialize: function(elem,name,container,options){
		this.elements = [];
		this.setOptions(options);
		this.parent = elem;
		this.container = container;
		this.name = name;
		this.hidden = new Element('input',
		{
			'type' : 'hidden',
			'name' : this.name,
			'value' : ''	
		}).inject(this.parent);
		this.container.each(this.build,this);
		if (this.options.display>-1) this.toggle(this.options.display);
	},
	build: function(el,i) {
		img = new Element('img',
		{
			'align' : 'absmiddle',
			'alt' : '',
			'class' : 'cRadio',
			'src' : this.options.mediaRoot+this.options.radioImageFalse	
		}).inject(this.parent).addEvent('click',this.toggle.bind(this,i));
		label = new Element('label',{'class':'cRadio'}).set('text',this.container[i][1]).inject(this.parent).addEvent('click', this.toggle.bind(this,i));
		this.elements[i] = img;
		
	},
	toggle: function(i){
		if ((this.options.display==i) && (!this.options.noHide)) {
			this.hidden.value = '';
			this.elements[i].src = this.options.mediaRoot+this.options.radioImageFalse;
			this.options.display=-1;
		}else {
			if (this.options.display>-1)
				this.elements[this.options.display].src = this.options.mediaRoot+this.options.radioImageFalse;
			this.elements[i].src = this.options.mediaRoot+this.options.radioImageTrue;
			this.options.display = i;
			this.hidden.value = this.container[this.options.display][0];
		}
	}
});
RadioBox.implement(new Events, new Options);
