/**
 * Библиотека FLINE (FonBet Line)
 */

/**
 * Хранилише видов спорта, событий, котировок
 */
function FLINE_Storage(){
    this.init();
}
FLINE_Storage.prototype.init = function(spt){
    this._sportTable = new Array();
    this._anonsTable = new Array();
    this._eventTable = new Array();
    this._eventsBySport = new Array();
    this._factorTable = new Array();
    this._factorsByEvent = new Array();

    this._updateListeners = new Array();    
    this._storageListeners = new Array();    
}
FLINE_Storage.prototype.clear = function(spt){
    this._sportTable = new Array();
    this._anonsTable = new Array();
    this._eventTable = new Array();
    this._eventsBySport = new Array();
    this._factorTable = new Array();
    this._factorsByEvent = new Array();
    this.fireClear();
}
FLINE_Storage.prototype.hasEvents = function(){
    for(var i in this._eventTable){
	return true;
    }
    return false;
}
FLINE_Storage.prototype.setSport = function(spt){
    this._sportTable[spt.id] = spt;
    this._eventsBySport[spt.id] = new Array();
}
FLINE_Storage.prototype.getSport = function(id){
    return this._sportTable[id];
}
FLINE_Storage.prototype.getSortedSportList = function(){
    var list = clone(this._sportTable, false);
    list.sort(function(s1, s2){
	if((s1 == null) || (s2 == null)) return 0;
	if(s1.num == s2.num) return 0;
	return (s1.num > s2.num)?1:-1;
    });
    return list;
}
FLINE_Storage.prototype.getSportList = function(){
    return this._sportTable;
}
FLINE_Storage.prototype.removeSport = function(id){
    var hasEvents = false;
    for(var ind in this._eventsBySport[id]) {hasEvents = true; break;}
    if(hasEvents) return;
    if(id in this._eventsBySport) delete this._eventsBySport[id];
    if(id in this._sportTable) delete this._sportTable[id];
}

FLINE_Storage.prototype.setAnons = function(a){
    this._anonsTable[a.id] = a;
}
FLINE_Storage.prototype.getAnons = function(id){
    return this._anonsTable[id];
}
FLINE_Storage.prototype.removeAnons = function(id){
    if(!(id in this._anonsTable)) return;
    delete this._anonsTable[id];
}
FLINE_Storage.prototype.getSortedAnonsList = function(){
    var list = new Array();
    for(var i in this._anonsTable) list.push(this._anonsTable[i]);
    list.sort(function(a1, a2){
        if( (a1 == null) || (a2 == null) || (a1.startTime == null) || (a2.startTime == null) ) return 0;
        var t1 = a1.startTime.valueOf();
        var t2 = a2.startTime.valueOf();
        if(t1 == t2) return 0;
        return (t1 > t2)?1:-1;
    });
    return list;
}
FLINE_Storage.prototype.hasAnons = function(){
    for(var i in this._anonsTable){
        return true;
    }
    return false;
}

FLINE_Storage.prototype.setEvent = function(evt){
    if(!(evt.sportId in this._sportTable)){  //проверка существования указанного вида спорта
        FLINE_Log.error('FLINE_Storage.setEvent','Missing Sport #'+evt.sportId+'. Event #'+evt.id+' can not be added.');
        return;
    }
    if(evt.id in this._eventTable){ //событие уже существует, надо почистить
        this.removeEvent(evt.id);
    }
    this._eventsBySport[evt.sportId].push(evt.id);
    this._eventTable[evt.id] = evt;
    this._factorsByEvent[evt.id] = new Array();
    evt.storage = this;
}
FLINE_Storage.prototype.getEventList = function(){
    return this._eventTable;
}
FLINE_Storage.prototype.getEvent = function(id){
    return this._eventTable[id];
}
FLINE_Storage.prototype.removeEvent = function(id){
    if(!(id in this._eventTable)) return;
    var evt = this._eventTable[id];
    var ind = array_search(evt.id, this._eventsBySport[evt.sportId]);
    if(ind !== false){
        delete this._eventsBySport[evt.sportId][ind];
        //this.removeSport(evt.sportId);
    }
    delete this._eventTable[evt.id];
    delete this._factorsByEvent[evt.id];
    this.fireEventRemoved(evt.id);
}
FLINE_Storage.prototype.getSportEvents = function(sportId){
    return this._eventsBySport[sportId];
}
FLINE_Storage.prototype.getSortedSportEventList = function(sportId){
    var list = clone(this._eventsBySport[sportId], false);
    var me = this;
    list.sort(function(s1i, s2i){
	var s1 = me._eventTable[s1i];
	var s2 = me._eventTable[s2i];
	if((s1 == null) || (s2 == null)) return 0;
	if(s1.num == s2.num) return 0;
	return (s1.num > s2.num)?1:-1;
    });
    return list;
}

FLINE_Storage.prototype.setFactor = function(fct){
    if(!(fct.eventId in this._eventTable)){  //проверка существования указанного вида спорта
        FLINE_Log.error('FLINE_Storage.setFactor','Missing Event #'+fct.eventId+'. Factor #'+fct.id+' ('+fct.type+') can not be added.');
        return;
    }
    if(fct.id in this._factorTable){ //событие уже существует, надо почистить
        this.removeFactor(fct.id);
    }
    this._factorsByEvent[fct.eventId][fct.type] = fct.id;
    this._factorTable[fct.id] = fct;
    fct.storage = this;
}
FLINE_Storage.prototype.getFactor = function(id){
    return this._factorTable[id];
}
FLINE_Storage.prototype.removeFactor = function(id){
    if(!(id in this._factorTable)) return;
    var fct = this._factorTable[id];
    if(fct.type in this._factorsByEvent[fct.eventId]){
        delete this._factorsByEvent[fct.eventId][fct.type];
    }
    delete this._factorTable[fct.id];
}
FLINE_Storage.prototype.findFactor = function(eventId, type){
    if(!(eventId in this._factorsByEvent)) return null;
    if(!(type in this._factorsByEvent[eventId])) return null;
    return this.getFactor(this._factorsByEvent[eventId][type]);
}

FLINE_Storage.prototype.registerUpdateListener = function(listener){
    this._updateListeners.push(listener);
}
FLINE_Storage.prototype.registerStorageListener = function(listener){
    this._storageListeners.push(listener);
}
FLINE_Storage.prototype.factorUpdated = function(fct){
    for(var i in this._updateListeners){
        var l = this._updateListeners[i];
        l.factorUpdated(fct);
    }
}
FLINE_Storage.prototype.eventUpdated = function(evt){
    for(var i in this._updateListeners){
        var l = this._updateListeners[i];
        l.eventUpdated(evt);
    }
}
FLINE_Storage.prototype.eventScoreUpdated = function(evt){
    for(var i in this._updateListeners){
        var l = this._updateListeners[i];
        l.eventScoreUpdated(evt);
    }
}
FLINE_Storage.prototype.fireEventRemoved = function(evtId){
    for(var i in this._storageListeners){
        var l = this._storageListeners[i];
        l.eventRemoved(evtId);
    }
}
FLINE_Storage.prototype.fireClear = function(){
    for(var i in this._storageListeners){
        var l = this._storageListeners[i];
        l.clear();
    }
}
loadingJSLoaded++;