/* SCTJS - Scrutari Javascript Library
 *
 * Copyright (C) 2010 Vincent Calame - Exemole <vincent.calame@exemole.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

function SCTJS_BaseData () {

}

SCTJS_BaseData.urlMap = new Object();

SCTJS_BaseData.getBaseDataMap = function (url) {
    return SCTJS_BaseData.urlMap[url];
}

SCTJS_BaseData.getBaseData = function (baseDataMap, codeBase) {
    return baseDataMap["c" + codeBase];
}

SCTJS_BaseData.getBaseIcon = function (baseDataMap, codeBase) {
    var obj = baseDataMap["c" + codeBase];
    if (!obj) return "";
    if (obj.baseicon) return obj.baseicon;
    else return "";
}

SCTJS_BaseData.initBaseDataMap = function (url, langUi) {
    if (SCTJS_BaseData.urlMap[url]) return;
    $.ajax({
        dataType: "jsonp",
        url: url,
        data: {
                type: "base",
                langui: langUi,
        },
        success: function(objetResultat) {
                SCTJS_BaseData.initBaseDataMapSuccess(url, objetResultat);
        }
    });
}

SCTJS_BaseData.initBaseDataMapSuccess = function (url, objetResultat) {
    var count = objetResultat.baseData.baseCount;
    if (count == 0) {
         SCTJS_BaseData.urlMap[url] = new Object();
        return;
    }
    var baseMap = new Object();
    var baseArray = objetResultat.baseData.baseArray;
    for(var i = 0; i < count; i++) {
        var base = baseArray[i];
        baseMap["c" + base.codebase] = base;
    }
    SCTJS_BaseData.urlMap[url] = baseMap;
}

SCTJS_BaseData.replaceM = function (mArray, balise_ouvrante, balise_fermante) {
    var resultat = "";
    var count = mArray.length;
    for(var i = 0; i < count; i++) {
        var obj = mArray[i];
        if (typeof obj == "object") {
            resultat = resultat + balise_ouvrante + obj.s + balise_fermante;
        } else {
            resultat = resultat + obj;
        }
    }
    return resultat;
}
