/*
@Author: Mohsen Taleb (http://www.mohsentaleb.com)
Origimal idea by Maik Vlcek (http://www.mediavrog.net)
******************************************************/

var customFormElements = new Class({
	
	initialize: function(options){
	
		this.setOptions({
			scope: false,
			spacer: "../../images/spacer.gif"
		}, options);
		
		this.chboxValues = [];  /* The main array, holds everything about the checkboxes */
		
		/* build aliases for checkboxes */
		$ES("input[type=checkbox]",this.options.scope || document.body).each(function(el, index){

			var _chbtemp = [];
			/* create main checkbox alias */
			_chbtemp["ref"] = new Element("span",{"class": "jsCheckbox"}).injectBefore(el).adopt(new Element("img",{"src": this.options.spacer}));

			/* pushing the references */
			_chbtemp["value"] = el.getProperty("value").toInt();
			_chbtemp["selected"] = false;
			
			this.chboxValues.push(_chbtemp);
			
			this.chboxValues[index].ref.input = el.setStyle("display","none");
			this.chboxValues[index].ref.index = index;
			this.chboxValues[index].ref.input.checked?this.chboxValues[index].ref.addClass("jsCheckboxActive"):"";

			
			/* Attaching events to main checkbox alias */
			this.chboxValues[index].ref.addEvents({"mouseover": this.cbHover.bind(this.chboxValues[index].ref),"mouseout": this.cbMouseOut.bind(this.chboxValues[index].ref),"click":this.cbClicked.bind(this.chboxValues[index].ref, this)});

			/* Attaching label to alias */
			this.chboxValues[index].ref.label = $E("label[for="+el.getProperty("id")+"]").removeProperty("for").addClass("for_"+el.getProperty("id")+" jsCheckboxLabel").addEvents({"mouseover": this.cbHover.bind(this.chboxValues[index].ref),"mouseout": this.cbMouseOut.bind(this.chboxValues[index].ref),"click":this.cbClicked.bind(this.chboxValues[index].ref, this)}) || false;

		}.bind(this));
		
	},
	getChboxValues: function() {
		return this.chboxValues;
	},
	/* Events for checkboxes */
	cbHover: function(){
		this.addClass(this.input.checked?"jsCheckboxHoverActive":"jsCheckboxHover");
		this.label?this.label.addClass("jsCheckboxLabelHover"):"";
	},
	cbMouseOut: function(){
		this.removeClass("jsCheckboxHoverActive").removeClass("jsCheckboxHover").removeClass(this.input.checked!=true?"jsCheckboxActive":"");
		this.label?this.label.removeClass("jsCheckboxLabelHover"):"";
	},
	cbClicked: function(theClass){
		this.fireEvent("mouseover");

		if(this.input.checked==true){
			theClass.cbChangeStatus(this.index, false);
			this.input.removeProperty("checked");
			this.removeClass("jsCheckboxActive").removeClass("jsCheckboxHoverActive");
			this.addClass("jsCheckboxHover");
			
		}
		else{
			theClass.cbChangeStatus(this.index, true);
			this.input.checked = true;
			this.addClass("jsCheckboxActive jsCheckboxHoverActive");
		}
		SamService.sncRefreshView(theClass.chboxValues)
	},
	cbForceCheck: function(index) {
		this.chboxValues[index].selected = true;
		this.chboxValues[index].ref.input.checked = true;
		this.chboxValues[index].ref.addClass("jsCheckboxActive");
	},
	cbForceUncheck: function(index) {
		this.chboxValues[index].selected = false;
		this.chboxValues[index].ref.input.removeProperty("checked");
		this.chboxValues[index].ref.removeClass("jsCheckboxActive");
		
	},
	cbGroupSelectorInit: function(selector) {
		this.predefined = $$('#predefined li a');
		if ( this.predefined != null ) {
			this.predefined.each(function(el, index){
				this.predefined[index].specs = this.predefined[index].getElement('span').getText().split(',');
				for (var i = 0; i < this.predefined[index].specs.length; i++) {
					this.predefined[index].specs[i] = parseInt(this.predefined[index].specs[i]);
				}
				this.predefined[index].addEvent('click', function(e){
					e = new Event(e).stop();
					this.predefined.each(function(a){a.removeClass('active');});
					this.predefined[index].addClass('active');
					this.cbForceGroup(this.predefined[index].specs);
				}.bind(this));
			}.bind(this));
		}		
	},
	
	cbForceGroup: function(forcedArray) {
		this.chboxValues.each(function(checkbox, index){
			this.cbForceUncheck(index);
		}.bind(this));
		for (var i = 0; i < forcedArray.length; i++) {
			this.cbForceCheck(forcedArray[i]-1);
		}
		SamService.sncRefreshView(this.chboxValues);
	},
	cbResetAll: function() {
		this.chboxValues.each(function(checkbox, index){
			this.cbForceUncheck(index);
		}.bind(this));
		this.predefined.each(function(a){a.removeClass('active');});
		SamService.sncRefreshView(this.chboxValues);
	},
	cbChangeStatus: function(index, state) {
		this.chboxValues[index].selected = state;
	}
});

customFormElements.implement(new Options,new Events);