/**
 * Библиотека FLINE (FonBet Line)
 */

/**
 * Базовый класс для всех котировок
 */

function FLINE_Factor(){
  this.storage = null;
	this.id = null;
	this.eventId = null;
	this.type = null;
	this.typeId = 0;
	this.blocked = false;
	this.value = 0;
	this.valueStatus = this.STATUS_DISABLED;
	this._valueCleanUpdateStatusID = null;
}
FLINE_Factor.prototype.STATUS_DISABLED    = 0;
FLINE_Factor.prototype.STATUS_NOT_CHANGED = 1;
FLINE_Factor.prototype.STATUS_RAISED      = 2;
FLINE_Factor.prototype.STATUS_LOWERED     = 3;

FLINE_Factor.prototype.EMPTY_VALUE        = '---';
FLINE_Factor.prototype.STATUS_CLEAN_TIMEOUT  = 5000; //время сколько подсвечивать изменившиеся коэфициенты/параметры (миллисекунд)


FLINE_Factor.prototype.setBlocked = function(blocked){
    if(this.blocked != blocked){
	this.blocked = blocked;
	if(this.blocked){
	    this.valueStatus = this.STATUS_DISABLED;
	}else{
	    if(this.valueStatus == this.STATUS_DISABLED) {
		this.valueStatus = this.STATUS_NOT_CHANGED;
	    }
	}
	this.storage.factorUpdated(this);
    }
}

FLINE_Factor.prototype.getValue = function(){
	if((typeof(this.value) == 'undefined') || (this.value == 0)){
	    return this.EMPTY_VALUE;
	}else{
	    return this.value.toFixed(2);
	}
}
FLINE_Factor.prototype.getTypeInfo = function(){
    return lang.geti('fct_code_'+this.typeId, this.getEvent(), this);
}
FLINE_Factor.prototype.getEvent = function(){
    return storage.getEvent(this.eventId);
}
FLINE_Factor.prototype.updateValue = function(newValue){
    if(this.value == newValue) return;
    if((newValue == 0)|| this.blocked){
	this.valueStatus = this.STATUS_DISABLED;
    }else if(newValue > this.value){
	this.valueStatus = this.STATUS_RAISED;
    }else if(newValue < this.value){
	this.valueStatus = this.STATUS_LOWERED;
    }
    this.value = newValue;
    if(this.storage != null){
	this.storage.factorUpdated(this);
    }
    this._valueCleanUpdateStatusID = fline_uniqid();
    setTimeout('FLINE_Factor_cleanUpdateValueStatus('+this.id+', '+this._valueCleanUpdateStatusID+')', this.STATUS_CLEAN_TIMEOUT);
    //FLINE_Log.notice('FLINE_Factor.updateValue', 'Updated. Factor id = '+this.id+'. updateId = '+this._valueCleanUpdateStatusID+'. Status = '+this.valueStatus+'.');
}
FLINE_Factor.prototype._cleanUpdateValueStatus = function(updateId){
  if(this._valueCleanUpdateStatusID == updateId){
    switch(this.valueStatus){
      case this.STATUS_RAISED:
      case this.STATUS_LOWERED:
	if(!this.blocked){
	    this.valueStatus = this.STATUS_NOT_CHANGED;
	}else{
	    this.valueStatus == this.STATUS_DISABLED;
	}
	this.storage.factorUpdated(this);
      	break;
      default:
      	//FLINE_Log.notice('FLINE_Factor._cleanUpdateValueStatus', 'Not required. Factor id = '+this.id+'. Current updateId = '+this._valueCleanUpdateStatusID+'. Requested updateId = '+updateId+'. Status = '+this.valueStatus+'.');
    }
  	this._valueCleanUpdateStatusID = null;
  }else{
    FLINE_Log.notice('FLINE_Factor._cleanUpdateValueStatus', 'Failed. Factor id = '+this.id+'. Current updateId = '+this._valueCleanUpdateStatusID+'. Requested updateId = '+updateId+'. Status = '+this.valueStatus+'.');
    //alert(this.eventId+' '+this.type+' '+this.value);
  }
}


/**
 * Простая котировка
 */
function FLINE_Factor_OK(id, eventId, type, typeId, blocked, value){
	this.id = id;
	this.eventId = eventId;
	this.type = type;
	this.typeId = typeId;
	this.blocked = blocked;
	this.value = value;
	if(value != 0) this.valueStatus = this.STATUS_NOT_CHANGED;
}
FLINE_Factor_OK.prototype = new FLINE_Factor();

/**
 * Котировка с параметром
 */
