﻿/**
 * @namespace Holds all localization methods
 * @uses lib/Prototype/Prototype.js
 * @uses lib/Sureflix/Sureflix.js
 * @uses lib/Utilities/Utils.js
 * @author Amy 05/06/2008
 */ 
Sureflix.Localization = {};

//Shorthand
SxLc = Sureflix.Localization;


/**
 * Gets the current language of the site
 * @author Amy 05/06/2008
 */
Sureflix.Localization.getLang = function () {
	var lang = Sureflix.Utilities.GetCookie("GMNLANG") || "en";	
	return lang;
}


/**
 * Gets the translated text for a phrase
 * @param {String} key The key for the phrase
 * @param {String} lang The language requested for the phrase
 * @param {Array[String]} replaceValues (optional) Array of values to replace in the phrase
 * @example Sureflix.Localization.getPhrase("scene", "fr")
 * @author Amy 05/07/2008
 */
Sureflix.Localization.getPhrase = function (key, lang, replaceValues) {
	if (!lang) lang = Sureflix.Localization.getLang();
	lang = lang.toLowerCase();
	
	var phrase = key;
	key = "GetPhrase" + key;
	
	// if the key/lang pair don't exist
	if(!Sureflix.Caching.isCached(key, lang)){
		
		var url = "http://" + document.location.host + "/Pull/Localization.asmx/GetPhrase";
		new Ajax.Request(url,{method: 'GET',
							asynchronous: false,
							parameters:{key:phrase,lang:lang},
						    onSuccess: function(transport){
											var result = transport.responseXML;
											try {
												if (result){
													Sureflix.Caching.setCachedObject(key, lang, result.getElementsByTagName("Phrase")[0].childNodes[0].nodeValue);
												}
											} catch(e) {
											}
										}
						});	
	}
	// need to assign this to a new variable because we don't want to replace
	// the values in the translation, only in what we are returning
	var phraseTrans = Sureflix.Caching.getCachedObject(key, lang);

	if(replaceValues && phraseTrans){
		for (var phraseIndex = 0; phraseIndex < replaceValues.length; phraseIndex++) 
		{
			var arg = replaceValues[phraseIndex] || "";
			var phraseRetext = "%%" + (phraseIndex+1) + "%%";
			var re = new RegExp( phraseRetext,"g");
			phraseTrans = phraseTrans.replace(re, arg);
		}
		
		//replace double quotations to prevent javascript errors
		phraseTrans = phraseTrans.replace( /\'/gi, "`" );
		phraseTrans = phraseTrans.replace(/\"/g,"”");
	}
	return phraseTrans;
}


/**
 * Gets the list of key-value data
 * @param {String} key The criteria for the data
 * @param {String} lang The language requested for the data
 * @param {String} movieZone The zone of the movie
 * @example Sureflix.Localization.getKeyValueData("category", "MovieAdded", "fr")
 * @author Amy 10/15/2008
 */
Sureflix.Localization.getKeyValueData = function (criteria, movieZone, lang, tagZone){
	if (!lang) lang = Sureflix.Localization.getLang();
	lang = lang.toLowerCase();
	
	var key = "GetKeyValueSearchComboData" + criteria + movieZone + tagZone;
	// if the key/lang pair don't exist
	if(!Sureflix.Caching.isCached(key, lang)){
		
		var url = "http://" + document.location.host + "/Pull/SmartSearchFromDB.asmx/GetKeyValueSearchComboData";
		new Ajax.Request(url,{method: 'GET',
							asynchronous: false,
							parameters: { criteria: criteria, movieZone: movieZone, lang: lang, tagZone: tagZone },
						    onSuccess: function(transport){
											var result = transport.responseXML;
											try {
												if (result)	Sureflix.Caching.setCachedObject(key, lang, result);
											} catch(e) {}
										}
						});	
	}
	return Sureflix.Caching.getCachedObject(key, lang);
}