
var LoaderRegistry = function(place) {
    this.place = place;
    this.func_name = null;
    this.params = new Object();
    this.options = new Object();
    
    this.setCallbackFunction = function(name) {
        this.func_name = name;
        return this;
    };
    
    this.setParams = function(params) {
        if (params) {
            this.params = params;
        }
        
        return this;
    };
    
    this.setOptions = function(options) {
        if (options) {
            this.options = options;
        }
        
        return this;
    }
    
    this.getCallbackFunction = function() {
        return this.func_name;
    };
    
    this.getParams = function() {
        return this.params;
    };
    
    this.getOptions = function() {
        return this.options;
    }
    
    this.getPlaceMarker = function() {
        return this.place;
    };
};

var Loader = new (function(){
    
    this.register = new Array();
    this.scriptDir = '/design/ordinace/scripts/';
    this.libs = new Object();
    
    this.fetchLibrary = function(library,callback) {
        $.ajax({
            url: this.scriptDir + library,
            dataType: 'script',
            success: callback
        });
    }
    
    this.load = function(library,func_name,params,options){
        var _self = this;
        var current = this.register.length;
        
        this.placeMark();
        this.register[current].setCallbackFunction(func_name)
            .setParams(params)
            .setOptions(options);
        
        if (!this.libs[library]) {
            this.fetchLibrary(library,function(){
                _self.libs[library] = true;
                
                if (options.inTime)
                    _self.dispatch(current);
            });
        } else if (options.inTime) {
            this.dispatch(current);
        }
        
        return;
    };
    
    this.run = function(library,options,params) {
        if (!this.libs[library]) {
            if (options.callback) {
                this.fetchLibrary(library,function(){
                    options.callback(params);
                });
            } else {
                this.fetchLibrary(library);
            }
        } else if (options.callback) {
            options.callback(params);
        }
    }
    
    this.placeMark = function() {
        document.write('<div id="loader_marker_'+this.register.length+'" class="loading">načítám obsah..</div>');
        this.register.push(new LoaderRegistry($('#loader_marker_'+this.register.length)));
    };
    
    this.dispatch = function(id) {
        var loader;
        var applyOn;

        if (!id && id!==0) {
            for (var i=0;i<this.register.length;i++) {
                if (this.register[i])
                    this.dispatch(i);
            }
        } else if ((id || id === 0) && this.register[id]) {
            loader = this.register[id];
            
            if (loader.getOptions().selector) {
                if (loader.getOptions().selector == 'parent') {
                    applyOn = $(loader.getPlaceMarker().parent());
                } else {
                    applyOn = $(loader.getParams().selector,loader.getPlaceMarker().parent());
                }
                
                $(loader.PlaceMarker()).remove();
            } else {
                applyOn = loader.getPlaceMarker();
            }
            
            loader.getPlaceMarker().removeClass('loading');
            loader.getPlaceMarker().html('');
        
            eval('applyOn.'+loader.getCallbackFunction()+'(loader.getParams());');
            this.register[id] = null;
        }
    };
    
})();


