(function () {
	window.intgrtnI18n = {};
	window.intgrtnI18n.getLanguage = function (language) {
		switch (language) {
			case "ar":
			case "bo":
			case "cl":
			case "co":
			case "cu":
			case "cr":
			case "do":
			case "ec":
			case "gt":
			case "gq":
			case "hn":
			case "mx":
			case "ni":
			case "pa":
			case "pe":
			case "pr":
			case "sv":
			case "uy":
			case "ve":
				language = "es";
				break;
			case "be":
				language = "fr";
				break;
			case "at":
			case "ch":
			case "lu":
				language = "de";
				break;
			case "gcc":
			case "ae":
			case "oa":
			case "kw":
			case "qa":
			case "bh":
			case "sa":
				language = "ae";
				break;
		}

		return language;
	};

	window.intgrtnI18n.showDataTags = function () {
		$(".js-intgrtn-i18n-styles").remove();
	};

	window.intgrtnI18n.translateAll = function (language, offerName) {
		window.intgrtn.store('language', language);
		language = window.intgrtnI18n.getLanguage(language);
		window.intgrtnI18n.appendMedia(language);

		window.intgrtnI18n.getTranslations(language, function (response) {
			window.intgrtnI18n.translateDataTags(response, offerName);
			window.intgrtnI18n.showDataTags();
		}, function () {
			window.intgrtnI18n.showDataTags();
		});
	};

	window.intgrtnI18n.getTranslations = function (
		language, onSuccess, onError) {
		if (typeof onSuccess == "undefined") {
			onSuccess = function () {
			};
		}
		if (typeof onError == "undefined") {
			onError = function () {
			};
		}
		if (!window.availableLanguages) {
			console.error("Set window.availableLanguages");
		}
		if (window.availableLanguages && window.availableLanguages.includes(language + '.json')) {
			$.ajax({
				url: 'i18n/' + language + '.json',
				method: 'GET'
			}).done(function (response) {
				onSuccess(response);
			}).fail(function (response) {
				onError(response);
			});
		} else {
			$.ajax({
				url: 'i18n/default.json',
				method: 'GET'
			}).done(function (response) {
				onSuccess(response);
			}).fail(function (response) {
				onError(response);
			});
		}
	};

	window.intgrtnI18n.translateDataTags = function (response, offerName) {
		$("[data-i18n]").each(function () {
			var textToTranslate = $(this).attr("data-i18n");
			switch ($(this).prop("tagName")) {
				case "INPUT":
					if (!textToTranslate) {
						textToTranslate = $(this).attr("placeholder");
					}
					var translatedText = response[textToTranslate] ? response[textToTranslate] : textToTranslate;
					translatedText = translatedText.split('{title}').join(offerName);
					$(this).attr("placeholder", translatedText);
					break;
				default:
					if (!textToTranslate) {
						textToTranslate = $(this).text();
					}
					var translatedText = response[textToTranslate] ? response[textToTranslate] : textToTranslate;
					translatedText = translatedText.split('{title}').join(offerName);
					$(this).text(translatedText);
			}
		});
	}

	window.intgrtnI18n.appendMedia = function (language) {
		$('video').append($('<track kind="captions" src="subtitles/sub-' + language + '.vtt" default>'));
	};

	setTimeout(function () {
		window.intgrtnI18n.showDataTags();
	}, 3000);


	if ($(".js-intgrtn-i18n-styles").length == 0) {
		console.error("Missing .js-intgrtn-i18n-styles tag");
	}
}());
