var Translator=function(){_this=this;String.prototype.format||(String.prototype.format=function(){a=this;for(k in arguments)a=a.replace("{"+k+"}",arguments[k]);return a});$(document).ready(_this.initialize);$(document).ajaxComplete(_this.translateAsyncResponse)};
Translator.prototype={strFormat:function(b,c){a=b;for(k in c)a=a.replace("{"+k+"}",c[k]);return a},allStaticFilesAreLoaded:!1,lastFileNumberOfElements:0,allStaticElementsTranslated:!1,data:[],filesLoaded:[],scriptLoadedFiles:[],errorConst:"error",blankString:"",translationStatusDataAttribute:"data-translated",sentenceCodeDataAttribute:"data-sentencecode",sentenceTypeDataAttribute:"data-sentencetype",sentenceValuesDataAttribute:"data-sentencevalues",setenceTranslationPathDataAttribute:"data-translationpath",
mimeTypes:{applicationXML:"application/xml",textHTML:"text/html"},sentenceTypes:{toolTip:"tooltip",helpText:"helptext"},translatorPrompt:"TRANSLATOR:> ",translationStatusAttribute:function(){return _this.translationStatusDataAttribute.replace("data-","")},sentenceCodeAttribute:function(){return _this.sentenceCodeDataAttribute.replace("data-","")},sentenceValuesAttribute:function(){return _this.sentenceValuesDataAttribute.replace("data-","")},sentenceTypeAttribute:function(){return _this.sentenceTypeDataAttribute.replace("data-",
"")},setenceTranslationPathAttribute:function(){return _this.setenceTranslationPathDataAttribute.replace("data-","")},jsongetter:function(b){if(-1==_this.filesLoaded.indexOf(b)){var c={cache:!0};c.crossDomain=-1==b.indexOf(window.location.hostname);c.url=b;c.contentType="text/plain";c.dataType="json";c.success=function(c){_this.filesLoaded.push(b);_this.loadTranslationFile(c)};$.ajax(c)}},initialize:function(){_this.updateTranslationStatuses();if("undefined"!=typeof TranslationFiles){for(var b=0;b<
_this.scriptLoadedFiles.length;b++)-1==TranslationFiles.indexOf(_this.scriptLoadedFiles[b])&&TranslationFiles.push(_this.scriptLoadedFiles[b]);for(b=0;b<TranslationFiles.length;b++)_this.jsongetter(TranslationFiles[b])}else"undefined"!=typeof AllTranslationSentences&&_this.loadTranslationFile(AllTranslationSentences)},loadTranslationFile:function(b){for(var c=0;c<b.length;c++)_this.data.push(b[c]);_this.allStaticFilesAreLoaded="undefined"==typeof TranslationFiles||_this.filesLoaded.length>=TranslationFiles.length;
b=$("*["+_this.sentenceCodeDataAttribute+"]["+_this.translationStatusDataAttribute+"=false]");_this.allStaticFilesAreLoaded&&(_this.lastFileNumberOfElements=b.length);b.each(_this.translateThisElement);if(0==b.length||_this.allStaticFilesAreLoaded)_this.allStaticElementsTranslated=!0},translateThisElement:function(){"uz-Latn"==$("#languageSelector").val()?_this.showSentenceCode($(this)):_this.translateElement($(this))},isTranslated:function(b){return Boolean(b.data(_this.translationStatusAttribute()))},
loadFileAndTranslateElement:function(b,c){-1==_this.scriptLoadedFiles.indexOf(c)&&_this.scriptLoadedFiles.push(c)},translateElement:function(b){if("undefined"!=typeof b[0]&&!1==_this.isTranslated(b)){var c=!1,h=b[0].tagName,e=b.data(_this.sentenceCodeAttribute()),f=b.data(_this.sentenceValuesAttribute()),g=b.data(_this.sentenceTypeAttribute()),d=_this.findTranslation(e),l;"undefined"!=typeof d&&null!=d&&g!=_this.sentenceTypes.helpText&&(l=d.Label);if("undefined"!=typeof l&&null!=l||"undefined"!=typeof d&&
null!=d&&g==_this.sentenceTypes.helpText||"undefined"!=typeof f&&"undefined"==typeof e){"undefined"!=typeof g&&null!=g&&(l=g==_this.sentenceTypes.toolTip&&null!=d.ToolTip?d.ToolTip:g==_this.sentenceTypes.toolTip&&null==d.ToolTip&&null!=d.HelpText?d.HelpText:g==_this.sentenceTypes.helpText&&null!=d.HelpText?d.HelpText:g==_this.sentenceTypes.toolTip&&null==d.HelpText&&null!=d.ToolTip?d.ToolTip:"",l==_this.blankString&&g==_this.sentenceTypes.helpText&&!1==b.is("input")&&b.remove());if("undefined"!=typeof f&&
null!=f&&0<f.length){e=[];if("string"==typeof f[0])l=_this.strFormat(l,f);else if("object"==typeof f[0])for(g=0;g<f.length;g++)"undefined"!=typeof f[g].sentencecode&&""!=f[g].sentencecode?(d=_this.findTranslation(f[g].sentencecode),"undefined"==typeof d&&(c=!0),"undefined"!=typeof d&&"undefined"!=typeof d.Label?e.push(d.Label):null!=f[g].value?e.push(f[g].value):e.push("")):e.push(f[g].value);"undefined"!=typeof l?l=_this.strFormat(l,e):b.html(_this.strFormat(b.html(),e))}switch(h.toLowerCase()){case "input":"radio"!=
b.attr("type")?"text"!=b.attr("type")&&"password"!=b.attr("type")||"undefined"==typeof b.attr("placeholder")||null==b.attr("placeholder")||""==b.attr("placeholder")?"text"!=b.attr("type")&&"password"!=b.attr("type")&&b.val(l):b.attr("placeholder",l):$("label[for='"+b.attr("id")+"']").each(function(){$(this).html(l)});break;case "span":"undefined"==typeof f&&b.empty();default:0==b.children().length?b.html(l):_this.translateElementWithChildren(b,l)}!1==c?b.attr(_this.translationStatusDataAttribute,
!0):b.attr(_this.translationStatusDataAttribute,!1)}else b.attr(_this.translationStatusDataAttribute,!1);"undefined"!=typeof b.attr("placeholder")&&""!=b.attr("placeholder")&&!1==!l&&b.attr("placeholder",l);"undefined"!=typeof d&&"undefined"!=typeof d.ToolTip&&""!=d.ToolTip&&null!=d.ToolTip&&("undefined"!=typeof b.attr("title")&&b.attr("title",d.ToolTip),"undefined"!=typeof b.attr("alt")&&b.attr("alt",d.ToolTip),"undefined"!=typeof b[0]&&"DIV"==b[0].tagName&&"undefined"!=typeof b[0].parentNode&&"undefined"!=
typeof b[0].parentNode.parentNode&&"LI"==b[0].parentNode.parentNode.tagName&&"undefined"!=typeof b[0].parentNode.parentNode.getAttribute("title")&&b[0].parentNode.parentNode.setAttribute("title",d.ToolTip))}},checkFileForSentenceCode:function(b){for(var c=b.split("."),h=0;h<_this.filesLoaded.length;h++)if(-1!=_this.filesLoaded[h].indexOf(c[0])&&-1!=_this.filesLoaded[h].indexOf(c[1])&&-1!=_this.filesLoaded[h].indexOf(c[2]))return console.info("SentenceCode File: "+_this.filesLoaded[h]),console.info("Defined Translation: ",
_this.findTranslation(b)),!0;return!1},translateElementWithChildren:function(b,c){if(0<b.children().length&&b.html()!=c&&_this.stringIsHTML(b.html())){var h=b.clone(),e=b.children();h.children().each(function(){$(this).remove()});"undefined"!=typeof h.text()&&""!=h.text()?(b.children().each(function(){$(this).remove()}),b.html(c),"undefined"!=typeof b.context.title&&0<b.context.title.trim().length&&(b.context.title=c),b.append(e)):"fieldset"==b[0].tagName.toLowerCase()&&0<b.children("legend").length&&
b.children("legend:eq(0)").html(c)}},findTranslation:function(b){for(var c=0;c<_this.data.length;c++)if(_this.data[c].SentenceCode==b)return _this.data[c]},translateAsyncResponse:function(b,c,h){-1==h.dataTypes.indexOf("json")&&_this.updateTranslationStatuses();$("*["+_this.sentenceCodeDataAttribute+"]["+_this.translationStatusDataAttribute+"=false],*["+_this.sentenceValuesDataAttribute+"]["+_this.translationStatusDataAttribute+"=false]").each(_this.translateThisElement)},addTranslationFile:function(b){_this.jsongetter(b)},
updateTranslationStatuses:function(){$("*["+_this.sentenceCodeDataAttribute+"],*["+_this.sentenceValuesDataAttribute+"]").each(function(){"undefined"==typeof $(this).data(_this.translationStatusAttribute())&&$(this).attr(_this.translationStatusDataAttribute,!1)})},translationInfo:function(){var b="*[{0}]".format(_this.sentenceCodeDataAttribute);console.clear();$(b).each(function(){var b=$(this).attr("id"),h=$(this).data(_this.sentenceCodeAttribute()),e=_this.findTranslation(h),f=$(this).attr(_this.translationStatusDataAttribute),
g=$(this)[0].tagName,d="",d="undefined"!=typeof e?"\nID: {0}\nSENTENCECODE: {1}\nHASTRANSLATION: true\nLABEL: {2}\nTOOLTIP: {3}\nHELPTEXT: {4}\nTRANSLATED: {5}\nTYPE: {6}\n".format(b,h,e.Label,e.ToolTip,e.HelpText,f,g):"\nID: {0}\nSENTENCECODE: {1}\nHASTRANSLATION: false\nTRANSLATED: {2}\nTYPE: {3}\n".format(b,h,f,g);console.info(d)})},problemsFound:function(){var b="*[{0}][{1}=false]".format(_this.sentenceCodeDataAttribute,_this.translationStatusDataAttribute);$(b).each(function(){var b=$(this).attr("id"),
h=$(this).data(_this.sentenceCodeAttribute()),e=_this.findTranslation(h),f=$(this).attr(_this.translationStatusDataAttribute),g=$(this)[0].tagName;"undefined"!=typeof e?"\nID: {0}\nSENTENCECODE: {1}\nHASTRANSLATION: true\nLABEL: {2}\nTOOLTIP: {3}\nHELPTEXT: {4}\nTRANSLATED: {5}\nTYPE: {6}\n".format(b,h,e.Label,e.ToolTip,e.HelpText,f,g):"\nID: {0}\nSENTENCECODE: {1}\nHASTRANSLATION: false\nTRANSLATED: {2}\nTYPE: {3}\n".format(b,h,f,g)})},stringIsHTML:function(b){try{var c=!1;try{return(new DOMParser).parseFromString(b,
_this.mimeTypes.textHTML).documentElement.getElementsByTagName("body")[0].innerHTML==b}catch(h){c=!0,console.info("DOM Parser() not accessible in this client;")}if(c){var e=document.createElement("div");e.innerHTML=b;for(var f=e.childNodes,g=f.length;g--;)if(1==f[g].nodeType)return!0}return!1}catch(d){return console.info(d),!1}},showSentenceCode:function(b){var c=b[0].tagName;if(!1==/\[.*\(.*\|.*\).*\]/.test(b.text())&&!1==b.data().translated){switch(c.toLowerCase()){case "input":"radio"!=b.attr("type")?
"text"!=b.attr("type")&&"password"!=b.attr("type")||"undefined"==typeof b.attr("placeholder")||null==b.attr("placeholder")||""==b.attr("placeholder")?"text"!=b.attr("type")&&"password"!=b.attr("type")&&b.val("["+b.html()+" ("+b.attr(_this.sentenceCodeDataAttribute)+")]"):b.attr("placeholder","["+b.html()+" ("+b.attr(_this.sentenceCodeDataAttribute)+")]"):$("label[for='"+b.attr("id")+"']").each(function(){$(this).html("["+b.html()+" ("+b.attr(_this.sentenceCodeDataAttribute)+")]")});break;default:b.html("["+
b.html()+" ("+b.attr(_this.sentenceCodeDataAttribute)+")]")}b.attr(_this.translationStatusDataAttribute,"true")}}};function CheckParentTitle(b,c,h,e,f){e!=f&&"undefined"!=typeof b.parentElement&&null!=b.parentElement&&("undefined"!=typeof b.parentElement.title&&0<b.parentElement.title.trim().length&&b.parentElement.title.toLowerCase()==c.toLowerCase()?b.parentElement.title=h:CheckParentTitle(b.parentElement,c,h,e+1,f))}var translator=new Translator;
