!function(t){function e(e,n){this.base=t(e),this.details=null,this.opts=n||{},this.events=[]}t.fn.calculate=function(n,r){var u=new e(this,r=t.extend({},t.calculate.defaults(),r||{}));return"string"==typeof n?u.formula(n):n.bind(this).call(n,u),this},t.calculate={inputParser:function(t){return t},outputFormatter:function(t){return t},options:function(t){t.inputParser&&(this.inputParser=t.inputParser),t.outputFormatter&&(this.outputFormatter=t.outputFormatter)},defaults:function(){return{inputParser:this.inputParser,outputFormatter:this.outputFormatter}}},e.prototype={formula:function(t){n.compile(this,t),n.updateEvents(this),this.run()},run:function(){var e=this;this.base.each(function(){e.runOne(t(this))})},runOne:function(e){var n=function(e,n,r){var u={},a=t(n);return t.each((i=e,s=[],Object.keys(i).forEach(function(t){var e=i[t];s.push([t,e])}),s),function(){var e=this[0],n=this[1],i=a.find(e);u[n]=i.toArray().reduce(function(e,n){var u=t(n).val();return e+1*r(u)},0)}),u;var i,s}(this.details.operands,e,this.opts.inputParser),r=this.compiled.eval(n),u=this.opts.outputFormatter(r);e.find(this.details.resultSelector).val(u)}};var n={compile:function(t,e){t.details=function(t){var e={operands:{}},n=t.match(/^\s*([^=\s]+)\s*=\s*(.*)$/);return e.rside=function(t,e){var n=new RegExp(r,"g");return t.replace(n,function(t,n){var r=e[n];return r||(e[n]="X"+u++)})}(n[2],e.operands),e.resultSelector=function(t){return new RegExp(r).exec(t)[1]}(n[1]),e}(e);var n=math.parse(t.details.rside);t.compiled=n.compile(math)},updateEvents:function(t){n.removeEvents(t),n.setEvents(t)},removeEvents:function(t){t.events.forEach(function(t){t.query.off(t.name,t.handler)}),t.events=[]},setEvents:function(e){Object.keys(e.details.operands).forEach(function(r){e.base.each(function(){var u=t(this),a=u.find(r);n.setEvent(e,a,"change",function(){e.runOne(u)})})})},setEvent:function(t,e,n,r){var u={query:e,name:n,handler:r};u.query.on(u.name,u.handler),t.events.push(u)}};var r="{{([^}]+)}}";var u=0}(jQuery);