/** Function:	tran_SwitchLang
 *  Purpose: 	This function is used to switch all string within
 *				spans identified as language specific (with lang="Y")
 *				to the equivalent string in the language specified
 *				as a parm.  If no match is found, the text is left as-is.
 *
 *              a second optional parameter "selector" can limit the translation 
 *              to decendents of the ccs selector passed in
 */
function tran_SwitchLang(target_lang, selector)
{	
	
	var id;
	var newstr;
	if (selector == undefined)
	{
		selector = "body";	
	}	
	
	jQuery(selector+' span[lang="Y"]').each(function(){		
		id = jQuery(this).attr('key');		
		if ((id == "") || (id == null))
		{
			newstr = document.tran_oMultiLangArr[jQuery(this).html()];
		}
		else
		{
			newstr = document.tran_oMultiLangArr[id];
		}	
		// If we found a replacement, replace it.
		if((newstr != null) && (newstr[target_lang] != null))
		{
			jQuery(this).html(newstr[target_lang]);	
		}
	});
} 

/** Function:	tran_SetLangs
 *  Purpose: 	This function is used to initialize the esdi multi-language
 *				engine in order to tell it what alternate languages are valid.
 *				The order of the languages provided to this function equates
 *				to the order of the alternate strings provided to the 
 *				tran_AddLangEntry function, and they also equate to the value
 *				passed to the tran_SwitchLang function
 */
function tran_SetLangs()
{
	var argv = tran_SetLangs.arguments;
	var argc = tran_SetLangs.arguments.length;
	var x = 0;
	lang_array = new Object();
	while(x < argc)
	{
		// Convert the parms into entries in the array
		lang_array[x] = argv[x];
		x++;
	}
	
	// Store the array in a new property of the document so that
	// we can retrieve it later
	document.tran_oSupportedLangs = lang_array;
}


/** Function:	tran_AddLangEntry
 *  Purpose: 	This function is used to create the 'translatation'
 *				table between the native language (the first parm)
 *				and each alternate language (as defined in the
 *				tran_SetLangs call)
 */
function tran_AddLangEntry(native_lang)
{
	var argv = tran_AddLangEntry.arguments;
	var argc = tran_AddLangEntry.arguments.length;
	var x = 1;
	
	// If the table (array) of language entries has not been
	// attached to the document yet, do it now.
	if(!document.tran_oMultiLangArr)
		document.tran_oMultiLangArr = new Object();
	
	// Construct a new language object for this 'record' in the table
	// and assign it the various translational equivalents (this assumes
	// the parms are in the same order as specified in tran_SetLangs
	langstr = new Object();
	while(x < argc)
	{
		langstr[document.tran_oSupportedLangs[x - 1]] = jQuery.trim(argv[x]);
		x++;
	}
	
	// Add the entry to the array
	document.tran_oMultiLangArr[native_lang] = langstr;
	
}


function tran_SwitchLang_old(target_lang)
{
	var i=0; 
	var tokens = document.getElementsByTagName("SPAN"); 
	// Cycle through all spans
	for (i=0; i < tokens.length; i++) 
	{
		// Trap for ones with a lang attribute set to "Y"
		lang = tokens[i].getAttribute("lang");
		if (lang == "Y") 
		{ 
			// If there is an ID attribute, we want to use that
			// to look it up in our replacement table, otherwise
			// use the string itself
			id = tokens[i].getAttribute("key");
			if((id == "") || (id ==null))
				newstr = document.tran_oMultiLangArr[tokens[i].innerHTML];
			else
				newstr = document.tran_oMultiLangArr[id];
			
			// If we found a replacement, replace it.
			if((newstr != null) && (newstr[target_lang] != null))
				tokens[i].innerHTML = newstr[target_lang];
		}
	} 
} 

