/**
 * Библиотека FLINE (FonBet Line)
 */

/**
 * Столбец таблицы основных (стандартных) ставок
 */
function FLINE_Line_StdBetDef(name, type, isParam){
    this.name = name;
    this.type = type;
    this.isParam = isParam;
}
function FLINE_Line_ExtraBetNextTypeGroups(name, typeP1, typeP2, typePX){
    this.name  = name;
    this.typeP1 = typeP1;
    this.typeP2 = typeP2;
    this.typePX = typePX;
}

/**
 * Объект интерфеса пользователя отвечающий за вывод линии
 */
function FLINE_Line(contentBlockId, storage, lang){
    this.contentBlockId = contentBlockId;
    this.lang = lang;
    this.storage = storage;
    this.storage.registerUpdateListener(this);
    this._eventIndex = new Array();
}
FLINE_Line.prototype.onBetClick = null;
FLINE_Line.prototype.onSportClick = null;
FLINE_Line.prototype.onEventClick = null;

FLINE_Line.prototype.IMAGE_COLLAPSED = './img/arrow_collapsed.gif';
FLINE_Line.prototype.IMAGE_EXPANDED = './img/arrow_expanded.gif';
FLINE_Line.prototype.IMAGE_COLLAPSE_ALL = './img/arrow_collapseAll.gif';
FLINE_Line.prototype.IMAGE_EXPAND_ALL = './img/arrow_expandAll.gif';
FLINE_Line.prototype.ID_STATUS_TD = 'lineStatusDiv';
FLINE_Line.prototype.ID_PREFIX_IMAGE_SPORT_EXPAND  = 'line_img_sport_exp_';
FLINE_Line.prototype.ID_PREFIX_IMAGE_EVENT_EXPAND  = 'line_img_event_exp_';
FLINE_Line.prototype.ID_PREFIX_EVENT_STD    = 'line_evt_std_';
FLINE_Line.prototype.ID_PREFIX_EVENT_EXTRA  = 'line_evt_ex_';
FLINE_Line.prototype.ID_PREFIX_EVENT_SCORE  = 'line_evt_score_';
FLINE_Line.prototype.ID_PREFIX_EVENT_TIMER  = 'line_evt_timer_';
FLINE_Line.prototype.ID_PREFIX_FACTOR_VALUE = 'line_fct_val_';
FLINE_Line.prototype.ID_PREFIX_FACTOR_PARAM = 'line_fct_param_';
FLINE_Line.prototype.ID_PREFIX_FACTOR_PARAM_TH = 'line_fct_param_th_';
//FLINE_Line.prototype.INTERVAL_UPDATE_STATUS_CLEAR = 5000;  //время сколько подсвечивать изменившиеся коэфициенты/параметры (миллисекунд)
FLINE_Line.prototype.CLASS_EVENT_DISSABLED = 'dissabledEvent';
FLINE_Line.prototype.CLASS_EVENT_IN_COUPON = 'eventInCoupon';
FLINE_Line.prototype.CLASS_FACTOR_VALUE_RAISED= 'factorValueRaised';
FLINE_Line.prototype.CLASS_FACTOR_VALUE_LOWERED= 'factorValueLowered';

FLINE_Line.prototype.STANDART_EVENTCOL_SCORE = 'line_eventcol_score';
FLINE_Line.prototype.STANDART_EVENTCOL_NAME  = 'line_eventcol_event';
FLINE_Line.prototype.BET_DEF_INDEX = new Array();
FLINE_Line.prototype.BET_DEF_INDEX['p1'] = new FLINE_Line_StdBetDef('line_stdbets_1',    'p1',  false),
FLINE_Line.prototype.BET_DEF_INDEX['pX'] = new FLINE_Line_StdBetDef('line_stdbets_X',    'pX',  false),
FLINE_Line.prototype.BET_DEF_INDEX['p2'] = new FLINE_Line_StdBetDef('line_stdbets_2',    'p2',  false),
FLINE_Line.prototype.BET_DEF_INDEX['t']  = new FLINE_Line_StdBetDef('line_stdbets_t',    'tG',  true),
FLINE_Line.prototype.BET_DEF_INDEX['tG'] = new FLINE_Line_StdBetDef('line_stdbets_2',    'tG',  false),
FLINE_Line.prototype.BET_DEF_INDEX['tL'] = new FLINE_Line_StdBetDef('line_stdbets_2',    'tL',  false),

