﻿//Add the following lines to any HTML page that needs to use ZStrings...
//<meta http-equiv="Content-Script-Type" content="text/javascript"/>
//<script src="cookie_support.js"></script>
//<script src="ZString.js"></script>
//Then, specify which dictionaries you will need (psxHtml1.txt is loaded by default)...
//<script>ZString.loadDictionary('legal')</script>
//Valid dictionaries are 'legal', and 'landing',
//Then, anywhere a string needs to be localized, insert the following...
//<script>ZString.write(...)</script>

function ZString()
{
	//Properties:
	this.instance = null;
	this.locale = "";
	this.dictFiles = {
		html: null,
		legal: null,
		landing: null
		};

	this.stringData = {
		html: null,
		legal: null,
		landing: null
		};

	//consts...
	this.FILE_NAMES = {
			html: 'psxHtml1.js',
			legal: 'psxLegal1.js',
			landing: 'psxLanding1.js'
			};
	this.PARAM_PATTERN = /\{\w*\}+/g;
	this.INVALID_STRING = "INVALID STRING";
	this.STRING_MACROS = {
			//These are required to be surrounded by {}'s:
			TAB: '\t',
			AMP: '&',
			LB: '{',
			RB: '}',
			NL: '\n',
			BS: '\\',
			SQ: '\'',
			DQ: '\"',
			LQ: '“',
			RQ: '”',
			AP: '’',
			A: '\u0410',
			W: '​',		//Zero Width Space
			EL: '</a>',		//DEPRECATED: Don't use for new strings.
			B: '<b>',		//DEPRECATED: Don't use for new strings.
			EB: '</b>',		//DEPRECATED: Don't use for new strings.
			U: '<u>',		//DEPRECATED: Don't use for new strings.
			EU: '</u>',		//DEPRECATED: Don't use for new strings.
			I: '<i>',		//DEPRECATED: Don't use for new strings.
			EI: '</i>',		//DEPRECATED: Don't use for new strings.
			BR: '<br/>',		//DEPRECATED: Don't use for new strings.
			LI: '<li>',		//DEPRECATED: Don't use for new strings.
			ELI: '</li>'		//DEPRECATED: Don't use for new strings.
			};

	this.LOCALIZED_GO_URLS = {
			product_select_photoshop:	'$$$/html/go/product_select_photoshop=http://www.adobe.com/go/product_select_photoshop/',
			product_select_lightroom:	'$$$/html/go/product_select_lightroom=http://www.adobe.com/go/product_select_lightroom/',
			cas_photog_pepe_learnmore:	'$$$/html/go/cas_photog_pepe_learnmore=http://www.adobe.com/go/cas_photog_pepe_learnmore/',
			privacy:			'$$$/html/go/privacy=http://www.adobe.com/go/privacy/',
			getflashplayer:			'$$$/html/go/getflashplayer=http://www.adobe.com/go/getflashplayer/',
			photoshopfamily:		'$$$/html/go/photoshopfamily=http://www.adobe.com/go/photoshopfamily/',
			eulas:				'$$$/html/go/eulas=http://www.adobe.com/go/eulas/',
			thirdparty:			'$$$/html/go/thirdparty=http://www.adobe.com/go/thirdparty/',
			copyright:			'$$$/html/go/copyright=http://www.adobe.com/go/copyright/'
			};

	//----------------------------------------
	this.substituteParameters = function(str, parameters)
	{
		var results = str.match(this.PARAM_PATTERN);
		for (var i = 0; i < results.length; i++)
		{
			var itemStr = results[i];
			itemStr = itemStr.substring( 1, itemStr.length - 1); 
			var replacement = null;
			if (parameters)
				replacement = parameters[itemStr];
			if (replacement == null)
				replacement = this.STRING_MACROS[itemStr];
			if (replacement == null)
				replacement = "";
			var result = str.replace(results[i], replacement);
			str = result;
		}
		return str;
	}

	this.parseDictFile = function(dict)
	{
		if (!this.locale || this.stringData[dict] || !this.dictFiles[dict])
			return;
		
		var dictFile = this.FILE_NAMES[dict];
		this.stringData[dict] = new Object();
		var data = this.dictFiles[dict];

		for (index in data)
		{
			var str = data[index];
			if (!str)
				continue;
			var splitIndex = str.indexOf('=');
			var quoted = false;
			if (str.charAt(0) == '"')
				quoted = true;
			var key = str.substring(4 + (quoted ? 1 : 0), splitIndex);	//skip over leading $$$/
			var value = str.substring(splitIndex + 1, str.length - (quoted ? 1 : 0));
			this.stringData[dict][key] = value;
		}
	}
}

