import com.lalex.core.CoreObject;

/**
 * @author LAlex
 * @class com.lalex.utils.Proxy
 */


class com.lalex.utils.Proxy
        extends CoreObject {
               
        public static function createFunction(o, f:Function):Function {
                return create.apply(null, arguments).getProxyFunction();
        }
               
        public static function create(o, f:Function):Proxy {
                var px:Proxy = new Proxy(o, f);
                var args:Array = arguments;
                args.splice(0,2);
                px.setArguments(args);
                return px;
        }
       
        private var _scope;
        private var _fct:Function;
       
        private var _proxy:Function;

        private var _args : Array;
       
        function Proxy(o, f:Function) {
                _scope = o;
                _fct = f;
                setArguments(arguments.slice(2));
                createProxyFunction();
        }
       
        private function createProxyFunction() {
                _proxy = function() {
                        var ff = arguments.callee.f;
                        var fs = arguments.callee.s;
                        var fa = arguments.callee.a;
                        ff.apply(fs, arguments.concat(fa));
                };
                _proxy.f = _fct;
                _proxy.s = _scope;
                _proxy.a = _args;
        }
       
        public function getProxyFunction():Function {
                return _proxy;
        }
       
        public function getFunction():Function {
                return _fct;
        }
       
        public function setArguments(a : Array) {
                _args = a.concat();
                if (_proxy) {
                        _proxy.a = _args;
                }
        }
       
        public function getScope():Function {
                return _scope;
        }
       
        public function equals(p:Proxy):Boolean {
                if (p.getScope() == _scope && p.getFunction() == _fct) {
                        return true;
                }
                return false;
        }
       
        public function launch() {
                _proxy.apply(null, arguments);
        }
       
}
import com.lalex.utils.Proxy;

/**
 * @author LAlex
 * @class com.lalex.utils.Interval
 */

class com.lalex.utils.Interval {
       
        private var _itv:Number = -1;
        private var _px:Proxy;
       
        private var _tim:Number;
       
        private var _nb:Number = 0;
        private var _max:Number = Number.POSITIVE_INFINITY;
       
        function Interval(tgt:Object, fct:Function, tim:Number, max:Number) {
                _px = new Proxy(tgt, fct);
                setTime(tim);
                setMax(max);
        }
       
        function setMax( max : Number ) {
                _max = max ? max : Number.POSITIVE_INFINITY;
        }
       
        function start( launchNow : Boolean ) : Void {
               
                stop();
               
                resume(launchNow);

        }
       
        function resume( launchNow : Boolean ) {
                if (!isRunning()) {
                        if (launchNow) {
                                launchProxy();
                        }
                        _itv = setInterval(this, "launchProxy", _tim);
                }
        }
       
        private function launchProxy() {
                if (_nb++ < _max) {
                        _px.launch();
                } else {
                        stop();
                }
        }
       
        public function isRunning() : Boolean {
                return _itv != -1;
        }
       
        function setTime( time : Number ) : Void {
                pause();
                _tim = time;
        }
       
        public function pause() {
                if (isRunning()) {
                        clearInterval(_itv);
                        _itv = -1;
                }
        }
       
        public function stop() {
                pause();
                _nb = 0;
        }
       
}