function Limit(from, to){ this.from = from; this.to = to; this.value = this.from; this.onmore = new DOMEvent(); this.onless = new DOMEvent(); this.onchange = new DOMEvent(); this.TO_REACHED = false; this.FROM_REACHED = true; this.MORE_VALUE = 0; this.LESS_VALUE = 0; } Limit.prototype.change = function(val){ if(val == 0) return; var value = val; this.TO_REACHED = false; this.FROM_REACHED = false; value = this.checkFrom(value); value = this.checkTo(value); this.value += value; this.onchange.fire(); if(this.TO_REACHED){ this.onmore.fire(); } if(this.FROM_REACHED){ this.onless.fire(); } } Limit.prototype.setValue = function(val){ var value = val - this.from; this.change(value); } Limit.prototype.checkTo = function(val){ var value = val; if(this.value + value >= this.to){ value = this.to - this.value; this.MORE_VALUE = value; this.TO_REACHED = true; } return value; } Limit.prototype.checkFrom = function(val){ var value = val; if(this.value + value <= this.from){ //toConsole(this.value); value = -this.value + this.from; this.LESS_VALUE = value; this.FROM_REACHED = true; } return value; }