/**
 *
 * @class ch.exmachina.bravofly.Checkbox
 */
if(!ch){
	var ch = {};
}
if(!ch.exmachina){
	ch.exmachina = {};
}
if(!ch.exmachina.bravofly){
	ch.exmachina.bravofly = {};
}
/**
 * @constructor
 * @description Costruttore della classe ch.exmachina.bravofly.Checkbox
 * @param {Object} ctorArgs Contiene:<br />
 * {String|Node} nodeId
 */
ch.exmachina.bravofly.Checkbox = function(/* object */ctorArgs){
	var nodeId = ctorArgs.nodeId, 
		targetNode = $(nodeId)
	;
	var className = "ta_checkbox_" + ((true == ctorArgs.checked) ? "checked" : "unchecked");
	this.checked = this.initialStatus = !!ctorArgs.checked;
	this.cId = ctorArgs.cId;
	this.value = ctorArgs.value || "";
	var _timestamp = (new Date()).getTime(),
		chkLabel = ctorArgs.label || targetNode.innerHTML,
		template = this.template.replace(/{\$CheckboxId}/g, _timestamp)
		.replace(/{\$CheckboxLabel}/, chkLabel)
		.replace(/{\$ClassName}/, className)
	;
		
	targetNode.innerHTML = template;
	
	this.domNode = $("node" + _timestamp);
	this.domNode.writeAttribute("id", "node" + _timestamp + "" + (++ch.exmachina.bravofly.Checkbox._ncboxes.count));
	//this.domNode.writeAttribute("id", null);

	this.onClick = ctorArgs.onClick || function(){};
	
	this.domNode.observe("click", this._onclick.bindAsEventListener(this));
	ch.exmachina.bravofly.Checkbox._cboxes[ctorArgs.cId] = this;
	ch.exmachina.bravofly.Checkbox._ncboxes["node" + _timestamp + "" + ch.exmachina.bravofly.Checkbox._ncboxes.count] = this;
	
};
/**
 * @static
 * @returns Oggetto contenente tutte le checkbox: la chiave per ognuna sara' l'id
 */
ch.exmachina.bravofly.Checkbox.checkBoxes = function(){
	return ch.exmachina.bravofly.Checkbox._cboxes; 
};

ch.exmachina.bravofly.Checkbox.prototype = {
	/**
	 * @type {String}
	 * @description Template della checkbox, con segnaposti per id e className
	 */
	template: '<div id="node{$CheckboxId}" class="{$ClassName}"> {$CheckboxLabel}</div>',
	/**
	 * @type {Boolean}
	 * @description Stato della checkbox
	 * @default false
	 */
	checked: false,
	/**
	 * @type {Boolean}
	 * @description Stato di abilitazione della checkbox, di default e' abilitata
	 * @default false
	 */
	disabled: false,
	/**
	 * @type {Number}
	 * @description Id di questa istanza
	 */
	cId: 0,
	/**
	 * @type {Boolean}
	 * @description Stato iniziale della checkbox, applicato dal costruttore; ha lo stesso significato di checked
	 * @default false
	 */
	initialStatus: false,
	/**
	 * @private
	 * @description Gestore dell'evento onclick. Si occupa del cambiamento di stato dell'oggetto (cheched/unchecked),
	 * di modificarne l'aspetto di conseguenza e di chiamare l'eventuale gestore specificato dall'utente
	 */
	_onclick: function(){
		if(this.disabled){ return }
		this.toggleCheck();
		this.onClick.call(this);
	},
	/**
	 * @description Restituisce una stringa contenente id e attributo checked per questa checkbox
	 */
	toString: function(){
		return "id = " + this.cId + "\nclass = ch.exmachina.bravofly.CheckBox\nchecked = " + this.checked;
	},
	/**
	 * @description hook collegato a onclick sulla checkbox, sovrascrivibile dall'utente in fase di costruzione (ma
	 * anche dopo, e' sufficiente riassegnare la proprieta')
	 */
	onClick: function(){
		// hook
	},
	/**
	 * @description Mette checked a <b>true</b> e modifica l'aspetto dell'oggetto di conseguenza. Duale di
	 * {@link ch.exmachina.bravofly.Checkbox#uncheck}
	 */
	check: function(){
		this.checked = true;
		this.domNode.className = "ta_checkbox_checked";
	},
	/**
	 * @description Mette checked a <b>false</b> e modifica l'aspetto dell'oggetto di conseguenza. Duale di
	 * {@link ch.exmachina.bravofly.Checkbox#check}
	 */
	uncheck: function(){
		this.checked = false;
		this.domNode.className = "ta_checkbox_unchecked";
	},
	/**
	 * @description Metodo generico per impostare lo stato della checkbox corrente
	 * @param {Boolean} cStatus lo stato di questo oggetto
	 * @see ch.exmachina.bravofly.Checkbox#check
	 * @see ch.exmachina.bravofly.Checkbox#uncheck
	 */
	setStatus: function(cStatus){
		this.checked = cStatus;
		this.domNode.className = "ta_checkbox_" + ((this.checked) ? "checked" : "unchecked");
	},
	/**
	 * @description Riporta lo stato della checkbox a quello originario assegnato subito dopo la costruzione
	 */
	reset: function(){
		this.setStatus(this.initialStatus);		
	},
	/**
	 * @description Disabilita la checkbox, occupandosi anche della modifica dell'aspetto
	 * @see ch.exmachina.bravofly.Checkbox#enable
	 * @see ch.exmachina.bravofly.Checkbox#disabled
	 */
	disable: function(){
		this.disabled = true;
		this.domNode.className = "ta_checkbox_disabled";
	},
	/**
	 * @description Abilita la checkbox, occupandosi anche della modifica dell'aspetto
	 * @see ch.exmachina.bravofly.Checkbox#disable
	 * @see ch.exmachina.bravofly.Checkbox#disabled
	 */
	enable: function(){
		this.disabled = false;
		this.domNode.className = "ta_checkbox_" + ((this.checked) ? "checked" : "unchecked");
	},
	/**
	 * @description Inverte lo stato della checkbox
	 * @see ch.exmachina.bravofly.Checkbox#enable
	 * @see ch.exmachina.bravofly.Checkbox#disable
	 */
	toggleCheck: function(){
		this.checked = !this.checked;
		this.domNode.className = "ta_checkbox_" + ((this.checked) ? "checked" : "unchecked");
	}
};

// static members and methods
/**
 * @private
 * @description Insieme di tutte le checkbox registrate
 */
ch.exmachina.bravofly.Checkbox._cboxes = {};
/**
 * @private
 * @description Insieme dei nodi di tutte le checkbox registrate
 */
ch.exmachina.bravofly.Checkbox._ncboxes = {};
/**
 * @private
 * @description Contatore delle checkbox presenti
 */
ch.exmachina.bravofly.Checkbox._ncboxes.count = 0;
/**
 * @description Funzione per arrivare all'oggetto checkbox dato l'id
 * @returns L'oggetto con l'id specificato
 * @param {String} objId Id della checkbox richiesta
 */
ch.exmachina.bravofly.Checkbox.byId = function(/** String */ objId){
	return ch.exmachina.bravofly.Checkbox._cboxes[objId];
};
/**
 * @description Funzione per arrivare al nodo DOM della checkbox, dato l'id
 * @returns Il nodo la cui checkbox ha l'id specificato
 * @param {String} objId Id della checkbox il cui nodo e' stato richiesto
 */
ch.exmachina.bravofly.Checkbox.byNodeId = function(/** String */ objId){
	return ch.exmachina.bravofly.Checkbox._ncboxes[objId];
};
