/**
* CHECKBOX OF DOOM (Double Object Option Marker!) 
* @author ade.se
* @url http://ade.se
* ade at ade dot S to the E
* @copyright ade.se 2009
**/

if(typeof(Prototype) == "undefined")
	throw "DoomBox requires the Prototype javascript library to be loaded.";
	
var DOOMBOX_groups = new Object();
	
var DoomBox = Class.create({
	initialize: function(container,options){
		this.container = $(container);
		this.displayValue = false;
		this.options = {
			template: 'doombox_template',  //The container to copy the .on and .off div templates from. (<div class="on">...)
			labelclass: 'doombox_label',  //An element whose innerHTML will be REPLACED by the value below
			text: '',     //Label inserted into labelclass containers
			text_on: '',  //Label inserted into labelclass container when activated
			text_off: '', //Label inserted into labelclass container when deactivated
			value: false, //Initial value.
			group: null, //Member of a group? Set to a group id. Format: string.
			trigger_on_deactivate: false, //Fire afterChange on deactivation?
			sticky: false, //Disable selection of zero elements. You'll have to set up the initial case yourself.
			afterChange: Prototype.emptyFunction //Function to be executed after value has changed
		};
		Object.extend(this.options,options || {});
		
		//Set toggle event
		Event.observe(this.container, 'click', this.click.bindAsEventListener(this));
		
		//Get input element
		this.input = this.container.down('input');
		this.input.hide();
		
		//Radio compability - create a group automatically if none was specified
		if(this.input.readAttribute('type') == 'radio') {
			if(this.options.group == null) {
				this.options.group = this.input.readAttribute('name');	
			}
		}
		
		//Grouping
		if(this.options.group != null) {
			if(DOOMBOX_groups[this.options.group] == null) {
				DOOMBOX_groups[this.options.group] = new Array();
			}
			DOOMBOX_groups[this.options.group].push(this);
		}		
		
		//Copy template
		this.input.insert({'before': $(this.options.template).innerHTML});

		//Get option objects
		this.ondiv = this.container.down('div.on');
		this.offdiv = this.container.down('div.off');
		
		//Set text label
		if(this.options.text != '') {
			this.ondiv.down('div.'+this.options.labelclass).update(this.options.text);
			this.offdiv.down('div.'+this.options.labelclass).update(this.options.text);
		}
		
		//Special on off labels
		if(this.options.text_on != '')
			this.ondiv.down('div.'+this.options.labelclass).update(this.options.text_on);
		if(this.options.text_off != '')	
 			this.offdiv.down('div.'+this.options.labelclass).update(this.options.text_off);
 		
 		//Set cursor
 		this.container.setStyle({cursor: 'pointer'});
		
		//Set initial value
		if(this.options.value != false)
			this.setValue(this.options.value, false);
		else
			this.setValue(this.input.checked, false);
	},
	setValue: function(value, trigger_afterchange){
		this.value = value;
		this.input.checked = (this.value == false ? false : true);
		this.render(value);
		
		//Grouping
		if (this.options.group != null && value == true) {
			this.deactivateGroup(false);
		}
		
		if(trigger_afterchange == true)
			this.options.afterChange(this);
	},
	getValue: function() {
		return this.input.checked;
	},
	render: function(value) {
		value = (value == undefined ? this.input.value : value); 
		this.displayValue = value;
		if(value != false) {
			this.offdiv.hide();
			this.ondiv.show();
		} else {
			this.ondiv.hide();
			this.offdiv.show();
		}
	},
	click: function(event){
		var trigger_event = true;
		if(this.getValue() && !this.options.trigger_on_deactivate)
			trigger_event = false;
		this.setValue(this.value == false ? true : false, trigger_event);
		Event.stop(event);
		return false;
	},
	deactivateGroup: function(include_this) {
		for (var i = 0; i < DOOMBOX_groups[this.options.group].length; i++) {
			grouped_doombox = DOOMBOX_groups[this.options.group][i];
			if(grouped_doombox != this || include_this) {
				if(grouped_doombox.displayValue == true) {
					grouped_doombox.setValue(false, this.options.trigger_on_deactivate);
				}
			}
		}
	}
});