/**
 * @class Basic internationalization (i18n) service
 * @constructor
 * @param translations the catalog of translations to use
 * @param options options see the class attribute
 */

function I18n(culture, options) {
	
	this.culture = culture;
	this.options = options;
	this.defaultCultures = new Array('en', 'fr', 'it');
	this.catalogTranslate = new Array();
	//alert(this.culture);
	if(this.defaultCultures.indexOf(this.culture) != -1){
		
		var translations = new CatalogTranslation();
		this.catalogTranslate = translations.catalog;
	}
	
    };
   
  
    I18n.instance = null; // Will contain the one and only instance of the class
  
    // This function ensures that I always use the same instance of the object
    I18n.getInstance = function( culture ) {
    
            if (I18n.instance == null) {
            	
            	I18n.instance = new I18n( culture );   
            }
            
            return I18n.instance;
    
    };
    
    
    I18n.prototype.getTranslate = function() {
    
            return this.translate;
    
    };
    
    I18n.prototype.getCulture = function() {
        
        return this.culture;

    };
    
    /**
	 * search the translation of key into the catalog and substitute the params.
	 * ex: key = "test", params = ["toto"] => if (catalog[key] == "%1") then
	 * toto
	 * 
	 * @param key
	 *            the key to search for in the catalog
	 * @param params
	 *            the value to substitute in the string
	 */
    
    I18n.prototype.getTranslate = function( key, params ) {
        
           params = params || [];
           var t = this.catalogTranslate[key];
   
            // if the translation does not exist, use the key
            if (! t || this.culture == 'es') {
               
            	t = key;
            }

            //replace %1, %2, ... by the args
            for (var i = 0; i < params.length; i++) {
                //all %X not preceded by %
                var regexp = new RegExp("([^%]|^)%" + (i + 1), 'gi');
                t = t.replace(regexp, "$1" + params[i]);
            }

            return t;
        };

//global function for translate 
 function __( key, params ){

	var i18n = I18n.getInstance();

	return i18n.getTranslate( key, params );

  };