JsLoader = Class.create({
	initialize: function() {
		this.loaded = new Hash;
	},
	load: function(url, options) {
		var script = this.loaded.get(url);
		if (Object.isUndefined(script)) {
			script = new JsLoader.Script(url);
			this.loaded.set(url, script);
		}
		script.prepare(options);
		script.retrieve();
	}
});
JsLoader.Script = Class.create({
	initialize: function(url) {
		this.url = url;
		this.complete = false;
		this.options = {onSuccess: Prototype.emptyFunction};
		this.js = new Element('script', {
			type: 'text/javascript',
			src: url,
			defer: true
		});
	},
	prepare: function(options) {
		Object.extend(this.options, options);
	},
	retrieve: function() {
		if (this.complete) {
			this.execute();
			return;
		}
		var head = document.getElementsByTagName('head')[0];
		head.appendChild(this.js);
		this.js.onreadystatechange = this.js.onload = this.onStateChange.bind(this);
	},
	execute: function() {
		this.options.onSuccess(this);
	},
	onStateChange: function() {
		if (this.js.readyState && (this.js.readyState != 'loaded') && (this.js.readyState != 'complete')) {
			return;
		}
		this.js.onreadystatechange = this.js.onload = null;
		this.complete = true;
		this.execute();
	}
});
var jsLoader = new JsLoader;
