/** !
 * $R || rb
 * @author Rhys Burnie, [rb] Hybrid Garden, http://hybridgarden.com
 * Personal js library object.
 * An instanced object with some useful tools and properties.
 * 
 * NB: 	May also adds a few useful things to MooTools or jQuery 
 * 		depending on what's loaded. If in doubt can always see if
 * 		they .has any :P
 * 		Ex. if($R.has('Radio',Element)) to see if theres a  Element.Radio
 */

(function( window, undefined ) {
	var $R 		= function() {},
		has		= function(k,s){/*scope*/
					return typeof (s || this)[k] != 'undefined';
				},
		info 	= {
					name: '[rb] library',
					author:'Rhys Burnie',
					version:'beta'
				},
		data	= {},
		tmp		= {};
	
	$R.prototype = {
		log: function(m,fn){typeof console!='undefined'?console[fn||'log'](m):function(){};},
		print: function(into) { this.into = $(into); this.log = function(what) { this.into.appendText(what); }.bind(this); },
		enhancements:{},
		enhance: function(k){
			var b = this.has(k,this.enhancements) ? this.enhancements[k]() : false;
			if(b===false) {
				this.log('$R.enhancements.'+k+'() was not found - not executed.','warn');
			}
			return b;
		},
		has: has,
		info: info,
		data: data
	};
	
	// Add framework Boolean keys to info to determine if framework exists
	info.frameworks = ['MooTools','jQuery'];
	for(var i=0,c=info.frameworks.length;i<c;i++) {
		if(has(info.frameworks[i])) {
			tmp[info.frameworks[i]] = true;
		}else{
			tmp[info.frameworks[i]] = false;
		}
	}
	info.frameworks = tmp;tmp={};
	
	
	
	/**
	 * $R.adio ($R.radio) radio utility
	 * @param {String} name attributes value
	 * @param {String} the word 'checked' if you want it checked
	 * @return {Element} <input type="radio" name="name" checked="checked"/> (checked optional)
	 * 
	 * DOES NOT inject or set value, id or other atttributes other than:
	 * type,name & checked.
	 */
	var radio = function(n) {
		var r = null,
			s = {
				i: 'input',
				t: 'type',
				r: 'radio',
				n: 'name',
				c: 'checked'
			},
			c = (arguments.length==2 && arguments[1]==s.c) ? s.c+'="'+s.c+'"' : ''
			;
		
		try{  
			r = document.createElement('<'+s.i+' '+s.t+'="'+s.r+'" '+s.n+'="'+n+'" '+c+'/>');  
		}catch(err){  
			r = document.createElement(s.i);  
		}
		// Set attributes for non IE
		if(!/msie/i.test(navigator.userAgent)) {
			r.setAttribute(s.t,s.r); 
			r.setAttribute(s.n,n);
			if(c!='') {
				r.setAttribute(s.c, s.c);
			}
		}
		
		return r;
	};
	// Add raio utility to $R 
	// and the window if theres no existing radio (whatever that may be)
	$R.prototype.adio = $R.prototype.radio = radio;
	if(!has('radio',window)) { window.radio = radio; }
	
	
	// = Expose as instance = ==================================================
	window.$R = window.rb = new $R;
})(window);

/**!
---

script: Assets.js

description: Provides methods to dynamically load JavaScript, CSS, and Image files into the document.

license: MIT-style license

authors:
- Valerio Proietti

requires:
- core:1.2.4/Element.Event
- /MooTools.More

provides: [Assets]

...
*/
if($R.has('MooTools',window)) {
var Asset = {

	javascript: function(source, properties){
		properties = $extend({
			onload: $empty,
			document: document,
			check: $lambda(true)
		}, properties);
		
		if (properties.onLoad) properties.onload = properties.onLoad;
		
		var script = new Element('script', {src: source, type: 'text/javascript'});

		var load = properties.onload.bind(script), 
			check = properties.check, 
			doc = properties.document;
		delete properties.onload;
		delete properties.check;
		delete properties.document;

		script.addEvents({
			load: load,
			readystatechange: function(){
				if (['loaded', 'complete'].contains(this.readyState)) load();
			}
		}).set(properties);

		if (Browser.Engine.webkit419) var checker = (function(){
			if (!$try(check)) return;
			$clear(checker);
			load();
		}).periodical(50);

		return script.inject(doc.head);
	},

	css: function(source, properties){
		return new Element('link', $merge({
			rel: 'stylesheet',
			media: 'screen',
			type: 'text/css',
			href: source
		}, properties)).inject(document.head);
	},

	image: function(source, properties){
		properties = $merge({
			onload: $empty,
			onabort: $empty,
			onerror: $empty
		}, properties);
		var image = new Image();
		var element = document.id(image) || new Element('img');
		['load', 'abort', 'error'].each(function(name){
			var type = 'on' + name;
			var cap = name.capitalize();
			if (properties['on' + cap]) properties[type] = properties['on' + cap];
			var event = properties[type];
			delete properties[type];
			image[type] = function(){
				if (!image) return;
				if (!element.parentNode){
					element.width = image.width;
					element.height = image.height;
				}
				image = image.onload = image.onabort = image.onerror = null;
				event.delay(1, element, element);
				element.fireEvent(name, element, 1);
			};
		});
		image.src = element.src = source;
		if (image && image.complete) image.onload.delay(1);
		return element.set(properties);
	},

	images: function(sources, options){
		options = $merge({
			onComplete: $empty,
			onProgress: $empty,
			onError: $empty,
			properties: {}
		}, options);
		sources = $splat(sources);
		var images = [];
		var counter = 0;
		return new Elements(sources.map(function(source){
			return Asset.image(source, $extend(options.properties, {
				onload: function(){
					options.onProgress.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				},
				onerror: function(){
					options.onError.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				}
			}));
		}));
	}

};
}// end Asset class