text: !function(t,e){"function"==typeof define&&define.amd?define(e):"object"==typeof exports?module.exports=e():t.TheaterJS=e()}(this,function(){function t(t){var r=this,n={autoplay:!0,erase:!0,locale:"detect",minSpeed:50,maxSpeed:600};r.options=r.utils.merge(n,t||{}),"detect"===r.options.locale&&(r.options.locale=(e.navigator&&e.navigator.languages||["en"])[0].split("-")[0]),r.keyboards[r.options.locale]||(r.options.locale="en"),r.utils.keyboard=r.keyboards[r.options.locale],r.events={},r.scene=-1,r.scenario=[],r.casting={},r.current={},r.state="ready"}var e=this;return t.prototype={constructor:t,version:"1.4.2",keyboards:{},set:function(t,e){var r=this,n=r.current.voice,i=r.current.html||[];return r.utils.isArray(e)||(e=[]),r.current.model=e[0]=r.utils.injectHTML(t,i),r.utils.isFunction(n)?n.apply(r,e):n.innerHTML=r.current.model,r},getSayingSpeed:function(t){var e=this;e.utils.isNumber(t)||(t=0);var r=e.current.speed+t;r>1&&(r=1);var n=e.utils.randomFloat(r,1);return e.utils.getPercentageBetween(e.options.maxSpeed,e.options.minSpeed,n)},recover:function(t){var e=this,r=e.maxMistakes();return t>=r||e.utils.randomFloat(0,1)<=e.current.accuracy},maxMistakes:function(){var t=this,e=.8-t.current.accuracy;return 0>=e?1:10*e},isMistaking:function(){var t=this;return t.utils.randomFloat(0,.8)>t.current.accuracy},utils:{keyboard:{},isArray:function(t){return t instanceof Array},isFunction:function(t){return"function"==typeof t},isString:function(t){return"string"==typeof t},isNumber:function(t){return"number"==typeof t},isObject:function(t){return!this.isArray(t)&&!this.isFunction(t)&&t instanceof Object},stripHTML:function(t){return t.replace(/(<([^>]+)>)/gi,"")},mapHTML:function(t){for(var e,r,n=/<[^>]+>/gi,i=[],a=[];e=n.exec(t);)r={tagName:e[0],position:e.index},"/"===r.tagName.charAt(1)?r.opener=a.pop():"/"!==r.tagName.charAt(r.tagName.length-2)&&a.push(r),i.push(r);return i},injectHTML:function(t,e){for(var r,n=0,i=e.length;i>n;n++)r=e[n],r.position<t.length?t=t.substr(0,r.position)+r.tagName+t.substr(r.position):r.opener&&r.opener.position<t.length&&(t+=r.tagName);return t},mapKeyboard:function(t){for(var e,r={},n=0,i=t.length;i>n;n++){e=t[n];for(var a=0,s=e.length;s>a;a++)r[e[a]]={x:a,y:n}}return r},merge:function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);return t},getPercentageBetween:function(t,e,r){return t-t*r+e*r},randomCharNear:function(t){var e=this,r=e.mapKeyboard(e.keyboard),n=1,i=[],a=!!t.match(/[A-Z]/);t=t.toLowerCase();var s,o,u=r[t]||[];for(s in r)r.hasOwnProperty(s)&&s!==t&&(o=r[s],Math.abs(u.x-o.x)<=n&&Math.abs(u.y-o.y)<=n&&i.push(s));var c=i.length>0?i[e.randomNumber(0,i.length-1)]:e.randomChar();return a?c.toUpperCase():c},randomChar:function(){var t=this,e=t.keyboard.join("");return e.charAt(t.randomNumber(0,e.length-1))},randomNumber:function(t,e){return Math.floor(Math.random()*(e-t+1))+t},randomFloat:function(t,e){return Math.round(10*(Math.random()*(e-t)+t))/10},hasClass:function(t,e){return t.classList?t.classList.contains(e):new RegExp("(^| )"+e+"( |$)","gi").test(t.className)},addClass:function(t,e){t.classList?t.classList.add(e):t.className+=" "+e},removeClass:function(t,e){t.classList?t.classList.remove(e):t.className=t.className.replace(new RegExp("(^|\\b)"+e.split(" ").join("|")+"(\\b|$)","gi")," ")}},train:function(t){var e=this,r={experience:.6,voice:function(t){console.log(t)},model:""};return t=e.utils.merge(r,t),e.utils.isNumber(t.speed)||(t.speed=t.experience),e.utils.isNumber(t.accuracy)||(t.accuracy=t.experience),e.utils.isNumber(t.invincibility)||(t.invincibility=10*t.accuracy),t},describe:function(t,e,r){var n=this,i={name:t};return n.utils.isNumber(e)?i.experience=e:n.utils.isObject(e)&&(i=n.utils.merge(i,e)),void 0!==r&&(i.voice=n.utils.isString(r)&&document&&document.querySelector?document.querySelector(r):r),n.casting[t]=n.train(i),n},write:function(t){for(var e,r=this,n=r.utils.isArray(t)?t:Array.prototype.splice.apply(arguments,[0]),i=0,a=n.length;a>i;i++)if(e=n[i],r.utils.isString(e)){var s=e.split(":"),o=s.length>1&&"\\"!==s[0].charAt(s[0].length-1),u=o?s.shift().trim():null,c=s.join(":").replace(/\\:/g,":");o&&r.write({name:"actor",args:[u]}),r.options.erase&&o&&r.write({name:"erase",args:[]}),r.write({name:"say",args:[c,!o]})}else r.utils.isNumber(e)?r.write(0>e?{name:"erase",args:[e]}:{name:"wait",args:[e]}):r.utils.isFunction(e)?r.write({name:"call",args:[e]}):r.utils.isObject(e)&&r.scenario.push(e);return r.options.autoplay&&r.play(),r},play:function(t){var e=this;return t===!0&&(e.scene=-1),"playing"!==e.state&&(e.state="ready",e.next()),e},stop:function(){var t=this;return t.state="stopped",t},on:function(t,e){var r=this;t=t.split(",");for(var n,i=0,a=t.length;a>i;i++)n=t[i]=t[i].trim(),(r.events[n]||(r.events[n]=[])).push(e);return r},emit:function(t,e,r){var n=this,i=t;return n.utils.isString(e)?i+=":"+e:(void 0==r&&(r=e),e=null),n.trigger(i,r).trigger("*",[i].concat(r)),n},trigger:function(t,e){var r=this,n=r.events[t]||[];e instanceof Array||(e=[e]);for(var i=0,a=n.length;a>i;i++)n[i].apply(r,[t].concat(e));return r},call:function(t,e){var r=this;return t.apply(r),e?r:r.next()},next:function(){var t=this,e=t.scenario[t.scene];if(e&&t.emit(e.name,"end",[e.name].concat(e.args)),"stopped"===t.state||t.scene+1>=t.scenario.length)t.state="ready";else{t.state="playing";var r=t.scenario[++t.scene];t.emit(r.name,"start",[r.name].concat(r.args)),t[r.name].apply(t,r.args)}return t},actor:function(t){var e=this;return e.current=e.casting[t],e.next()},say:function(t,e){var r,n,i=this,a=0,s=!1,o=i.current.invincibility;i.current.html=i.utils.mapHTML(t),t=i.utils.stripHTML(t),e?(n=i.current.model,r=i.current.model.length-1,t=n+t):(n=i.current.model="",r=-1);var u=setTimeout(function c(){var e,l,p=n.charAt(r);a>0&&(r>=t.length||s||i.recover(a))?(o=i.current.invincibility,s=!0,e=null,l=n=n.substr(0,r),a--,r--):(s=!1,r++,e=t.charAt(r),r<t.length&&--o<0&&(p!==e||i.current.accuracy<=.3)&&i.isMistaking()&&(e=i.utils.randomCharNear(e)),(e!==t.charAt(r)||a>0)&&a++,l=n+=e),i.set(l,[l,e,p,t]),a>0||r<t.length?u=setTimeout(c,i.getSayingSpeed()):i.next()},i.getSayingSpeed());return i},erase:function(t,e){var r=this;if(!r.utils.isString(r.current.model))return r.next();{var n=r.utils.stripHTML(r.current.model),i=n.length,a=0>t?i+1+t:0;setTimeout(function s(){var t=n.charAt(i),e=n.substr(0,--i);r.set(e,[e,null,t,e]),n=e,i>=a?setTimeout(s,r.getSayingSpeed(.2,!0)):r.next()},e||r.getSayingSpeed(.2))}return r},wait:function(t){var e=this;return setTimeout(function(){e.next()},t),e}},t.prototype.keyboards.en=["qwertyuiop","asdfghjkl","zxcvbnm"],t.prototype.keyboards.fr=["azertyuiop","qsdfghjklm","wxcvbn"],t});