
var SdEcValues = {};		// public  global identifiers
var $_EcValues = {};		// private global identifiers

$_EcValues.parameter_cache = {};
$_EcValues.variables_cache = {};

$_EcValues.translations = {};

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.getField = function (symbol, context, empty)
{
	var  definition = context["definition"];
	if (!definition) SdCommon.error("no definition in context: " + SdCommon.mapToText(context));
	var field = definition[symbol];
	return field;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.setVariable = function (name, value)
{
	if (typeof value === 'function') throw "cannot set variable '" + name + "' as a function with value '" + value + "'"; 
	$_EcValues.variables_cache[name] = value;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$_EcValues.getVariables = function ()
{
	var map = {};
	for (var name in $_EcValues.variables_cache)
	{
		var value = $_EcValues.variables_cache[name];
		if (typeof value === 'function') continue;
		map[name] = value;
	}
	return map;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.setParameter = function (name, value)
{
	var check = $_EcValues.parameter_cache[name];
//	SdCommon.log( "write parameter '" + name + "' with '" + value + "' (was: '" + check + "')" );
	if (typeof value === 'function') throw "cannot set parameter '" + name + "' as a function with value '" + value + "'"; 
	if (check) throw "can not overwrite parameter '" + name + "' with '" + value + "' (was: '" + check + "')"; 
	$_EcValues.parameter_cache[name] = value;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.getParameter = function (name)
{
	var value = $_EcValues.parameter_cache[name];
	return value;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.getParameters = function (document)
{
	var map = $_EcValues.getVariables();
	for (var name in $_EcValues.parameter_cache)
	{
		var value = $_EcValues.parameter_cache[name];
		if (typeof value === 'function') continue;
		map[name] = value;
	}
	return map;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.getTranslation = function (original)
{
	var  translation = $_EcValues.translations[original];
	if ( translation && translation.charAt(0) == '$' ) translation = null;
	if (!translation) translation = "(" + original + ")";
	return translation;
} 

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.setTranslation = function (original, translation)
{
	$_EcValues.translations[original] = translation;
} 

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

$_EcValues.getValue = function (symbol, context)
{
	if (context["jason-type"] != "context") SdCommon.error("invalid context");

	var definition = context["definition"];
	var properties = context["properties"];
	var parameters = context["parameters"];

	var value = definition[symbol];

       if (!value) value = properties[symbol];
       if (!value) value = parameters[symbol];
       if (!value) value =                '' ;

	return value;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

SdEcValues.populate = function (text, context)
{
	while (true)
	{
		var indexOfLeftBrace = text.indexOf('{');
		if (indexOfLeftBrace < 0) break;

		var indexOfRightBrace = text.indexOf('}', indexOfLeftBrace);
		if (indexOfRightBrace < 0) break;

		var prefix = text.substring(0, indexOfLeftBrace                       );
		var middle = text.substring(   indexOfLeftBrace+1, indexOfRightBrace  );
		var suffix = text.substring(                       indexOfRightBrace+1);

		var replacement = $_EcValues.getValue(middle, context);

		text = prefix + replacement + suffix;

	}
	return text;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


