
module.exports = new Class
({
	 basePath: null
	,path: null
	,moduleName: null
	,callbacks: []
	,localeReady: false
	,jsReady: false
	,uiReady: false
	,error: false
	,ready: false

	,initialize: function (basePath, path)
	{
		var absPath = basePath +'/'+ path;

		var aux = path.split ('/');
		var moduleName = aux[aux.length - 1];

		Vn.Locale.load (absPath,
			this.onLocaleReady.bind (this));
		Vn.includeJs (absPath +'/'+ moduleName +'.js',
			this.onJsReady.bind (this));
		Vn.loadXml (absPath +'/ui.xml',
			this.onUiReady.bind (this));

		this.basePath = basePath;
		this.path = path;
		this.moduleName = moduleName;
	}
	
	,addCallback: function (callback)
	{
		if (!this.ready)
			this.callbacks.push (callback);
		else
			callback (this);
	}

	,onLocaleReady: function (success)
	{
		this.localeReady = true;
		this.onReady ();
	}

	,onJsReady: function (success)
	{
		this.jsReady = true;
		this.error = !success;
		this.onReady ();
	}
	
	,onUiReady: function (success)
	{
		this.uiReady = true;
		this.error = !success;
		this.onReady ();
	}

	,onReady: function ()
	{
		if (!(this.localeReady && this.jsReady && this.uiReady))
			return;

		this.ready = true;

		var klassName = this.toCamelCase (this.moduleName);
		
		try {
			this.klass = Hedera[klassName];
		}
		catch (e)
		{
			this.error = true;
			console.error (e);
		}

		var callbacks = this.callbacks;
		this.callbacks = null;

		for (var i = 0; i < callbacks.length; i++)	
			callbacks[i] (this);
	}
	
	,toCamelCase: function (dashedName)
	{
		var camelCase = dashedName.charAt (0).toUpperCase ();
		camelCase += dashedName.substr (1).replace (/\w\-\w/g, function (token)
		{
			return token.charAt (0) + token.charAt (2).toUpperCase ();
		});
		return camelCase;
	}
});

