/*
* 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
* @param  bool        mode			   in seconds or formatted
* @param  string      list         format
*
* @return object
* @access public
*/ 
function timer(exec,target,time,tempo,noAutoStart,format)
{
 	// 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();
		
	if (format) {
		this.f = format.split(';',4);
	}
}

/************************** 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 (this.f) {
		var d = Math.floor(this.current/86400);
		var h = Math.floor((this.current%86400)/3600);
		var m = Math.floor((this.current%3600)/60);
		var s = Math.floor(this.current%60);
		var text = (d>0 ? d + this.f[0] : "") +  (h>0 ? h + this.f[1] : "") +(m>0 ? m + this.f[2] : "") + s + this.f[3];
		if(_targetObj.type && _targetObj.type == "text")
	    _targetObj.value = text;
            else 
               _targetObj.innerHTML = text;
	}
	else {
if(_targetObj.type && _targetObj.type == "text")
	    _targetObj.value = this.current;
            else 
               _targetObj.innerHTML = this.current;
}
}
