function Limit(from, to){ this.from = from; this.to = to; this.value = this.from; this.onmore = new DOMEvent(this); this.onless = new DOMEvent(this); this.onmoreout = new DOMEvent(this); this.onlessout = new DOMEvent(this); this.onchange = new DOMEvent(this); this.TO_REACHED = false; this.FROM_REACHED = true; this.TO_OUT = false; this.FROM_OUT = false; this.MORE_VALUE = 0; this.LESS_VALUE = 0; this.exceeded = 0; this.preceeded = 0; this.MORE_OUT_VALUE = 0; this.LESS_OUT_VALUE = 0; this._attemped_value; } Limit.prototype.change = function(val){ this._attempted_value = val; var value = val; 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(); } if(this.TO_OUT){ this.onmoreout.fire(); } if(this.FROM_OUT){ this.onlessout.fire(); } } Limit.prototype.setValue = function(val){ var value = val - this.from; this.change(value); } Limit.prototype.checkTo = function(val){ var value = val; this.TO_OUT = false; if(this.TO_REACHED){ if(this.value + value < this.to){ this.TO_REACHED = false; this.TO_OUT = true; this.TO_OUT_VALUE = value; }else{ this.exceeded = value; value = 0; this.MORE_VALUE = value; } }else{ if(this.value + value >= this.to){ var v = this.to - this.value; this.exceeded = value - v; value = v; this.MORE_VALUE = value; this.TO_REACHED = true; } } return value; } Limit.prototype.checkFrom = function(val){ var value = val; this.FROM_OUT = false; if(this.FROM_REACHED){ if(this.value + value > this.from){ this.FROM_REACHED = false; this.FROM_OUT = true; this.FROM_OUT_VALUE = value; }else{ this.preceeded = value; value = 0; this.LESS_VALUE = value; } }else{ if(this.value + value <= this.from){ var v = this.value + value; value = -this.value + this.from; this.LESS_VALUE = value; this.preceeded = v; this.FROM_REACHED = true; } } return value; } Limit.prototype.__name = "Limit"; Limit.prototype.toString = __toString;