/* Javascript library to create the l10nComponent for BigAdmin articles. */
var l10nKey = "";
var l10nCurrentLocale = "";
var queryURL = "/tools/global/linker.xml";
function checkLocaleLinker() {
	var l10nStyleOutput = "<style type=\"text/css\">" +
			"a.l10n_selector_icon {border: 1px solid #E76F00;background-color:#fff;padding: 4px 0px 0px 2px;margin-left:3px;}" +
			"a.l10n_selector_icon:hover {background-color: #FFC726;text-decoration:none;}" +
			"#l10n_selector_block{float:right;padding:2px;margin:3px 3px 6px 3px;clear:right;display:none;}</style>\n";
	var l10nSelectorBlock = "<div id=\"l10n_selector_block\"></div>\n";
	document.write(l10nStyleOutput);document.write(l10nSelectorBlock);
	makeHttpRequest(queryURL,'baParseLocaleResults',true,null);
}
function baParseLocaleResults(responseXML) {
	var resultOut = "";
	var localizedContent = responseXML.getElementsByTagName("localizedContent")[0];
	var articles = localizedContent.getElementsByTagName("article");
	var articleKey = "";
	var docLocation = window.location;
	var docPath = docLocation.pathname;
	var	foundKey = false;
	var resultBlock;
	
	// Go through the articles until we find out key
	if (l10nKey != "" && articles.length > 0) {
		for (var i = 0; i < articles.length; i++) {
			var result = articles[i];
			var translations = result.getElementsByTagName("translation");
			var currentKey = "";
			var l10nIconsList = "";
				
			var transValue = "";
			var transLang = "";
			var transLangIconCode = "";
				
			// Do we have a key?
			if (result.getAttribute("key")) {currentKey = result.getAttribute("key");} else {currentKey = "";}
				
			// Did we find what we need? If so, process, else next!
			if ((currentKey != "" && l10nKey != "") && currentKey == l10nKey) {
				foundKey = true;
				translations = result.getElementsByTagName("translation");
					
				if (translations.length > 0) {
					for (var j = 0; j < translations.length; j++) {
						var trans = translations[j];
						transValue = "";
						transLang = "";
						transLangIconCode = "";

						if (trans.getAttribute("lang")) {
							transLang = trans.getAttribute("lang");
							if (transLang != l10nCurrentLocale)
								transLangIconCode = getLocalizedIcon(transLang);
						}

						if (trans.textContent != null) {
							transValue = trans.textContent;
						} else if (trans.firstChild.nodeValue) {
							transValue = trans.firstChild.nodeValue;
						} else if (trans.nodeValue) {
							transValue = trans.nodeValue;
						}
							
						if (transLang != "" && transValue != "" && transLangIconCode != "") {
							l10nIconsList += '\n<a  class="l10n_selector_icon" href="' + transValue + '">' + transLangIconCode + '<\/option>';
						}
					}
				}
				break;
			}
		} 
	}
		
	if (foundKey && l10nIconsList != "") {
		resultBlock = getDocObject('l10n_selector_block');
		resultBlock.innerHTML = l10nIconsList;
		resultBlock.style.display = "block";
	}
}
function getLocalizedIcon(langKey) {
	var langIcon = ""; var returnIcon = "";
	var iconWidth = 0; var iconHeight = 0;
	switch (langKey) {
		case "en-US":
			langIcon = "/tools/global/images/en.gif";
			iconWidth = 40; iconHeight = 13;
			break;
		case "ja-JP":
			langIcon = "/tools/global/images/ja.gif";
			iconWidth = 44; iconHeight = 13;
			break;
		case "zh-CN":
			langIcon = "/tools/global/images/zh-cn.gif";
			iconWidth = 47; iconHeight = 13;
			break;
		case "es-ES":
			langIcon = "/tools/global/images/es.gif";
			iconWidth = 44; iconHeight = 13;
			break;
		case "fr-FR":
			langIcon = "/tools/global/images/fr-FR.gif";
			iconWidth = 49; iconHeight = 13;
			break;
		case "ko-KR":
			langIcon = "/tools/global/images/ko-KR.gif";
			iconWidth = 40; iconHeight = 13;
			break;
		case "zh-TW":
			langIcon = "/tools/global/images/zh-TW.gif";
			iconWidth = 48; iconHeight = 13;
			break;
		default:
			break;
	}
	if (langIcon != "" && iconWidth != 0 && iconHeight != 0) {
		returnIcon = "<img src=\"" + langIcon + "\" width=\"" + iconWidth + "\" " +
		             "height=\"" + iconHeight + "\" border=\"0\"/>";
	}
	return returnIcon;
}
