Ext.namespace("Ext.ux");

Ext.ux.Language = function(config){
	Ext.apply(this, config);
	this.literals = this.literals || [];
	this.languageFile = null;
	this.loadLanguageFile(this.defaultLanguage);
};

Ext.ux.Language.prototype = {

	//private
	loadLanguageFile: function(language){
		Ext.Ajax.request({
			url: this.url,
			scope: this,
			success: function(o){
				this.languageFile = o.responseXML;
				this.applyLanguage(language || this.defaultLanguage);
			},
		    failure: function(){alert("error 1: loadLanguageFile")}
		});
	},
	
	applyLanguage: function(language){
		var aux = Ext.DomQuery.select('literal', Ext.DomQuery.selectNode(language, this.languageFile))
		if(aux){
			for(var i=0; i<=this.literals.length-1; i++){
				var aux1 = Ext.DomQuery.filter(aux, '[id=' + this.literals[i] + ']');
				Ext.get(this.literals[i]).dom.innerHTML = aux1[0].text;
			}
		}else{alert("error 2: applyLanguage")}
	},
	
	//public
	swapLanguage: function(language){
		if(this.languageFile){
			this.applyLanguage(language);
		}else{
			this.loadLanguageFile(language);
		}
	}
	
}