/**
 *
 * @author Ardeleanu Ionut
 * @langversion JAVASCRIPT
 *
 * http://www.neokinetics.ro
 * iardeleanu@neokinetics.ro
 *
 */
function MultiLanguage(){
 	var JSObject = this;
	
	this.array;  			    	//array data from csv file
	this.language = "french";   	//destination language
	this.index;                 	//column index of the current language inside array
	this.ref_language = "english";  //referer language
	this.ref_index;                 //column index of the referer language inside array
	
	
	/*****************************************************************************************/
	/*                                      INIT MULTILANGUAGE                               */
	/*****************************************************************************************/
	/**
	 * load csv file for multilanguage suport
	 * method type: LOCAL
	 * params: @path : CSV url path
	 */
	this.init = function(path) {
 		
		$.get(path, function(data) { 
			JSObject.array = $.csv("~")(data); 
			
			//set language index
			for (var i=0; i<=JSObject.array[0].length-1; i++){
				//alert(JSObject.array[0][i].toLowerCase() + "-"+JSObject.language+"-"+JSObject.ref_language)
				if (JSObject.array[0][i].toLowerCase() == JSObject.language){
					JSObject.index = i;
				}
				if (JSObject.array[0][i].toLowerCase() == JSObject.ref_language){
					JSObject.ref_index = i;
				}
			}
			
			
			//initialize Interface objects
			JSInterface.initObjects();
		}); 

	}
	
	
	
	/*****************************************************************************************/
	/*                                TRANSLATE  INTO SELECTED LANGUAGE                      */
	/*****************************************************************************************/
	/**
	 * translate a string from english to other language
	 * method type: LOCAL
	 * params: @str : string to translate
	 */
	this.translate = function(str,type) {
 		
		for (var i=1; i<=this.array.length-1; i++){
			
			if (this.array[i][this.ref_index] && this.array[i][this.ref_index] != null){
				if (this.array[i][this.ref_index].toLowerCase().stripBlanks() == str.toLowerCase().stripBlanks()){
					
					if (type == "html" || type == null) return this.toHTML(this.array[i][this.index]);
					
					if (type == "alert") return this.toAlert(this.array[i][this.index]);
					
				}
			}
		}
		
		return "";
	}
	
	
	
	/*****************************************************************************************/
	/*                                      TO HTML CHARSET                                  */
	/*****************************************************************************************/
	/**
	 * transform ISO chars to UTF-8 chars: Ex: société -> soci&eacute;t&eacute;
	 * method type: LOCAL
	 * params: @string : the string to convert
	 */
	this.toHTML = function(string) {
 		
		str = string;
		str = str.replace(/Á/g,"&Aacute;");   
		str = str.replace(/á/g,"&aacute;");  
		str = str.replace(/Â/g,"&Acirc;");
		str = str.replace(/â/g,"&acirc;");
		str = str.replace(/Æ/g,"&AElig;");
		str = str.replace(/æ/g,"&aelig;");
		str = str.replace(/À/g,"&Agrave;");
		str = str.replace(/à/g,"&agrave;");
		str = str.replace(/ŕ/g,"&agrave;");
		str = str.replace(/ä/g,"&auml;");
		
		str = str.replace(/Ç/g,"&Ccedil;");   
		str = str.replace(/ç/g,"&ccedil;");  
		
		str = str.replace(/É/g,"&Eacute;");
		str = str.replace(/é/g,"&eacute;");
		str = str.replace(/Ê/g,"&Ecirc;");
		str = str.replace(/Ę/g,"&Ecirc;");
		str = str.replace(/ê/g,"&ecirc;");
		str = str.replace(/ę/g,"&ecirc;");
		str = str.replace(/È/g,"&Egrave;");
		str = str.replace(/è/g,"&egrave;");
		str = str.replace(/č/g,"&egrave;");
		str = str.replace(/ë/g,"&euml;");
		
		str = str.replace(/Í/g,"&Iacute;");   
		str = str.replace(/í/g,"&iacute;");  
		str = str.replace(/Î/g,"&Icirc;");
		str = str.replace(/î/g,"&icirc;");
		str = str.replace(/Ì/g,"&Igrave;");
		str = str.replace(/ì/g,"&igrave;");
		str = str.replace(/Ï/g,"&Iuml;");
		
		str = str.replace(/Ó/g,"&Oacute;");   
		str = str.replace(/ó/g,"&oacute;");  
		str = str.replace(/Ô/g,"&Ocirc;");
		str = str.replace(/ô/g,"&ocirc;");
		str = str.replace(/Œ/g,"&OElig;");
		str = str.replace(/œ/g,"&oelig;");
		str = str.replace(/Ò/g,"&Ograve;");
		str = str.replace(/ò/g,"&ograve;");
		
		str = str.replace(/Ú/g,"&Uacute;");   
		str = str.replace(/ú/g,"&uacute;");  
		str = str.replace(/Û/g,"&Ucirc;");
		str = str.replace(/û/g,"&ucirc;");
		str = str.replace(/ű/g,"&ucirc;");
		str = str.replace(/Ù/g,"&Ugrave;");
		str = str.replace(/ù/g,"&ugrave;");
		
		str = str.replace(/'/g,"'");
		str = str.replace(/`/g,"'");
		
		return str;
	}
	
	
	/*****************************************************************************************/
	/*                                      REVERSE HTML CHARSET                             */
	/*****************************************************************************************/
	/**
	 * transform HTML chars to UTF-8 chars: Ex: soci&eacute;t&eacute; -> société
	 * method type: LOCAL
	 * params: @string : the string to convert
	 */
	this.reverseHTML = function(string) {
 		
		str = string;
		str = str.replace(/&Aacute;/g,"Á");   
		str = str.replace(/&aacute;/g,"á");  
		str = str.replace(/&Acirc;/g,"Â");
		str = str.replace(/&acirc;/g,"â");
		str = str.replace(/&AElig;/g,"Æ");
		str = str.replace(/&aelig;/g,"æ");
		str = str.replace(/&Agrave;/g,"À");
		str = str.replace(/&agrave;/g,"à");
		str = str.replace(/&auml;/g,"ä");
		
		str = str.replace(/&Ccedil;/g,"Ç");   
		str = str.replace(/&ccedil;/g,"ç");  
		
		str = str.replace(/&Eacute;/g,"É");
		str = str.replace(/&eacute;/g,"é");
		str = str.replace(/&Ecirc;/g,"Ê");
		str = str.replace(/&Ecirc;/g,"Ę");
		str = str.replace(/&ecirc;/g,"ê");
		str = str.replace(/&Egrave;/g,"È");
		str = str.replace(/&egrave;/g,"è");
		str = str.replace(/&euml;/g,"ë");
		
		str = str.replace(/&Iacute;/g,"Í");   
		str = str.replace(/&iacute;/g,"í");  
		str = str.replace(/&Icirc;/g,"Î");
		str = str.replace(/&icirc;/g,"î");
		str = str.replace(/&Igrave;/g,"Ì");
		str = str.replace(/&igrave;/g,"ì");
		str = str.replace(/&Iuml;/g,"Ï");
		
		str = str.replace(/&Oacute;/g,"Ó");   
		str = str.replace(/&oacute;/g,"ó");  
		str = str.replace(/&Ocirc;/g,"Ô");
		str = str.replace(/&ocirc;/g,"ô");
		str = str.replace(/&OElig;/g,"Œ");
		str = str.replace(/&oelig;/g,"œ");
		str = str.replace(/&Ograve;/g,"Ò");
		str = str.replace(/&ograve;/g,"ò");
		
		str = str.replace(/&Uacute;/g,"Ú");   
		str = str.replace(/&uacute;/g,"ú");  
		str = str.replace(/&Ucirc;/g,"Û");
		str = str.replace(/&ucirc;/g,"û");
		str = str.replace(/&Ugrave;/g,"Ù");
		str = str.replace(/&ugrave;/g,"ù");
		
		return str;
	}
 	
	
	/*****************************************************************************************/
	/*                                      TO ALERT CHARSET                                 */
	/*****************************************************************************************/
	/**
	 * transform ISO chars to UTF-8 chars: Ex: société -> soci\u00E9t\u00E9
	 * method type: LOCAL
	 * params: @string : the string to convert
	 */
	this.toAlert = function(string) {
 		
		str = string;
		str = str.replace(/Á/g,"\u00C1");   
		str = str.replace(/á/g,"\u00E1");  
		str = str.replace(/Â/g,"\u00C2");
		str = str.replace(/â/g,"\u00E2");
		str = str.replace(/Æ/g,"\u00C6");
		str = str.replace(/æ/g,"\u00E6");
		str = str.replace(/À/g,"\u00C0");
		str = str.replace(/à/g,"\u00E0");
		str = str.replace(/ŕ/g,"\u00E0");
		str = str.replace(/ä/g,"\u00E4");
		
		str = str.replace(/Ç/g,"\u00C7");   
		str = str.replace(/ç/g,"\u00E7");  
		
		str = str.replace(/É/g,"\u00C9");
		str = str.replace(/é/g,"\u00E9");
		str = str.replace(/Ê/g,"\u00CA");
		str = str.replace(/Ę/g,"\u00CA");
		str = str.replace(/ê/g,"\u00EA");
		str = str.replace(/ę/g,"\u00EA");
		str = str.replace(/È/g,"\u00C8");
		str = str.replace(/è/g,"\u00E8");
		str = str.replace(/č/g,"\u00E8");
		str = str.replace(/ë/g,"\u00EB");
		
		str = str.replace(/Í/g,"\u00CD");   
		str = str.replace(/í/g,"\u00ED");  
		str = str.replace(/Î/g,"\u00CE");
		str = str.replace(/î/g,"\u00EE");
		str = str.replace(/Ì/g,"\u00CC");
		str = str.replace(/ì/g,"\u00EC");
		str = str.replace(/Ï/g,"\u00CF");
		
		str = str.replace(/Ó/g,"\u00D3");   
		str = str.replace(/ó/g,"\u00F3");  
		str = str.replace(/Ô/g,"\u00D4");
		str = str.replace(/ô/g,"\u00F4");
		str = str.replace(/Ò/g,"\u00D2");
		str = str.replace(/ò/g,"\u00F2");
		
		str = str.replace(/Ú/g,"\u00DA");   
		str = str.replace(/ú/g,"\u00FA");  
		str = str.replace(/Û/g,"\u00DB");
		str = str.replace(/û/g,"\u00FB");
		str = str.replace(/Ù/g,"\u00D9");
		str = str.replace(/ù/g,"\u00F9");
		
		str = str.replace(/'/g,"\'");
		str = str.replace(/`/g,"\'");
		str = str.replace(/"/g,"\"");
		
		return str;
	}
	
 
}