var Localization = function(param_data_path , param_i18n_path, param_force_locale) {
						
	this.dataPath = param_data_path;
	this.i18nPath = param_i18n_path;
	this.forceLocale = param_force_locale;
	
	this.currentLocaleIndex;
	this.defaultLocaleID;
	this.dictionary;
	this.countryList ;
	this.allowOverwrite;
	this.definitionsFilesQueue;
	
	var ref = this;


	$.ajax({
		  url: this.dataPath + 'locales.xml',
		  success: function(param_data) {
				var xml = jQuery.createXMLDocument(param_data);
				ref.countryList = new XML2Object(xml).finalObject.locales;
				


				if (ref.forceLocale != undefined) {
					if(ref.localeExists(ref.forceLocale)){
						ref.chooseLocale(ref.forceLocale);
						return false;
					}
					
					//else {
						//if (Prosaic.config.get('force_locale') != '')
							//this.chooseLocale(Prosaic.config.get('force_locale'));
					//	else
						//	$(document).trigger('localization_ready');
				//	}
					
				}

				var discreteLocale = ref.getDiscreteLocale();
				if(discreteLocale != undefined)
					ref.chooseLocale(discreteLocale);
				else
					$(document).trigger('localization_ready');

			},
		  dataType: 'html'
		});
		
		
};
			
		
Localization.prototype = {
		
	
	getCountryList:function() {
		return countryList.concat();
	},
	
	
	setOverwrite:function(param_to) {
		allowOverwrite = param_to;
	},
	
	
	chooseLocale:function(param_locale) {
		console.log('Localization : choosing locale' , param_locale);
		this.forceLocale  = '';
		var index = this.getLocaleIndex(param_locale);
		if (index < 0) {
			console.error('Localization : locale ' + param_locale + ' does not exist'); 
			return;
		}
		this.currentLocaleIndex = index;
		this.purgeDictionary();
		this.loadDefinitions(this.i18nPath +  this.getLocale()  + '/lang.xml');
	},
	
	
	loadDefinitions:function(param_path){
		$.ajax({
			  url: param_path,
			  success: function(param_data) {
					var xml = jQuery.createXMLDocument(param_data);
					var srcObject = new XML2Object(xml).finalObject;
					for (var i in srcObject) {
						//current.dictionary[i] = srcObject[i];
					}
					$(document).trigger('localization_ready');
				},
			  dataType: 'html'
			});
	},
	
	
	getLocale:function() {
		if (this.currentLocaleIndex == undefined) {
			console.warn('Localization : locale has not been chosen yet');
			return '';
		}
		return this.countryList[this.currentLocaleIndex].locale;
	},
	
	
	 purgeDictionary:function(){
		dictionary  = {}
	},

	
	addDefinition:function(param_key , param_value) {
		if (this.dictionary.hasOwnProperty(param_key)) {
			if (!allowOverwrite) {
				console.error('Localization : an attempt to overwrite "' + param_key +'" has been denied' );
				return;
			}
			console.warn('Localization : definition "' + param_key +'" has been overwritten');
		}
		this.dictionary[param_key] = param_value;
	},
	
	
	get:function(param_key){
		if (!this.hasDefinition(param_key)) {
			console.warn('Localization : no definition found for "' + param_key + '"');
			return param_key.toUpperCase();
		}
		
		return this.dictionary[param_key];	
	},
	
	
	hasDefinition:function(param_key) {
		if (!dictionary.hasOwnProperty(param_key))
			return false;
		return true;
	},
	
	
	localeExists:function(param_locale){
		if (this.getLocaleIndex(param_locale) > -1)
			return true;
		return false;
	},
	
	
	getLocaleInfo:function(param_locale){
		var index = this.getLocaleIndex(param_locale);
		if (index == -1)
			return null;
		return this.countryList[index];
	},
	
	
	getDiscreteLocale:function(){
		var locale = undefined;
		var tabLength = this.countryList.length
		for(var i = 0; i < tabLength ; i++){
			if(this.countryList[i].hasOwnProperty('show_locale_in_url') && this.countryList[i].show_locale_in_url == 'false')
				locale = this.countryList[i].locale;
		}
		return locale;
	},
	
	
	getLocaleIndex:function(param_locale) {
		var tabLength = this.countryList.length
		for(var i = 0; i < tabLength ; i++){
			if(this.countryList[i].locale == param_locale){
				return i;
			}
		}
		return -1;
	},
		

};
