var preloader = new function(){
    var _this = this;
    this.toDL = [];
    this.f = null;
    this.init = function(f){
        this.toDL = [];
        for (category_id in siteData.categories)
        {
            category = siteData.categories[category_id];
            this._push(category.background);
            this._push(category.background_small);
            this._push(category.background_smaller);
            this._push(category.shadow);
            for (app_id in category.applications)
            {
                application = category.applications[app_id];
                this._push(application.css);
                this._push(application.thumbnail);
                //this._push(application.xml);
            }
        }
	this.f = f;
        this.start();
    };
    
    this._push = function(element){
        if (element)
            _this.toDL.push(element);
    };
    
    this.start = function(){
        $.preload(this.toDL,
                {
                    onFinish: this.onPreloadFinish, 
                    onComplete: this.onRequestFinished,
                    enforceCache: true
        });        
    };
    
    this.onPreloadFinish = function(){
        $('#preloader').remove();
	if (_this.f)
		_this.f();
    };
    
    this.di = 0;
    this.onRequestFinished = function(data){
        _this.di ++;
        var percent = Math.ceil(_this.di / _this.toDL.length * 100);
        $('#preloader_progressbar_percent').text(percent + "%");
        $('#preloader_progressbar_bar').css({width: (100 - percent) + "%"});
    };
}

