/**
 * Библиотека FLINE (FonBet Line)
 */

/**
 * Событие
 */
function FLINE_Event(id, sportId, parent, num, name){
    this.storage = null;
    this.id = id;
    this.sportId = sportId;
    this.parent = parent;
    this.num = num;
    this.name = name;
    this.version = 0;
    this.isEnabled = true;
    this.isExtraBetsExpanded = true;
    this.hasExtraBets = false;
    this.score1 = 0;
    this.score2 = 0;
    this.scoreComment = "";
    this.isInCoupon = false;
    this.timerDirection = 0;
    this.timerSeconds = 0;
    this.timerUpdateTime = null;
    this._initTeams();
}
/*
FLINE_Event.prototype.NEXT_EVENT_TYPE_GOAL  = 0;
FLINE_Event.prototype.NEXT_EVENT_TYPE_EMPTY = 1;
FLINE_Event.prototype.NEXT_EVENT_TYPE_SET   = 102;
FLINE_Event.prototype.NEXT_EVENT_TYPE_GAME  = 103
FLINE_Event.prototype.NEXT_EVENT_TYPE_FRAME = 104
*/
FLINE_Event.prototype.getName = function(){
  return this.num + " " + this.name;
}
FLINE_Event.prototype.getTeam1 = function(){
  return this.team1;
}
FLINE_Event.prototype.getTeam2 = function(){
  return this.team2;
}
FLINE_Event.prototype.getNum = function(){
  return this.num;
}
FLINE_Event.prototype.getBaseName = function(){
  return this.name;
}
FLINE_Event.prototype.getScoreText = function(){
    var scoreText = this.score1 + ":" + this.score2;
    if(this.scoreComment != null) scoreText += " "+this.scoreComment;
    return scoreText;
}
FLINE_Event.prototype.getScoreTextWithoutComment = function(){
    return this.score1 + ":" + this.score2;
}
FLINE_Event.prototype.setEnabled = function(isEnabled){
  if(this.isEnabled == isEnabled) return;
  this.isEnabled = isEnabled;
	if(this.storage != null){
		this.storage.eventUpdated(this);
	}
}
FLINE_Event.prototype.setScore = function(score1, score2, scoreComment){
    if( (this.score1 == score1) &&
	(this.score2 == score2) &&
	(this.scoreComment == scoreComment)
	) return;
    this.score1 = score1;
    this.score2 = score2;
    this.scoreComment = scoreComment;
    if(this.storage != null){
	this.storage.eventScoreUpdated(this);
    }
}
FLINE_Event.prototype.setInCoupon = function(isInCoupon){
    this.isInCoupon = isInCoupon;
    if(this.storage != null){
	this.storage.eventUpdated(this);
    }
}
FLINE_Event.prototype.setTimerData = function(direction, seconds, updateTime){
    this.timerDirection = direction;
    this.timerSeconds = seconds;
    this.timerUpdateTime = updateTime;
}
FLINE_Event.prototype.getTimerValue = function(){
    if(this.timerUpdateTime == null) return null;
    var now = new Date();
    var timeDiff = Math.floor( (now.valueOf() - this.timerUpdateTime.valueOf())/1000 );
    var value = this.timerSeconds + (this.timerDirection * timeDiff);
    var min = Math.floor(Math.abs(value)/60);
    var sec = Math.abs(value % 60);
    return ((value>=0)?"":"-")+min+":"+((sec<10)?("0"+sec):sec);
}
FLINE_Event.prototype._initTeams = function(){
    this.team1 = "1";
    this.team2 = "2";
    var teamNames = this.name.split(" - ", 2);
    if(teamNames.length == 2 && this._initTeams_isTeamNameCorrect(teamNames[0])){
        this.team1 = teamNames[0];
        this.team2 = teamNames[1];
    }
}
FLINE_Event.prototype._initTeams_isTeamNameCorrect = function(tname){
    var first = tname.substr(0,1);
    var firstLower = first.toLowerCase();
    if(first == firstLower){
        var hasSpace = tname.indexOf(" ");
        return (hasSpace < 0);
    }else{
        return true;
    }
}
loadingJSLoaded++;