/* 
 * Copyright © 2007 Touring It Logistic | developer: Mugurel Mirica mmugur81@yahoo.com
 */

//-- OBIECTE ---------------------------------------------------------------------------------------------------
var SEP = ','; //values separator for type == 'multi'

function jsPushButton(p_text, p_value, p_classOff, p_classOn, p_bUseDefClassOn) {
	//all memberes -> string, with some exceptions
	this.index			= -1;//set by jsPushButtonGroup
	this.text			= p_text;
	this.value			= p_value;
	this.classOff		= p_classOff;
	this.classOn		= p_classOn;
	this.bUseDefClassOn	= p_bUseDefClassOn;//bool
	this.checked		= false;
}


var aGroups = new Array();//all jsPushButtonGroup objects are saved here too

function jsPushButtonGroup(p_name, p_container, p_fnCallback){
	//all memberes -> string, with some exceptions
	this.name 			= p_name;	//the name of the hidden input with the final value 
	this.type 			= 'single';	//can be: 'single' or 'multi'	 
	this.container		= p_container;//HTML object that will host the button group
	this.useSpacer		= true;//BOOL: a space between buttons
	this.spacer			= ' ';//the text use as spacer
		
	//each time a button is pressed a function is called
	//the function is passed 2 arguments: this.name and value of button
	this.fnCallback		= (p_fnCallback)? p_fnCallback : pb_defCallback;	
	
	//internal members
	this.aButtons		= new Array();
	
	//methods
	this.add = pb_add;
	this.reset = pb_reset;
	this.refreshValues = pb_refreshValues;
	this.switchAll = pb_switchAll;
	
	//init code
	
	//[1]creation oh hiden input with final value/values
	var ctrl = document.createElement('input');
	ctrl.setAttribute('type', 'hidden');
	ctrl.setAttribute('name', this.name);
	ctrl.setAttribute('id', this.name);
	this.container.appendChild(ctrl);
	this.idHidden = this.name;
	
	aGroups.push(this);
}

function jsPushButtonGroupMulti(p_name, p_container, p_fnCallback){
	var tmp_jsBtnGrp = new jsPushButtonGroup(p_name, p_container, p_fnCallback);
	tmp_jsBtnGrp.type = 'multi';
	return tmp_jsBtnGrp;
}

/**
* @method add
* @param {jsPushButton} pushBtn Button to be added to the group
*/
function pb_add(pushBtn){
	//[1] add to the array
	this.aButtons.push(pushBtn);
	index = this.aButtons.length - 1;
	this.aButtons[index].index = index;
	
	//[2]create the object itself
	//[a] spacer
	if (index > 0 && this.useSpacer){
		//tn = document.createTextNode(this.spacer);
		tn = document.createElement('span');
		tn.innerHTML = this.spacer;
		this.container.appendChild(tn);
	}
	
	//[b] object
	//var ctrl = document.createElement('label');
	var ctrl = document.createElement('input');
	ctrl.setAttribute('type', 'button');
	ctrl.setAttribute('value', pushBtn.text);
	
	ctrl.setAttribute('id', this.name+'|'+index);
	ctrl.className = pushBtn.classOff;
	ctrl.style.padding = '2px';
	ctrl.onclick = pb_clickButton;
	
	this.container.appendChild(ctrl);
	
	return ctrl;
}

function pb_reset(){
	//dprint('aButtons = '+this.aButtons);
	for(var i=0; i<this.aButtons.length; i++){
		if (this.aButtons[i].checked){
			pb_uncheckButton(this, i);
		}
	}
}

function pb_clickButton(){
	var aTmp = this.id.split('|');
	
	var k = pb_searchGroup(aTmp[0]);
	//dprint('pb_clickButton k = ['+k+']');
	if (k > -1){
		if (aGroups[k].type == 'single') {
			//uncheck other buttons
			for(var j=0; j<aGroups[k].aButtons.length; j++){
				pButton = aGroups[k].aButtons[j];
				if (pButton.checked){
					pb_uncheckButton(aGroups[k], j);
				}
			}
		}
		
		//check/uncheck current
		if(aGroups[k].aButtons[aTmp[1]].checked)
			pb_uncheckButton(aGroups[k], aTmp[1]);
		else
			pb_checkButton(aGroups[k], aTmp[1]);
	}
}

function pb_checkButton(obGrp, index){
	obGrp.aButtons[index].checked = true;
	//visual styles
	sIdButton = obGrp.name +'|'+ index
	if (getById(sIdButton))
		getById(sIdButton).className = obGrp.aButtons[index].classOn;
	//callback for ON
	obGrp.fnCallback(obGrp.name, obGrp.aButtons[index].value, true);
	obGrp.refreshValues();
}

function pb_uncheckButton(obGrp, index){
	obGrp.aButtons[index].checked = false;
	//visual styles
	sIdButton = obGrp.name +'|'+ index
	if (getById(sIdButton))
		getById(sIdButton).className = obGrp.aButtons[index].classOff;
	//callback for OFF - only for 'multi'
	if (obGrp.type == 'multi')
		obGrp.fnCallback(obGrp.name, obGrp.aButtons[index].value, false);
	obGrp.refreshValues();
}

function pb_switchAll(bSwitch) {
	for(var i=0; i<this.aButtons.length; i++){
		if (this.aButtons[i].checked == bSwitch)
			continue;
		
		if (bSwitch)
			pb_checkButton(this, this.aButtons[i].index);
		else
			pb_uncheckButton(this, this.aButtons[i].index);
	}
	obGrp.refreshValues();
}

function pb_refreshValues() {
	var sValues = '';
	for(var i=0; i<this.aButtons.length; i++){
		if (this.aButtons[i].checked){
			if (sValues) sValues += SEP;
			sValues += this.aButtons[i].value;
		}
	}
	getById(this.idHidden).value = sValues;
	//dprint('pb_refreshValues: '+getById(this.idHidden).id+' = '+sValues);
}

function pb_searchGroup(p_name){
	for(var j=0;j<aGroups.length;j++){
		if(aGroups[j].name == p_name)
			return j;
	}
	return -1;
}

function pb_getGroup(p_name) {
	for(var j=0;j<aGroups.length;j++){
		if(aGroups[j].name == p_name)
			return aGroups[j];
	}
	return -1;
}

function pb_defCallback(a, b, c) {
	return;
}
