/*
* timers objects reference object for global compatibilitie with the setInterval()
*
* @var _timersObjRef
* @access private
*/ 
var _timersObjRef = {
                    count: 0,
                    obj: new Array
                };

/*
* timer class constructor
*
* @param mixed     exec                   javascript evaluable code or function reference or url (for redirect) executed at the end of the timer
* @param  str          target                 html contener id
* @param  int          time                    start value (in seconds)
* @param  int          tempo                 decrement offset (in seconds)
* @param  bool        noAutoStart      disable auto start timer
*
* @return object
* @access public
*/ 
/*
* @param  mixed     exec             code js à évaluer, référence vers une fonction/méthode ou url pour redirection
* @param  str       target           id du conteneur html
* @param  int       time             temps de départ en secondes (par défaut 5)
* @param  int       tempo            valeur de décrémentation
* @param  bool      noAutoStart      si true le timer ne démarrera pas automatiquement

var monTimer = new timer(exec,target,time,tempo,noAutoStart);

en outre vous disposez de quelques méthodes pour gérer le timer 4 pour le gérer et 2 setter...

- start() pour le lancer s'il n'est pas en mode auto
- stop() pour l'arrêter
- rewind() pour le remettre à sa valeur de début
- restart() on repart depuis le début

- setTime() pour modifier le temps total du timer
- setTempo() pour modifier sont rythme de décrémentation
*/
function timer(exec,target,time,tempo,noAutoStart)
{
    // object attributs initialize
    this._id = _timersObjRef.count++;
    this.tempo = (tempo) ? tempo : 1;
    this.interval = this.tempo*1000;
    this.time = (time) ? time : 5;
    this.current = this.time;
    this.target = (target) ? target : null;
    this._handle = null;
    this._exec = null;
    
    if(exec)
    {
        // we search if the exec var is an url
        var regex = new RegExp("^((ht|f)tps?:\/\/)?(www\.)?([A-Za-z0-9-_:]+\\.[A-Za-z]{2,4})|([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+(:[0-9]+)?)", "gi");
        
        //if it is the exec code is a redirection
        if (regex.exec(exec) != null)
            this._exec = 'location.href = "'+exec+'";';
                else 
                    this._exec = exec;
    }
    
    //global object ref for setInterval execution
    _timersObjRef.obj[this._id] = this;
    //set the timer html contener value
    this._setContents();
    
    if(!noAutoStart)
        this.start();
}

/************************** Publics methods **************************/

/*
* timer start method
*
* @return void
* @access public
*/ 
timer.prototype.start = function()
{
    //register the interval... we use the global referenced timer for updating action
    if(!this._handle)
        this._handle = setInterval("_timersObjRef.obj["+this._id+"]._update();",this.interval);
}

/*
* timer stop method
*
* @return void
* @access public
*/ 
timer.prototype.stop = function()
{
    if(this._handle)
    {
        clearInterval(this._handle);
        this._handle = null;
    }
}

/*
* timer current value reset
*
* @return void
* @access public
*/ 
timer.prototype.rewind = function()
{
    this.current = this.time;
}

/*
* timer count restart
*
* @return void
* @access public
*/ 
timer.prototype.restart = function()
{
    this.stop();
    this.rewind();
    this.start();
}

/*
* timer time setter
*
* @param  int          time                    start value (in seconds)
*
* @return void
* @access public
*/ 
timer.prototype.setTime = function(time)
{
    this.time = (time) ? time : 5;
}

/*
* timer tempo setter
*
* @param  int          tempo                 decrement offset (in seconds)
*
* @return void
* @access public
*/ 
timer.prototype.setTempo = function(tempo)
{
    this.tempo = (tempo) ? tempo : 1;
    this.interval = this.tempo*1000;
}


/************************** Privates methods **************************/

/*
* timer updating
*
* @return void
* @access private
*/ 
timer.prototype._update = function()
{    
    this.current -= this.tempo;
    
    if(this.target)
        this._setContents();
    
    if(this.current == 0)
    {
        this.stop();
        
        if(this._exec)
        {
            if(typeof this._exec == "function")
                this._exec();
                    else
                        eval(this._exec);
        }
    }
}

/*
* timer target contener updater
*
* @return void
* @access private
*/ 
timer.prototype._setContents = function()
{
    var _targetObj;

    if(document.getElementById)
        _targetObj = (typeof document.getElementById(this.target) == "object") ? document.getElementById(this.target) : void(0);
            else if(document.all) 
                _targetObj = (typeof document.all[this.target] == "object") ? document.all[this.target] : void(0);
                    else if(document.layers) 
                        _targetObj = (typeof document[this.target] == "object") ? document[this.target] : void(0);
    
    if(!_targetObj)
        return false;
    
    // html input ?
    if(_targetObj.type && _targetObj.type == "text")
        _targetObj.value = this.current;
            else 
                _targetObj.innerHTML = this.current;
}

