﻿function Loader(version) {
    this.fileList = new Array();
    this.version = version === undefined ? '0' : version;
    //proprietà necessarie per la gestione degli eventi
    this._listener = [];
    this._eventListenerFired = [];
    this._eventListen = [];
}
Loader.inherits(Base);
Loader.prototype.pushFile = function (file) {
    this.fileList.push(file);
}

Loader.prototype.loadFiles = function () {

    for (var j = 0; j < this.fileList.length; j++) {
        this.loadAll(this.fileList[j]);
    }

}



Loader.prototype.loadAll = function (index) {
    //alert(index);
    if (index === undefined) {
        index = 0;
    }

    var i = 0;

    if (index == this.fileList.length) {
        return;
    } else {

        //alert(8);
        var oHead = $('head')[0];

        var oScript = document.createElement('script');
        oScript.type = 'text/javascript';
        oScript.src = '/' + this.fileList[index];
        // most browsers
        index += 1;

        // IE 6 & 7
        /*   
        */
        oHead.appendChild(oScript);
        oHead.onload = this.loadAll(index);
        oHead.onreadystatechange = function () {
            if (this.readyState == 'complete') {
                setTimeout("this.loadAll(" + index + ")", 1000);
            }
        }
    }
}


function load1(file, loader, type, version, name) {
    //alert(caricato);
    if (!caricando && !caricato) {
        //alert(1);
        if (!loader.fileExists(file)) {
            var oScript = '';
            var oHead = $('head')[0];
            //var token = Math.round(100000 * Math.random());
            var token = version;
            if (type == 'js') {
                oScript = document.createElement('script');
                oScript.type = 'text/javascript';
                oScript.src = '/' + file + '?_=' + token;
                oHead.appendChild(oScript);
            } else if (type == 'css') {
                if (templateVersion === undefined || templateVersion == 1) {
                    oScript = document.createElement("link");
                    oScript.setAttribute("rel", "stylesheet");
                    oScript.setAttribute("type", "text/css");
                    oScript.setAttribute("href", '/' + file + '?_=' + token);
                    oHead.appendChild(oScript);
                }
                //oScript.src = '/' + file + '?_=' + token;
            } else if (type == 'references') {
                var script = document.createElement("script");

                if (name != undefined) {
                    var self = this;
                    window['fn'+name] = function () {
                        object = { file: name, className: '', custom: '', type: 'references' };
                        loader.fireEvent("caricato", object);
                        window['fn'+name] = undefined;
                    }
                }
                script.src = file + '?callback=fn' + name;
                script.type = "text/javascript";
                document.getElementsByTagName("head")[0].appendChild(script);
            }

            caricando = true;
            setTimeout('load1("' + file + '")', 100);
        }

        else {
            //alert(2);
            caricando = false;
            caricato = true;
            return true;
        }

    } else if (caricando && !caricato) {
        //alert(3);
        setTimeout('load1("' + file + '")', 100);
    } else if (caricato) {
        //alert(4); 
        //loader.fileList.push(file);
        return true;
    }

}



Loader.prototype.isUp = function (file, custom, type) {
    var className = custom == 1 ? 'Custom' : '';
    object = { file: file, className: className, custom: custom, type: type };
    this.fireEvent("caricato", object);
}


Loader.prototype.load = function (file, type, name) {
    caricando = false;
    caricato = false;
    //alert(caricato);
    if (type === undefined) type = 'js';

    caricato = load1(file, this, type, this.version, name);
    this.fileList.push(file);
    this.fireEvent("caricato", file);


}
Loader.prototype.fileExists = function (value) {

    for (var i = 0; i < this.fileList.length; i++) if (this.fileList[i] == value) return true;
    return false;
}

/*
Function.prototype.setScope = function(obj) {

var method = this;
var arg = [];
for (var i = 1; i < arguments.length; i++) arg.push(arguments[i]);

temp = function() {
return method.apply(obj, arg);
};

return temp;
}

Array.prototype.remove = function(value) {
for (var i = 0; i < this.length; i++) if (this[i] == value) this.splice(i, 1);
}
*/