function FLINE_Factor_PARAM(id, eventId, type, typeId, blocked, value, param){
	this.id = id;
	this.eventId = eventId;
	this.type = type;
	this.typeId = typeId;
	this.blocked = blocked;
	this.value = value;
	if(value != 0) this.valueStatus = this.STATUS_NOT_CHANGED;
	this.param = param;
	this.paramStatus = this.STATUS_NOT_CHANGED;
	this._paramCleanUpdateStatusID = null;
}
FLINE_Factor_PARAM.prototype = new FLINE_Factor();

FLINE_Factor_PARAM.prototype.getParamIfHasValue = function(){
    if(this.value == 0){
        return this.EMPTY_VALUE;
    }else{
        return this.getParam();
    }
}
FLINE_Factor_PARAM.prototype.getParam = function(){
    //if(this.value == 0) return this.EMPTY_VALUE;
    switch(this.type){
        case 'f1':
        case 'f2':
        case 'fA1':
        case 'fA2':
        case 'ecF011':
        case 'ecF012':
        case 'ecF021':
        case 'ecF022':
        case 'ecF031':
        case 'ecF032':
        case 'ecF041':
        case 'ecF042':
        case 'ecF051':
        case 'ecF052':
        case 'ecF061':
        case 'ecF062':
        case 'ecF071':
        case 'ecF072':
        case 'ecF081':
        case 'ecF082':
        case 'ecF091':
        case 'ecF092':
        case 'ecF101':
        case 'ecF102':
        case 'ecF111':
        case 'ecF112':
            return this._getFora();
        default:
            return this.param;
    }
}
FLINE_Factor_PARAM.prototype._getFora = function(){
	return ((this.param >0)?'+':'') + this.param;
}
FLINE_Factor_PARAM.prototype.getTypeInfo = function(){
    //return this.type + '(' + this.typeId + ')' + ' ' + this.getParam();
    return lang.geti('fct_code_'+this.typeId, this.getEvent(), this, this.getParam());
}


FLINE_Factor_PARAM.prototype.updateParam = function(newValue){
  if(this.param == newValue) return this.EMPTY_VALUE;
  if(newValue > this.param){
		this.paramStatus = this.STATUS_RAISED;
	}else if(newValue < this.param){
		this.paramStatus = this.STATUS_LOWERED;
	}
	this.param = newValue;
	if(this.storage != null){
		this.storage.factorUpdated(this);
	}
	this._paramCleanUpdateStatusID = fline_uniqid();
	setTimeout('FLINE_Factor_cleanUpdateParamStatus('+this.id+', '+this._paramCleanUpdateStatusID+')', this.STATUS_CLEAN_TIMEOUT);
  //FLINE_Log.notice('FLINE_Factor.updateParam', 'Updated. Factor id = '+this.id+'. updateId = '+this._paramCleanUpdateStatusID+'. Status = '+this.paramStatus+'.');
}
FLINE_Factor_PARAM.prototype._cleanUpdateParamStatus = function(updateId){
  if(this._paramCleanUpdateStatusID == updateId){
    switch(this.paramStatus){
      case this.STATUS_RAISED:
      case this.STATUS_LOWERED:
      	this.paramStatus = this.STATUS_NOT_CHANGED;
				this.storage.factorUpdated(this);
      	break;
      default:
      	FLINE_Log.error('FLINE_Factor._cleanUpdateParamStatus', 'Not required. Factor id = '+this.id+'. Current updateId = '+this._paramCleanUpdateStatusID+'. Requested updateId = '+updateId+'. Status = '+this.paramStatus+'.');
    }
  	this._paramCleanUpdateStatusID = null;
  }else{
    FLINE_Log.notice('FLINE_Factor._cleanUpdateParamStatus', 'Failed. Factor id = '+this.id+'. Current updateId = '+this._paramCleanUpdateStatusID+'. Requested updateId = '+updateId+'. Status = '+this.paramStatus+'.');
    //alert(this.eventId+' '+this.type+' '+this.param+' : '+this.value);
  }
}



function FLINE_Factor_cleanUpdateValueStatus(fctId, tid){
	var fct = storage.getFactor(fctId); //TODO придумать что-нибудь чтобы получать storage динамически а не по заренее известому имени
	fct._cleanUpdateValueStatus(tid);
}
function FLINE_Factor_cleanUpdateParamStatus(fctId, tid){
	var fct = storage.getFactor(fctId); //TODO придумать что-нибудь чтобы получать storage динамически а не по заренее известому имени
	fct._cleanUpdateParamStatus(tid);
}
loadingJSLoaded++;