  ///
  /// NetForce Tecnologia
  /// Controle de Timer
  ///

  var _timer = new NTimer();

  /**
   * NTimer
   * Controle de Timer e gatilhos
   */
  function NTimer()
  {
    this.data     = null;
    this.markLast = null;
    this.sleep    = 1000;
    this.funcs    = new Array();

    /**
     * Iniciar Timer
     */
    this.init = function(strDataServer) {

      // Carregar hora do servidor
      this.data     = strDataServer.toDate();
      this.markLast = new Date();

      // Show Timer
      var strAgora = this.data.toStrDMYHNS();
      $('#divTimer').text(strAgora);

      // Agendar próximo
      window.setTimeout('_timer.ping();', this.sleep, 'JavaScript');
    };

    /**
     * Ping do Timer
     */
    this.ping = function() {
      // Somar no contador 
      var agora = new Date();
      var diff = this.markLast.getDiff(agora);
      this.data.addMSeconds(diff);

      // Show Timer
      var strAgora = this.data.toStrDMYHNS();
      $('#divTimer').text(strAgora);

      // Contadores
      this.doContadores();

      // Executar gatilhos
      for (var i = 0;i < this.funcs.count(); i++)
      {
        if (this.funcs[i].data == strAgora)
          window.setTimeout(this.funcs[i].exec, 500, 'JavaScript');      
      }

      // Agendar próximo
      window.setTimeout('_timer.ping();', this.sleep, 'JavaScript');
      this.markLast = new Date();
    };

    /**
     * Ping do Timer
     */
    this.addFunc = function(strData, func) {
      var item = {data: strData, exec: func};
      this.funcs.add(item);
    };

    /**
     * Contadores
     */
    this.doContadores = function() {
      var dt = this.data;
      $('[timer]').each(function() {
        var obj = $(this);

        if (obj.attr('timer') != 'ACABOU')
        {
          var odt = obj.attr('timer').trim().toDate();
          var dif = dt.getDiff(odt);

          if (dif > 0)
          {
            var h = (dif / 3600000).intVal().toString(10).fillChars(2);
            dif  -= (h * 3600000);
            var m = (dif / 60000).intVal().toString(10).fillChars(2);
            var baixou = ((m < 1) && (h == 0));
            dif  -= (m * 60000);
            var s = (dif / 1000).intVal().toString(10).fillChars(2);

            var res = h + ':' + m + ':' + s;
          } else {
            if (obj.attr('reload') != 'false')
            {
              var res = obj.attr('ok');
              document.location.reload();
            }
          }

          obj.html(res);
          if ((baixou) && (obj.css('color') != 'red'))
            obj.css('color', 'red');
          else
            obj.css('color', '');
        }
      });
    };
  }
