
var NG_TRANSLATE_LANG_KEY = 'NG_TRANSLATE_LANG_KEY';

var lang = new function () {

	// -------------------------------------------------- Local Variables --------------------------------------------------

	var languages = {};
	var currentLang = null;
	var currentLangName = null;
	var globalVars = { };

	// -------------------------------------------------- Methods --------------------------------------------------

	var init = function (preferredLanguage, globals) {
		if (globals) {
			globalVars = globals;
		}

		preferredLanguage = preferredLanguage || 'en_US';
		set(preferredLanguage);
		localStorage.setItem(NG_TRANSLATE_LANG_KEY, preferredLanguage);
	};

	var get = function (langName) {
		if (langName === undefined) {
			return currentLangName;
		} else {
			var lang = languages[langName];
			if (lang === undefined) {
				lang = {};
				languages[langName] = lang;
			}
			return lang;
		}
	};

	var set = function (langName) {
		currentLangName = langName;
		currentLang = get(langName);
		apply();

		switch (langName) {
			case 'en_US':
				$("#selectEnUsButton").addClass('active');
				$("#selectPtBrButton").removeClass('active');
				break;
			case 'pt_BR':
				$("#selectPtBrButton").addClass('active');
				$("#selectEnUsButton").removeClass('active');
				break;
		};


		localStorage.setItem('NG_TRANSLATE_LANG_KEY', langName);
	};

	var apply = function () {
		$('[data-message]').each(function () {
			var e = $(this);
			var key = e.attr('data-message');
			e.text(getMessage(key));
		});
	};

	var add = function (lang, msgs) {
		languages[lang] = msgs;
	};

	var addMessage = function (key, msgs) {
		$.each(msgs, function (lang, msg) {
			get(lang)[key] = msg;
		});
	};

	var getMessage = function (key) {
		var s = currentLang[key];
		if (s === undefined) {
			if (console.log) {
				console.log('Message not found: ' + key);
			}
			s = key;
		}
		for (var varName in globalVars) {
			if (globalVars.hasOwnProperty(varName)) {
				s = s.replace(new RegExp('{{' + varName + '}}', 'g'), globalVars[varName]);
			}
		}
		return s;
	};

	var getAllMessages = function () {
		return currentLang;
	};

	// -------------------------------------------------- Export public methods --------------------------------------------------

	this.init = init;
	this.get = get;
	this.set = set;
	this.apply = apply;
	this.add = add;
	this.addMessage = addMessage;
	this.getMessage = getMessage;
};

lang.add('en_US', {
	documentation: 'Documentation',
	login: 'Sign in',
	digitalSignature: "Digital Certification solution for your application",
	enable: "Enable digital signatures, validations and authenticate users with X.509 certificates",
	signUp: "Get started",
	knowMore: "See pricing",
	privacyPolicy: "Privacy Policy",

	please: ''
});

lang.add('pt_BR', {
	documentation: 'Documentação',
	login: 'Entrar',
	digitalSignature: "Certificação Digital para sua aplicação",
	enable: "Habilite assinaturas digitais, validações da cadeia ICP-Brasil e o uso de certificados X.509 para login",
	signUp: "Comece sem custo",
	knowMore: "Conheça os preços",
	privacyPolicy: "Política de Privacidade",

	_: ''

});


(function () {

	var setupView = null;

	var init = function () {
		$('#selectEnUsButton').click(function () {
			lang.set('en_US');
			$(this).addClass('active');
			$("#selectPtBrButton").removeClass('active');
		});
		$('#selectPtBrButton').click(function () {
			lang.set('pt_BR');
			$(this).addClass('active');
			$('#selectEnUsButton').removeClass('active');
		});
		$('[data-question]').each(function () {
			var e = $(this);
			var q = e.attr('data-question');
			e.find('h3 > a').click(function () {
				showAnswer(q);
			});
		});

		var defaultLanguage = localStorage.getItem(NG_TRANSLATE_LANG_KEY);
		if (defaultLanguage) {
			lang.set(defaultLanguage);
		} else if (/^pt(-)*[\w]*$/.test(navigator.language)) {
			lang.set('pt_BR');
		} else {
			lang.set('en_US');
		}

		$('.button-collapse').sideNav();

	};

	$(init);
})();
