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

//-- OBIECTE ---------------------------------------------------------------------------------------------------

function jsPushButton(p_text, p_value, p_classOff, p_classOn, p_bUseDefClassOn) {
	//all memberes -> string, with some exceptions
	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.container		= p_container;//HTML object that will host the button group
		
	//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;	
	
	//internal members
	this.aButtons		= new Array();
	
	//methods
	this.add = pb_add;
	this.reset = pb_reset;
	
	//init code
	
	//[1]creation oh hiden input
	var ctrl = document.createElement('input');
	ctrl.setAttribute('type', 'hidden');
	ctrl.setAttribute('name', this.name);
	ctrl.setAttribute('id', this.name);
	this.container.appendChild(ctrl);
	this.obHidden = ctrl;
	
	aGroups.push(this);
}

/**
* @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;
	
	//[2]create the object itself
	//[a] text separator
	if (index > 0){
		tn = document.createTextNode(' ');
		this.container.appendChild(tn);
	}
	
	//[b] object
	var ctrl = document.createElement('label');
	ctrl.setAttribute('id', this.name+'|'+index);
	ctrl.className = pushBtn.classOff;
	ctrl.style.padding = '2px';
	ctrl.onclick = pb_clickButton;
	
	//[c] text node
	tn = document.createTextNode(pushBtn.text);
	ctrl.appendChild(tn);
	
	this.container.appendChild(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){
		//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 current
		pb_checkButton(aGroups[k], aTmp[1]);
	}
}

function pb_checkButton(obGrp, index){
	obGrp.aButtons[index].checked = true;
	//set the value to the hidden button
	obGrp.obHidden.value = obGrp.aButtons[index].value;
	//visual styles
	sIdButton = obGrp.name +'|'+ index
	getById(sIdButton).className = obGrp.aButtons[index].classOn;
	//callback
	obGrp.fnCallback(obGrp.name, obGrp.aButtons[index].value);
}

function pb_uncheckButton(obGrp, index){
	obGrp.aButtons[index].checked = false;
	//set the value to the hidden button
	obGrp.obHidden.value = 0;
	//visual styles
	sIdButton = obGrp.name +'|'+ index
	getById(sIdButton).className = obGrp.aButtons[index].classOff;
}

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

