//if (typeof(IMPORT_JS) != 'function')
//	alert(document.location);

IMPORT_JS("/res/js/dom/CMDom.js");
IMPORT_JS("/res/js/util/strftime.js");
IMPORT_JS("/res/js/util/i18n.js");
IMPORT_JS("/cgi/CMLocale.js");



var LANGUAGE_DEBUG = false;
var USE_XML = true;

var topWnd = getTopWindow();
if (!topWnd.languageContainer)
	topWnd.languageContainer = new Object();
var theLanguageContainer = topWnd.languageContainer;


function getLanguage() {
	if (typeof(theLanguage) == 'undefined') {
		var topWnd = getTopWindow();
		theLanguage = topWnd.theLanguage;
	}
	return theLanguage;
}
function getCountry() {
	if (typeof(theCountry) == 'undefined') {
		var topWnd = getTopWindow();
		theCountry = topWnd.theCountry;
	}
	return theCountry;
}



CMLanguage = function() {
}

CMLanguage.formatByteSize = function(bytes, thousandBase, language, country) {

	var topWnd = getTopWindow();
	if (!language)
		language = topWnd.theLanguage;
	if (!country)
		country = topWnd.theCountry;

	return CMLocale.formatByteSize(bytes, thousandBase, language, country);

}

CMLanguage.formatNumber = function(number, language, country) {

	var topWnd = getTopWindow();
	if (!language)
		language = topWnd.theLanguage;
	if (!country)
		country = topWnd.theCountry;

	return CMLocale.formatNumber(number, language, country);
}

CMLanguage.fillThousand = function(number) {
	return CMLocale.fillThousand(number);
}

CMLanguage.ensureLanguageObject = function(objectName, language, country) {
	var objectName2 = objectName;
	if (!objectName) {
		objectName2 = "dummy_Object";
		objectName = "";
	}
	objectName2 = objectName2 + "_" + language + "_" + country;

	if (/*LANGUAGE_DEBUG ||*/ typeof(theLanguageContainer[objectName2]) == "undefined") {
		theLanguageContainer[objectName2] = new Object();

		var parser = new CMDOMParser();
		if (USE_XML) {
			var dir = "/";
			if (objectName.indexOf("oem_") == 0 && typeof(window.top.resDescription.main.oemLanguageRoot) != 'undefined')
				dir = window.top.resDescription.main.oemLanguageRoot;
			parser.load(dir + "language_" + objectName + ".xml");
		} else {
			parser.load(
					  "/servlet/GetLanguage"
					+ "?language=" 		+ language
					+ "&country=" 		+ country
					+ "&objectName=" 	+ objectName
			);
		}
		var doc = parser.getDocument();
		theLanguageContainer[objectName2].__doc__ = doc;
		if (doc && doc.documentElement) {
			for (var child = doc.documentElement.firstChild; child != null; child = child.nextSibling) {
				if(child.nodeType != 1 /*ELEMENT_NODE*/){
					continue;
				}
				var variable  = new Object();
				//set language
				variable.language = child.getAttribute("language");
				//set country
				variable.country = child.getAttribute("country");
				//set text
				{
					var valueChild = child.firstChild;
					while (valueChild.nodeName != "VALUE"){
						valueChild = valueChild.nextSibling;
					}
					for(var valueText = valueChild.firstChild;valueText != null;valueText = valueText.nextSibling){
						if(valueText.nodeType == 4 /*CDATA_SECTION_NODE*/){
							variable.value = valueText.nodeValue;
						}
					}
				}
				theLanguageContainer[objectName2][child.getAttribute("variable")] = variable;
			}
		}
	}
}

CMLanguage.getLanguageVariable = function(objectName, variableName, language, country, defaultStr) {

	var objectName2 = objectName;
	if (!objectName) {
		objectName = "";
		objectName2 = "dummy_Object";
	}

	objectName2 = objectName2 + "_" + language + "_" + country;


	if (theLanguageContainer[objectName2][variableName]) {
		if (LANGUAGE_DEBUG)
			return theLanguageContainer[objectName2][variableName].value;
		else
			return theLanguageContainer[objectName2][variableName].value;
	} else {
		if (LANGUAGE_DEBUG)
			alert(objectName + "_" + language + "_" + country + "." + variableName )
		if (typeof(defaultStr) != 'undefined')
			return defaultStr;
		else {
			var str = I18N.xlate(objectName, variableName);
			if (!str)
				str = "|#|#| Missing text (" + objectName + "_" + language + "_" + country + "." + variableName + ") |#|#|";
			return str;
		}
	}
}


CMLanguage.setLanguageVariable = function(objectName, variableName, text, language, country) {

	var objectName2 = objectName;
	if (!objectName) {
		objectName = "";
		objectName2 = "dummy_Object";
	}

	objectName2 = objectName2 + "_" + language + "_" + country;

	var variable  = new Object();
	//set language
	variable.language = language;
	//set country
	variable.country = country;
	//set text
	variable.value = text;
	theLanguageContainer[objectName2][variableName] = variable;

}


// Alle Methoden im language-Objekt und auch Solo
function getText(name, language, country, defaultStr) {

	var objectName;
	var variableName;
	var idx = name.indexOf(".");
	if (idx == -1) {
		objectName = "";
		variableName = name;
	} else {
		objectName = name.substring(0,idx);
		variableName = name.substring(idx+1);
	}

	var topWnd = getTopWindow();
	if (!language)
		language = topWnd.theLanguage;
	if (!country)
		country = topWnd.theCountry;
	// Fuer Sprachvariablen, denen kein Objekt zugeordnet ist.
	if (!objectName)
		objectName = "dummy_Object";

	CMLanguage.ensureLanguageObject(objectName, language, country);
	return CMLanguage.getLanguageVariable(objectName, variableName, language, country, defaultStr);
}


function setText(name, text, language, country) {

	var objectName;
	var variableName;
	var idx = name.indexOf(".");
	if (idx == -1) {
		objectName = "";
		variableName = name;
	} else {
		objectName = name.substring(0,idx);
		variableName = name.substring(idx+1);
	}

	var topWnd = getTopWindow();
	if (!language)
		language = topWnd.theLanguage;
	if (!country)
		country = topWnd.theCountry;
	// Fuer Sprachvariablen, denen kein Objekt zugeordnet ist.
	if (!objectName)
		objectName = "dummy_Object";

	CMLanguage.ensureLanguageObject(objectName, language, country);
	CMLanguage.setLanguageVariable(objectName, variableName, text, language, country);
}

function setTextObject(objname, obj, language, country) {

	for (var variable in obj) {
		setText(objname + "." + variable, obj[variable], language, country);
	}

}

function setTextLabels(doc) {
	doc = (doc || document);
	var nl = doc.getElementsByTagName("span");

	for (var i=0; i<nl.length; i++) {
		var span = nl.item(i);
		var id = span.id;
		var type = span.getAttribute("type");
		if (type == "text") {
			var html = getText(id);
			if (html)
				Element.update(span, html);
		}
	}
}

