﻿/************************************************\
' Qbrix software timer object                    '
'************************************************'
' All code is made and owned by Qbrix software.  '
' You are not allowed to change the code or to   '
' copy it to your own server.                    '
'                                                '
' All rights reserved by Qbrix software 2007     '
\************************************************/


// Declare a single namespace in the global scope
var dk;
if (!dk) dk = {};
else if (typeof(dk) != 'object')
	throw new Error('Der findes allerede en global variabel ved navn \u0027dk\u0027, som ikke er et object');
if (!dk.qbrix) dk.qbrix = { };
else if (typeof(dk.qbrix) != 'object')
	throw new Error('Der findes allerede en global variabel ved navn \u0027dk.qbrix\u0027, som ikke er et object');

dk.qbrix.Timer = function(total_time, num_steps, step_function, /*optional*/cleanup_function, /*optional*/auto_start)
{
    var _that = this;
    var _timer = 0;
    var _counter = 0;
    var _step = 0;
    var _initialized = false;
    
    this.initialize = function()
    {
        _initialized  = (typeof(total_time) == 'number');
        _initialized &= (typeof(num_steps)  == 'number');
        _initialized &= (typeof(step_function) == 'function');
        
        _initialized &= (total_time > 0);
        _initialized &= (num_steps  > 0);
        
        _initialized = Boolean(_initialized); // !!_initialized
        if (_initialized)
            _step = total_time / num_steps;

        return _initialized;
    }
    
    this.start = function()
    {
        if (!_initialized && !this.initialize())
            return false;
        
        // Don't start if already running
        if (_timer != 0)
            return true;

        // Reset timer and create (private) progress function            
        _counter = 0;
        var progress = function()
        {
            _counter++;
            step_function(_counter);
            
            if (_counter >= num_steps)
                _that.stop();
        }
        
        // Start timer
        _timer = setInterval(progress, _step);
        return (_timer != 0);
    }
    
    this.stop = function()
    {
        if (_timer != 0) clearInterval(_timer);
        _timer = 0;
        _counter = 0;
        
        if (typeof(cleanup_function) == 'function')
            cleanup_function();
        
        _initialized = false;
    }
    
    this.getCounter = function()
    {
        if (!_initialized) return 0;
        return _counter;
    }
    this.setCounter = function(new_value)
    {
        if (!_initialized) return;
        if (typeof(new_value) == 'number')
            _counter = Math.floor(new_value);
    }
    
    // Autostart timer function
    // I prefer to start it 'myself' after creation, but this is possible as well
    if (typeof(auto_start) == 'boolean' && auto_start)
        this.start();
}

dk.qbrix.Timer.prototype.getNumSteps = function()
{
    return num_steps;
}
