/*!
 i18n-text - v0.4.3 - 2014-08-14
 https://vogdb@bitbucket.org/vogdb/i18n-text.git
 Copyright (c) 2014 Sanin Aleksey aka vogdb; Licensed WTFPL
*/
!function(a){function b(a){return"[object Object]"===Object.prototype.toString.call(a)}function c(a,b){return a.replace(/{{([\w]+)}}/g,function(a,c){var d=b[c];return d?d:a})}function d(a,b){throw new Error(c(a,b))}function e(a){return-1!==a.indexOf("file://")}function f(a){return e(a)||e(window.location.href)&&a.indexOf(!1)}var g={parseAndEval:function(a,b){return a.replace(/{{([^{}]+#[^}]*(?:|.*#.*)+)}}/g,function(a,c){return g.eval(c,b)})},eval:function(a,b){for(var c=a.split("|"),e=c.length-1;e>=0;e--){var f=c[e].split("#"),i=f[0];if(g.isValidFormula(i)||d(h.error.INVALID_PLURAL,{plural:i}),g.evalFormula(i,b))return f[1]}return a},isValidFormula:function(a){return/[ \dn<>=*-+!?]+/g.test(a)},evalFormula:function(a,b){return new Function("n","return "+a)(b)}},h=function(a){this._loadedLocales={},this._currentLocale=null,this._msgPath=null,this._subscribers={},this._init(a)};h.prototype.getLocale=function(){return this._currentLocale},h.prototype.setLocale=function(a){this.hasLocale(a)?this._setLocale(a):(this.once(h.event.LOCALE_LOAD,function(b){b.error||b.locale!==a||this._setLocale(b.locale)}.bind(this)),this.loadLocale(a))},h.prototype._setLocale=function(a){this._currentLocale=a,this._fire(h.event.LOCALE_CHANGE,{locale:a})},h.prototype.hasLocale=function(a){return!!this._loadedLocales[a]},h.prototype.loadLocale=function(a,b){b=b||this._msgPath;var d=b+"/"+a+".json";h.loadFile({url:d,success:function(b){b.length>0?(this._loadedLocales[a]={},this._mergeKeys(JSON.parse(b),this._loadedLocales[a],""),this._fire(h.event.LOCALE_LOAD,{locale:a})):this._fire(h.event.LOCALE_LOAD,{error:c(h.error.EMPTY_MESSAGES,{file:d})})}.bind(this),error:function(a){this._fire(h.event.LOCALE_LOAD,{error:c(h.error.NO_MESSAGES,{file:d,error:a})})}.bind(this)})},h.prototype._mergeKeys=function(a,c,d){for(var e in a)if(b(a[e])){var f;f=d?d+h.KEY_SEPARATOR+e:e,this._mergeKeys(a[e],c,f)}else{var g;g=d?d+h.KEY_SEPARATOR+e:e,c[g]=a[e]}},h.prototype.text=function(a,e,f){arguments[1]&&!b(arguments[1])&&(f=arguments[1],e=void 0);var i=f||this.getLocale();i||d(h.error.NO_LOCALE_IS_SET),this.hasLocale(i)||this.loadLocale(i);var j=this._loadedLocales[i][a];return void 0===j&&d(h.error.INVALID_KEY,{key:a,locale:i}),e?(void 0!==e.n&&(j=g.parseAndEval(j,e.n)),c(j,e)):j},h.prototype.on=function(a,b){this._subscribers[a]||(this._subscribers[a]=[]),this._subscribers[a].push(b)},h.prototype._fire=function(a,b){if(this._subscribers[a])for(var c=this._subscribers[a],d=c.length-1;d>=0;d--)c[d].call(null,b)},h.prototype.off=function(a,b){if(this._subscribers[a]){if(b){var c=this._subscribers[a].indexOf(b);this._subscribers[a].splice(c,1)}b&&0!==this._subscribers.length||delete this._subscribers[a]}},h.prototype.once=function(a,b){this.on(a,function(c){this.off(a,b),b(c)}.bind(this))},h.prototype._init=function(a){a||d(h.error.NO_OPTS),a.path?this._msgPath=a.path:d(h.error.NO_PATH)},h.loadFile=function(a){var b=new XMLHttpRequest;b.open("GET",a.url),b.onreadystatechange=function(){4===b.readyState&&(200==b.status||f(a.url)&&b.responseText.length>0?a.success(b.responseText):a.error(b.responseText))},b.send(null)},h.error={NO_OPTS:"Options are not present",NO_PATH:"Options path is not present",NO_MESSAGES:"{{file}} is unreachable. Error {{error}}",EMPTY_MESSAGES:"{{file}} is empty",INVALID_KEY:"{{key}} key is not present in locale {{locale}}",NO_LOCALE_IS_SET:"Locale is not set.",INVALID_PLURAL:"Invalid plural form: {{plural}}"},h.event={LOCALE_LOAD:"localeload",LOCALE_CHANGE:"localechange"},h.KEY_SEPARATOR=".","function"==typeof define&&define.amd?define(function(){return h}):a.I18nText=h}("undefined"==typeof window?this:window);