
// ONE API Loader
var ONEAPI_BASE_URL = "http://api.one.lv";
var ONEAPI_REFRESH_WDAY = 3;
var ONEAPI_BUILD = '1.1.1';
var ONEAPI_DEBUG = false;
function ONEAPI()
{
}
ONEAPI.DEBUG_COUNTAINER = "no";
ONEAPI.jQLoadCalled = false;
ONEAPI.LoadData={};

ONEAPI.init = function(appKey, language, version, encoding)
{
    if (null == document || document.body == null)
    {
        window.setTimeout(function()
        {
            ONEAPI.init(appKey, language, version, encoding);
        }, 10);
        return;
    }
    
    version = parseInt(version, 10);
    if (isNaN(version) || version == 0)
    {
        version = 1;
    }
    //
    ONEAPI.LoadData.appKey = appKey;
    ONEAPI.LoadData.language = language;
    ONEAPI.LoadData.version = version;
    ONEAPI.LoadData.encoding = encoding;
    
    if( typeof(encoding) == 'undefined' )
    {
    	encoding = "UTF-8";
    }
    //
    var scriptUrl = ONEAPI_BASE_URL +"/pub/oneapijavascript.do?script=api&version=1"+'&p='
            + this.getExpirationParameter() + '&locale=' + language + '&appKey=' + appKey+"&encoding="+encoding+'&build='+ONEAPI_BUILD;
    if (ONEAPI_DEBUG)
    {
        scriptUrl += '&' + (new Date()).getMilliseconds();
        var dbgDiv = document.createElement("div");
        document.body.appendChild(dbgDiv);
        //
        this.DEBUG_COUNTAINER = dbgDiv;
        this.DEBUG_COUNTAINER.style.width = "90%";
        this.DEBUG_COUNTAINER.style.height = "160px";
        this.DEBUG_COUNTAINER.style.position = "absolute";
        this.DEBUG_COUNTAINER.style.bottom = "0px";
        this.DEBUG_COUNTAINER.style.left = "0px";
        this.DEBUG_COUNTAINER.style.border = "1px solid red";
        this.DEBUG_COUNTAINER.style.backgroundColor = "#f0f0f0";
        this.DEBUG_COUNTAINER.style.overflow = "auto";
        this.DEBUG_COUNTAINER.style.opacity = "0.6";
        this.DEBUG_COUNTAINER.style.fontFamily = "Verdana";
        this.DEBUG_COUNTAINER.style.fontSize = "11px";
        ONEAPI.LOG.debug("Version: " + version + "; Build: " + ONEAPI_BUILD + " AppKey: " + appKey);
        ONEAPI.LOG.debug("Api Src: " + scriptUrl);
    }
    this.includeScript(scriptUrl);
    ONEAPI.init2();
    ONEAPI.LOG.debug("ONEAPI.init END");
}
ONEAPI.init2 = function()
{
    if (typeof(ONEAPI.initModules) == 'undefined')
    {
        ONEAPI.LOG.debug("ONEAPI.init2: No modules");
        window.setTimeout(function()
        {
            ONEAPI.init2();
        }, 10);
        return;
    }
    ONEAPI.initModules();
    ONEAPI.LOG.debug("ONEAPI.init2: END");
}
ONEAPI.includeScript = function(url)
{
    var script = document.createElement('script');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', url);
    document.getElementsByTagName('head').item(0).appendChild(script);
}
ONEAPI.getExpirationParameter = function()
{
    var date = new Date();
    var sDate = new Date();
    sDate.setDate(date.getDate() + (ONEAPI_REFRESH_WDAY - date.getDay()));
    return '' + sDate.getFullYear() + '' + sDate.getMonth() + '' + sDate.getDate();
}
ONEAPI.LOG = function()
{
}
ONEAPI.LOG.debug = function(message)
{
    if (!ONEAPI_DEBUG)
        return;
    var dbgMsg = document.createElement("li");
    dbgMsg.innerHTML = message.replace(/&/g, "&#38;");
    ONEAPI.DEBUG_COUNTAINER.appendChild(dbgMsg);
    ONEAPI.DEBUG_COUNTAINER.scrollTop = ONEAPI.DEBUG_COUNTAINER.scrollHeight;
}

