
//=====================================================================================================//
function CFMPropMod(id) {
	if (this == window)
		return new CFMPropMod(id);
	if (id === undefined)
		this.init(CFMPropMod.__getunicid());
	else 
		if  (CFMPropMod.__items[id] === undefined){
			this.init(id);
		}else 
			return CFMPropMod.__items[id];
};
//=====================================================================================================//
if (CFMPropMod.__items === undefined)
	CFMPropMod.__items = new Array();
//=====================================================================================================//
if (CFMPropMod.__maxvalue === undefined)
	CFMPropMod.__maxvalue = 0;
//=====================================================================================================//
CFMPropMod.__getunicid = function(){
	while (CFMPropMod.__items['aid_'+CFMPropMod.__maxvalue] !== undefined)
		CFMPropMod.__maxvalue++;
	return 'aid_'+CFMPropMod.__maxvalue;
}

//CFMPropMod("145_1").dPropReg("length","11");	











//=====================================================================================================//
CFMPropMod.prototype.init = function(_id){ // инициализатор
	this.id = _id;

	CFMPropMod.__items[this.id] = this;
	this.clean();
	
	//this.log('Init '+this.id);
}
//=====================================================================================================//
CFMPropMod.prototype.log = function(msg){ // логгер
	alert(msg);
}
//=====================================================================================================//
CFMPropMod.prototype.clean = function(id){ // очистить список модификаторов свойств
	this.mods 		= new Array ();
	this.defProp 	= new Array ();
	this.curProp 	= new Array ();
	this.tmpProp 	= new Array ();
	this.koeffs 	= new Array ();
	this.evOnChange	= new Array ();	
	this.realnames	= new Array ();	
}
//=====================================================================================================//
// добавить модификатор списка свойств
CFMPropMod.prototype.modReg = function(_mod){ 
	var mod = new Array();
	
	mod.prop	=	'_'+_mod[0];	mod.usl		=	_mod[1];
	mod.value	=	_mod[2];	mod.koeff 	=	'_'+_mod[3];
	mod.motion	=	_mod[4];	mod.kvalue	=	_mod[5];

	this.mods.push(mod);
}
//=====================================================================================================//
// добавить значение свойства по умолчанию
CFMPropMod.prototype.dPropReg = function(name, value){ 
	this.realnames['_'+name] = name;
	name = '_'+name;
	this.defProp[name] = value;
	this.curProp[name] = value;
	this.tmpProp[name] = value;
}
//=====================================================================================================//
// добавить значение свойства по умолчанию
CFMPropMod.prototype.dKPropReg = function(name, value){ 
	this.realnames['_'+name] = name;
	this.koeffs['_'+name] = value;
	this.tmpProp['_'+name] = value;
}
//=====================================================================================================//
// добавить значение свойства по умолчанию
CFMPropMod.prototype.regOnChange = function(name, proc){ 
	this.evOnChange['_'+name] = proc;
}



//=====================================================================================================//
// задать значение свойства(коэффициента)
CFMPropMod.prototype.setPropValue = function(name, value){  // осторожно ... имя имеет вид  _<короткое имя>
	var item;
	if (item = document.getElementById(this.id +  name))
		if (item.innerHTML != value){
			item.innerHTML = value;
			if (this.evOnChange[name] !== undefined)
				this.evOnChange[name].call(this, this.id, this.realnames[name]);
		}
}
//=====================================================================================================//
// получить значение свойства(коэффициента)
CFMPropMod.prototype.getProp = function(name){  // осторожно ... имя имеет вид  _<короткое имя>
	/////////////////////////////////////////////
	if (this.tmpProp['_'+name] === undefined)
		return '';
	else 
		return this.tmpProp['_'+name];
}
//=====================================================================================================//
// изменилось состояние свойства //TODO баг с tmpProp. Там только коэффициенты

CFMPropMod.prototype.onPropChange = function(name, value){ 

	name = '_' + name;
	if (this.evOnChange[name] !== undefined)
			this.evOnChange[name].call(this, this.id, this.realnames[name]);	
	//this.log(name + ' ' + value);	
	if ((this.curProp[name] !== undefined) && (this.curProp[name] == value))
			return;
	this.curProp[name] = value;

	this.tmpProp = new Array();

	for (var i in this.curProp)
			this.tmpProp[i] = this.curProp[i];
		
	fAndNo = false;
	for (var i in this.mods){
		var mod = this.mods[i];
		
		
		if ((fAndNo) && (mod.motion != 100)){
			fAndNo = false;
			continue;
		} else if (fAndNo)
				continue;

		if (this.usl[mod.usl] === undefined)
			continue;
		var f = this.usl[mod.usl];

		if (f(mod.usl, mod, this.tmpProp)){

			if (mod.motion == 100)
				continue;	
			if (this.datamod[mod.motion] === undefined)
				continue;
			f = this.datamod[mod.motion];
			f.call(this, mod.motion, mod, this.tmpProp);
			continue;
		} else
			if (mod.motion == 100){//кто-то из логического И вернул ложь
				fAndNo = true;
			}

	}
	
	// ищем изменённые коэффициенты
	for (var i in this.koeffs ){
			this.setPropValue(i, this.tmpProp[i]);
	}
	
	
}
//=====================================================================================================//
//Условия
CFMPropMod.prototype.usl = new Array();
CFMPropMod.prototype.usl[1] = function(usl, mod, res){ //равно
	//alert(res);
	//alert('равно '+res[mod.prop]+' и '+mod.value+'?');
	if (res[mod.prop] == mod.value)
		return true; else return false;
}
CFMPropMod.prototype.usl[2] = function(usl, mod, res){ // не равно
	if (res[mod.prop] != mod.value, res)
		return true; else return false;
}
CFMPropMod.prototype.usl[3] = function(usl, mod, res){ // больше
	if (res[mod.prop] > mod.value)
		return true; else return false;

}
CFMPropMod.prototype.usl[4] = function(usl, mod, res){ // меньш
	if (res[mod.prop] < mod.value)
		return true; else return false;	

}
//=====================================================================================================//
//Действия
//=====================================================================================================//
CFMPropMod.prototype.datamod = new Array();
CFMPropMod.prototype.datamod[1] = function(usl, mod, res){ //присвоить
	res[mod.koeff] = mod.kvalue;
}
CFMPropMod.prototype.datamod[2] = function(usl, mod, res){ // прибавить
	res[mod.koeff] += mod.kvalue;
}

CFMPropMod.prototype.datamod[3] = function(usl, mod, res){ // вызвать js функцию
	eval("if ("+mod.kvalue+") "+mod.kvalue+"('"+this.id+"');" );
}

// Изменилось состояние контрола
CFMPropMod.prototype.onControlChange = function(pointer){ 
	if ((pointer.name === undefined)||(pointer.value === undefined))
		return;
	this.onPropChange(pointer.name, pointer.value);
}
//=====================================================================================================//







