
 
function JSFile() {
	var scripts = document.getElementsByTagName('script');
	this.base = scripts[0];
	this.list = {};
	this.book = {};
 
	for (var i = 0, len = scripts.length; i < len; ++i) {
		var src = scripts[i].getAttribute('src');
		if (!src) continue;
		this.list[src] = true;
		var path = src.replace(/([^\/]+?)\.js(\?.*)?$/, ''), inc = src.match(/\?.*load=([^&]*)/);
		if (inc) {
			if (path in this.book)
				this.book[path] += ',';
			else
				this.book[path] = '';
			this.book[path] += inc[1];
		}
	}
	for (var path in this.book)
		this.load(this.book[path].split(','), path);
}
 
JSFile.prototype = {
	_load: function (uri) {
		var XUL_NS_URI = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
		if (document.documentElement &&
			document.documentElement.namespaceURI == XUL_NS_URI) {
			var tmp = document.createElementNS(XUL_NS_URI, 'script');
			    tmp.setAttribute('type', 'application/x-javascript');
			    tmp.setAttribute('src', uri);
			this.base.parentNode.appendChild(tmp);
		} else {
			document.write('<script type="text/javascript" src="' + uri +'"></script>');
		}
	},
	load: function (jsfile, prefix, suffix) {
		prefix = prefix || '';
		suffix = suffix || '.js';
		for (var i = 0, len = jsfile.length; i < len; ++i) {
			var path = prefix + jsfile[i] + suffix;
			if (!this.list[path]) this._load(path);
		}
	}
}
