;FCL.GET =
(function($) {
	/** ____ CUSTOMIZED MALT CODE ____ **/
	var Resource = function(name, dependencies, callback) {
		var self = this;

		self.name     = name;
		self.status   = null;
		self.parents  = [];
		self.callback = callback;
		self.children = null;

		if (self.name !== null) {
			FCL.GET._resourceMap[self.name] = self;
		}

		if (typeof dependencies === 'object')
		{
			self.children = [];
			$.each(dependencies, function(k, dep) {
		        self.children.push(FCL.GET._resourceMap[dep] ? FCL.GET._resourceMap[dep] : new Resource(dep));
			});
		}
	};
	Resource.prototype.load = function() {
		var self = this;

		if (self.status !== null) {
			return;
		}
		self.status = FCL.GET.STATUS_LOADING;

		if (!self.children) {
			var xhrObj = jQuery.ajaxSettings.xhr();
			xhrObj.onreadystatechange = function() {
				if (xhrObj.readyState == 4) {
					FCL.GET._deferredScripts[self.name] = xhrObj.responseText;

					// Run the onload() stuff
					self.status = FCL.GET.STATUS_FINISHED;
					FCL.GET._log.push(self.name);
					self.updateParents();
				}
			};
			xhrObj.open('GET', self.name, true);
			xhrObj.send('');
		} else {
			$.each(self.children, function(k, child) {
				child.parents.push(self);
				child.load();
			});
		}
	};

	Resource.prototype.updateParents = function() {
		$.each(this.parents, function(k, parent) {
			if (parent.isLoaded())
			{
				parent.status = FCL.GET.STATUS_FINISHED;
				parent.insertDeferredScripts();
				parent.callback && parent.callback();
				parent.updateParents();
			}
		});
	};

	Resource.prototype.isLoaded = function() {
		if (this.status === FCL.GET.STATUS_FINISHED) {
			return true;
		}

		if (!this.children) {
			return this.status === FCL.GET.STATUS_FINISHED;
		} else {
			var allLoaded = true;
			$.each(this.children, function(k, child) {
				if (!child.isLoaded()) {
					allLoaded = false;
				}
			});
			return allLoaded;
		}
	};

	Resource.prototype.insertDeferredScripts = function() {
		$.each(this.children, function(k, resource) {
			if (!FCL.GET._deferredScripts[resource.name]) {
				return;
			}

			var response = FCL.GET._deferredScripts[resource.name];
			var se = document.createElement('script');
			document.getElementsByTagName('head')[0].appendChild(se);
			se.text = response;

			FCL.GET._deferredScripts[resource.name] = null;
		});
	};
	/** ____ CUSTOMIZED MALT CODE ____ **/


	// Public methods and variables
	;FCL.GET = {
		STATUS_LOADING: '1',
		STATUS_FINISHED: '2',
		_deferredScripts: [],
		_resourceMap: {},
		_log: [],

		/**
		 * Use this function to load javascript (curried function)
		 */
		js: function() {
			var urls      = $.makeArray(arguments).slice(0, -1);
		    var callback  = arguments[arguments.length - 1];

		    if (typeof callback === 'string') {
				urls = [callback];
				callback = null;
		    }

		    (new Resource(null, urls, callback)).load();
		},

		/**
		 * Use this to completely reset the object
		 */
		reset: function()
		{
			this._deferredScripts = [];
			this._resourceMap = {};
			this._log = [];
		}
	};
	return FCL.GET;
})(jQuery);
