/*
	Creator: sixtyseconds creative media cc <http://www.sixtyseconds.co.za>
	Thanks to:  Mootools.More Asset Class
				Oleg Slobodskoi, xLazyLoader 1.0
	mootools.loader.js - Proposed architecture for Mootools.More Asset Class Architecture.
*/
var Loader = new Class({
	Implements: [Options, Events],
	options: {
		'scripts': [],
		'styles': [],
		'images': [],
		'onComplete': $empty, //Arguments: total assets, index as part of the whole, current asset array length, index as part of current asset array.
		'onProgress': $empty //Arguments: total assets, scripts array length, styles array length, images array length.
	},
	initialize: function(options) {		
		var that = this
		this.setOptions(options);
		
		var head = $$('head')[0];
		var scripts = this.options.scripts;
		var styles = this.options.styles;
		var images = this.options.images;
		
		//Determine length of asset arrays
		var total_assets = scripts.length + styles.length + images.length;
		var total_index = 0;
		
		//Loop through scripts array
		scripts.each(function(script, index) {
			var progress = function() {
				that.fireEvent('onProgress', [total_assets, total_index, scripts.length, index, 'scripts']);
				total_index++;
			}
			
			//Prevent duplicate script loads.
			if ($$('script[src*="' + script + '"]').length) progress();
			
			var element = new Element('script', {
				'type': 'text/javascript',
				'src': script
			}).inject(head);

			//MSIE onload eventing.
			if (Browser.Engine.trident) {
				element.onreadystatechange = function ()	{
					if (/loaded|complete/.test(element.readyState)) {
						progress();
					}
				}
			}
			//Everyone else's onload eventing.
			else {
				element.onload = progress;
			}
		});
		
		styles.each(function(style, index) {
			var progress = function() {
				that.fireEvent('onProgress', [total_assets, total_index, styles.length, index, 'styles']);
				total_index++;
			}			
			
			//Prevent duplicate style loads.
			if ($$('link[href*="' + style + '"]').length) progress();
			
			var element = new Element('link', {
				'type': 'text/css',
				'href': style,
				'media': 'all',
				'rel': 'stylesheet'
			}).inject(head);
				
			//MSIE onload eventing.
			if (Browser.Engine.trident) {
				element.onreadystatechange = function ()	{
					if (/loaded|complete/.test(element.readyState)) {
						progress();
					}
				}
			}
			//Opera onload eventing.
			else if (Browser.Engine.presto) {
				element.onload = progress;				
			}
			//Everyone else's onload eventing.
			else {
				(function(){
					try {
						element.sheet.cssRule;
					} catch(e){
						setTimeout(arguments.callee, 20);
						return;
					};
					progress();
				})();
			}
		});
		
		images.each(function(image, index) {
			var progress = function() {
				that.fireEvent('onProgress', [total_assets, total_index, images.length, index, 'images']);
				total_index++;
			}		
			
			var element = new Element('link', {
				'src': image
			});
			
			//Image onload, courtesy current Mootools.More Asset Class.
			var img = new Image();
			var element = $(img) || new Element('img');
			['load'].each(function(name){
				var type = 'on' + name;
				var event = progress;
				img[type] = function(){
					if (!img) return;
					if (!element.parentNode){
						element.width = img.width;
						element.height = img.height;
					}
					img = img.onload = img.onabort = img.onerror = null;
					event.delay(1, element, element);
					element.fireEvent(name, element, 1);
				};
			});
			img.src = element.src = image;
			if (img && img.complete) img.onload.delay(1);
		});
		
		this.fireEvent('onComplete', [total_assets, scripts.length, styles.length, images.length]);
	}
});