ZString.inst = function()
{
	if (ZString.instance == null)
	{
		ZString.instance = new ZString();
		var zinst = ZString.instance;
		zinst.locale = getCookie("locale");
		if (zinst.locale == "en_US")
			zinst.locale = "";
		ZString.loadDictionary('html');
	}
	return ZString.instance;
}

ZString.loadDictionary = function(dict)
{
	var zinst = ZString.inst();
	if (!zinst.locale)
		return;
	if (!zinst.FILE_NAMES[dict])
	{
		alert('Invalid language dictionary "' + dict + '"');
		return;
	}
	var head = document.getElementsByTagName("head")[0];
	var script = document.createElement('script');
	script.id = 'lang_' + dict;
	script.type = 'text/javascript';
	script.src = 'dictionaries/' + zinst.locale.toLowerCase() + '/' + zinst.FILE_NAMES[dict];
	head.appendChild(script);
}

ZString.get = function(resId, parameters)
{
	var returnString;
	var zinst = ZString.inst();

	var splitPoint = resId.indexOf('=');
	if (splitPoint == -1)
		return zinst.INVALID_STRING;

	var quoted = false;
	if (resId.charAt(0) == '\'' || resId.charAt(0) == '"')
		quoted = true;
	var resIdToUse = resId.substring((quoted ? 5 : 4), splitPoint);	//We skip over the '$$$/' part to save memory.
	var defaultString = resId.substring(splitPoint + 1, resId.length - (quoted ? 1 : 0));
	//Make sure there are no double quotes in the English string...
	if (defaultString.indexOf('"') != -1)
	{
		alert('Double quotes " are not allowed in strings.  Use {DQ}, {LQ}, or {RQ} instead:\n$$$/' + resIdToUse);
		return zinst.INVALID_STRING;
	}
	var dictFile = resIdToUse.substr(0, resIdToUse.indexOf('/'));
	zinst.parseDictFile(dictFile);
				
	if (zinst.locale && zinst.stringData[dictFile])
		returnString = zinst.stringData[dictFile][resIdToUse];
				
	if (!returnString)
		returnString = defaultString;
		
	if (parameters || (returnString.indexOf('{') != -1))
		returnString = zinst.substituteParameters(returnString, parameters);

	return returnString;
}

ZString.write = function(resId, parameters)
{
	document.write(ZString.get(resId, parameters));
}

ZString.setAlt = function(element, resId, parameters)
{
	document.getElementById(element).alt = ZString.get(resId, parameters);
}

ZString.setTitle = function(element, resId, parameters)
{
	document.getElementById(element).title = ZString.get(resId, parameters);
}

ZString.setPageTitle = function(resId, parameters)
{
	document.title = ZString.get(resId, parameters);
}

ZString.getGoUrl = function(goKey)
{
	var zinst = ZString.inst();
	var zStringKey = zinst.LOCALIZED_GO_URLS[goKey];
	if (zStringKey)
	{
		var result = ZString.get(zStringKey);	//Is localized
		return result.substr(result.indexOf('http'));	//Remove tags from beginning of string. 
	}
	else
		return 'http://www.adobe.com/go/' + goKey + '/';	//Is not localized
}

ZString.writeGoUrl = function(goKey)
{
	document.write(ZString.getGoUrl(goKey));
}

ZString.getGoUrlHref = function(goKey, useLinkAsText)
{
    if (useLinkAsText == null)
        useLinkAsText = true;
    var result = '<a href="' + ZString.getGoUrl(goKey) + '">';
    if (useLinkAsText)
        result += ZString.getGoUrl(goKey) + '</a>';
    return result;
}

ZString.getStringData = function(dictFile)
{
    var zinst = ZString.inst();
	zinst.parseDictFile(dictFile);
    return zinst.stringData[dictFile];    
}

ZString.getStringMacros = function()
{
    var zinst = ZString.inst();
    return zinst.STRING_MACROS;
}

ZString.inst();
