
function Timer(callback, timeout, autostart)
{
    this.timeout = timeout;
    this.callback = callback;
    this._loop = (autostart != undefined && autostart != false);
    this._id = null;
    this._interation = 0;

    this.stop = function(reason)
    {
        if( reason == undefined ){
            reason = 'undefined';
        }
        debug('TIMER STOP: ' + reason);
        this._loop = false;
        if( this._id != null)
        {
            clearInterval(this._id);
            this._id = null;
        }
    }

    this.start = function()
    {
        if( this._loop == false)
        {
            debug('TIMER START');
            this._loop = true;
            this._interation = 0;
            var self = this;
            this._id = setInterval(function(){
                self.run(self)
            }, this.timeout);

        }

    }

    this.run = function(self)
    {
        this._interation++;
        self.callback();
    }

    this.changeTimeout = function(timeout, reload)
    {
        this.timeout = timeout;
        if( reload == undefined || reload == true)
        {
            this.stop();
            this.start();
        }
    }

    if( autostart == true )
    {
        this.start();
    }
}


function Timeout(callback, timeout, autostart)
{
    this.timeout = timeout;
    this.callback = callback;
    this._loop = (autostart != undefined && autostart != false);
    this._id = null;

    this.stop = function()
    {
        this._loop = false;
        if( this._id != null )
        {
            clearTimeout(this._id);
            this._id = null;
        }
    }

    this.loop = function()
    {
        if( this._loop == true)
        {
            var self = this;
            this._id = setTimeout(function(){
                self.run(self)
            }, this.timeout);
        }
    }

    this.start = function()
    {
        if( this._loop == false)
        {
            this._loop = true;
            this.loop();
        }

    }

    this.run = function(self)
    {
        self.callback();
        this._id = null;
        this._loop = false;
        //self.loop();
    }

    this.changeTimeout = function(timeout, reload)
    {
        this.timeout = timeout;
        if( reload == undefined || reload == true)
        {
            this.stop();
            this.start();
        }
    }

    if( autostart == true )
    {
        this.start();
    }
}