// ============================================ РИСОВАНИЕ ======================
FLINE_Line.prototype._refreshEventIndex = function(){
    this._eventIndex.length = 0;
    for(var i in storage._eventTable){
        var e = storage._eventTable[i];
        this._eventIndex[e.num] = e.id;
    }
}
FLINE_Line.prototype.eventNum2id = function(eventNum){
    return this._eventIndex[eventNum];
}
FLINE_Line.prototype.getFactorHTML = function(eventNum, factorType){
    var betTd  = document.createElement('td');
    betTd.innerHTML = '&nbsp;';
    var betDef = FLINE_Line.prototype.BET_DEF_INDEX[factorType];

    var eId = this.eventNum2id(eventNum);
    if (eId == null) return this.__getOuterHTML(betTd);
    var fct = this.storage.findFactor(eId, betDef.type);
    if (fct == null) return this.__getOuterHTML(betTd);

    betTd.factor = fct.id;
    betTd.changeId = null;
    if(betDef.isParam){
        betTd.id = this.ID_PREFIX_FACTOR_PARAM+fct.id;
        this._paint_factorParam(betTd, fct);
    }else{
        betTd.id = this.ID_PREFIX_FACTOR_VALUE+fct.id;
        this._paint_factorValue(betTd, fct);
    }
    return this.__getOuterHTML(betTd);
}
FLINE_Line.prototype.__getOuterHTML = function(el){
  if(typeof(el.outerHTML) != 'undefined'){
      return el.outerHTML;
  }else{
      var s = document.createElement('span');
      s.appendChild(el);
      return s.innerHTML;
  }
}
FLINE_Line.prototype.getEventClass = function(evtId){
  var evt = storage._eventTable[evtId];
  if(evt == null) return null;
  return (evt.isEnabled)?((evt.isInCoupon)?this.CLASS_EVENT_IN_COUPON:null):this.CLASS_EVENT_DISSABLED;    
}
FLINE_Line.prototype.paint = function(){
    this._refreshEventIndex();
}
FLINE_Line.prototype._paint_onClick = function(td, fctId, isClickable){
  if(isClickable){
      td.setAttribute('onClick', 'coupon.addFactor('+fctId+');');
  }else{
      td.setAttribute('onClick', '');
  }
}
FLINE_Line.prototype._paint_factorValue = function(td, fct){
    td.innerHTML = fct.getValue();
    switch (fct.valueStatus){
	case fct.STATUS_DISABLED:
	    html_set_class(td, 'factorValueDisabled');
	    this._paint_onClick(td, fct.id, false);
	    break;
	case fct.STATUS_RAISED:
	    html_set_class(td, 'factorValueRaised');
	    this._paint_onClick(td, fct.id, true);
	    break;
	case fct.STATUS_LOWERED:
	    html_set_class(td, 'factorValueLowered');
	    this._paint_onClick(td, fct.id, true);
	    break;
	case fct.STATUS_NOT_CHANGED:
	default:
	    html_set_class(td, 'factorValueNormal');
	    this._paint_onClick(td, fct.id, true);
    }
}
FLINE_Line.prototype._paint_factorParam = function(td, fct){
    td.innerHTML = fct.getParam();
    switch (fct.paramStatus){
	case fct.STATUS_RAISED:
	    html_set_class(td, 'factorParamRaised');
	    break;
	case fct.STATUS_LOWERED:
	    html_set_class(td, 'factorParamLowered');
	    break;
	case fct.STATUS_NOT_CHANGED:
	default:
	    html_set_class(td, 'factorParamNormal');
    }
}

// ============================================ ОБНОВЛЕНИЯ =====================
FLINE_Line.prototype.factorUpdated = function(fct){
    //alert(fct.id);
    
    // обновление value и блокировки
    var vtd = document.getElementById(this.ID_PREFIX_FACTOR_VALUE+fct.id);
    if(vtd == null) return;
    this._paint_factorValue(vtd, fct);
    
    //обновление param
    if(fct instanceof FLINE_Factor_PARAM){
	var ptd = document.getElementById(this.ID_PREFIX_FACTOR_PARAM+fct.id);
	if(ptd != null){  //обычная котировка с параметром
	    this._paint_factorParam(ptd, fct);
	    return
	}
	var pth = document.getElementById(this.ID_PREFIX_FACTOR_PARAM_TH+fct.id);
	if(pth != null){ //параметр в названии группы доп. ставок
	    pth.innerHTML = this.lang.get(pth.textTemplateName, fct.param);
	}
    }
}
FLINE_Line.prototype.eventUpdated = function(evt){
    var stdRow = document.getElementById(this.ID_PREFIX_EVENT_STD+evt.id);
    if(stdRow == null) return;
    var eClass = (evt.isEnabled)?((evt.isInCoupon)?this.CLASS_EVENT_IN_COUPON:null):this.CLASS_EVENT_DISSABLED;
    html_set_class(stdRow, eClass);
}
FLINE_Line.prototype.eventScoreUpdated = function(evt){
    /*
    var scoreSpan = document.getElementById(this.ID_PREFIX_EVENT_SCORE+evt.id);
    if(scoreSpan == undefined) return;
    scoreSpan.innerHTML = evt.getScoreText();
    */
}
FLINE_Line.prototype.statusUpdated = function(){
    var statusDiv = document.getElementById(this.ID_STATUS_TD);
    if(statusDiv == null) return;
    statusDiv.innerHTML = dwrProxy.lineUpdater.getStatusText();
}
// ============================================ РАЗНОЕ =========================
loadingJSLoaded++;