/**
 * A3non Sandbox - Util.Asset
 * 
 * @author Andi Dittrich <andi.dittrich@a3non.org>
 * @url http://www.a3non.org
 * @license Creative Commons BY-NC, Creative Commons Namensnennung-Keine kommerzielle Nutzung 3.0 Deutschland Lizenz, 
 * @license http://creativecommons.org/licenses/by-nc/3.0/de/
 * @version 1.0
 * @Todo Asset.css.onload event isn't supported by mootools yet
 */
Sandbox.Util.Asset = new Class({
	Implements: [Options, Events],
	
	options: {
		js: [],
		css: []
	},
	
	length: 0,
	progress: 0,
	
	initialize: function(options){
		this.setOptions(options);
		this.length = this.options.js.length;// + this.options.css.length;
	},
	
	load: function(){
		// load css
		this.options.css.each(function(item, index){
			var jsSource = new Asset.css(item, {
				/*onload: function(){
					console.log('css loaded');
					this.update();
				}.bind(this),
				onerror: function(){
					console.log('css error');
					this.update();
				}.bind(this)
				*/
			});
		}.bind(this));

		// load js
		this.options.js.each(function(item, index){
			var jsSource = new Asset.javascript(item, {
				onload: function(){
					this.update();
				}.bind(this),
				onerror: function(){
					this.update();
				}.bind(this)

			});
		}.bind(this));
		
		// workaround no css.onload
		if (this.progress==this.length){
			this.fireEvent('complete');
		}
	},
	
	update: function(){
		this.progress++;
		this.fireEvent('update', [this.progress, this.length]);
		
		if (this.progress==this.length){
			this.fireEvent('complete');
		}
	}	
});
