//  WaitToRun JavaScript util, version 1.0.0 29-set-2006
//  (c) 2006 Emil Knorst <emilknorst@hotmail.com>

var WaitToRun = {
  oMethod: function(){},
  iMil: 1000,
  bExec: false,
  bLog: false,
  iCount: 0,
  setMethod: function(pMethod){
    this.oMethod = pMethod;
  },
  setMilliseconds: function(pMil){
    this.iMil = pMil;
  },
  setLog: function(pLog){
    this.bLog = pLog;
  },
  getCounter: function(){
    return this.iCount;
  },
  run: function(){
    if (isNaN(this.iMil)){
      WaitToRun.log('WaitToRun - Invalid Milliseconds! Use setMilliseconds to set.', true);
      return;
    }
    clearInterval(this.tmr);
    this.bExec = true;
    this.tmr = setInterval('WaitToRun.doExecute()', this.iMil);
  },
  doExecute: function(){
    if(!this.bExec){
      clearInterval(this.tmr);
      return;
    }
    this.bExec = false;
    if (!this.oMethod){
      WaitToRun.log('WaitToRun - Invalid Method! Use setMethod to set.', true);
      return;
    }
    this.iCount += 1;
    this.oMethod();
  },
  log: function(pMsg, pErro){
    if((this.bLog) || (pErro)){
      var divId = 'logWaitToRun';
      var div = document.getElementById(divId);
      if (!div){
        div = document.createElement('DIV');
        with(div){
          id = divId;
          style.border = '1px dashed #ff0000';
          style.color = '#ff0000';
        }
        document.body.appendChild(div);
      }
      div.innerHTML += '#'+ this.iCount + ((pMsg!='')?' - ':'') + pMsg + '<br/>';
    }
  }
}
