if(!Widget) var Widget = {};

Widget.Timer = function (o) {this._init(o)}

Widget.Timer.VERSION   = '0.40';
Widget.Timer.EXPORT_OK = ['timer'];

Widget.Timer.COUNTER   = 0;
Widget.Timer.FINISHED  = '--:--';

Widget.Timer.timer = function (o) {
   var id   = 'WidgetTimer' + Widget.Timer.COUNTER++;
   var CODE = id + "= new Widget.Timer (o);"
            + id + ".setid('" + id + "');"
            + id + ".tick();";
   eval(CODE);
}

Widget.Timer.prototype.setid = function (id) {
   if(id) this.ID = id;
}

Widget.Timer.prototype._init = function (o) {
   if(!o || typeof o != 'object') o = {};
   this.MINUTE   = o['minute'  ] || o['minutes'] || o['min'] || 0;
   this.SECOND   = o['second'  ] || o['seconds'] || o['sec'] || 0;
   this.HOUR     = o['hour'    ] || o['hours'  ] || o['hr' ] || 0;
   this.TARGET   = o['target'  ] || 'admin_timeout'; // span id
   this.BLINK    = o['blink'   ] || '';
   this.CALLBACK = o['callback'] || '';

   if(this.MINUTE > 60) {
      var hr_tmp = new Number(this.MINUTE/60).toFixed(6);
      var ar = hr_tmp.split('.');
      if(ar.length <= 1) {
         this.HOUR   = ar[0];
         this.MINUTE = 0;
      }
      else {
         this.HOUR   = ar[0];
         this.MINUTE = new Number(('0.' + ar[1] || 0) *60).toFixed(0);
      }
   }
   this.SECOND = (this.SECOND <= 0) ? 1 : this.SECOND+1;
   this.ID     = '';
}

Widget.Timer.prototype.callback = function () {
   if(this.CALLBACK) {
      if(typeof this.CALLBACK == 'function') {
         this.CALLBACK(this);
      }
      else {
         eval(this.CALLBACK);
      }
   }
}

Widget.Timer.prototype.tick = function () {
   var timeout;
   this.SECOND--;
   if (this.MINUTE == -1) {              this.MINUTE = 60;                   }
   if (this.MINUTE == 60) { this.HOUR--; this.MINUTE = 60;                   }
   if (this.SECOND ==  0) {              this.MINUTE--;    this.SECOND = 60; }

   var blink = ':';
   if(this.BLINK) {
	  blink = this.SECOND % 2
	        ? '<span id="' + this.BLINK[0] + '">:</span>'
	        : '<span id="' + this.BLINK[1] + '">:</span>'
	        ;
   }

   var finished = 0;
   if(this.MINUTE < 0 && this.HOUR <= 0) {
      timeout = Widget.Timer.FINISHED;
      finished++;
   }
   else {
      if(this.MINUTE == 60) this.MINUTE--;
      var xhr  = this.HOUR;
      var xmin = this.SECOND == 60 ? this.MINUTE+1 : this.MINUTE;
      var xsec = this.SECOND == 60 ? '00'          : this.SECOND;
      if(xmin < 10                ) xmin = '0' + xmin;
      if(xsec < 10 && xsec != '00') xsec = '0' + xsec;
      timeout = xmin + blink + xsec;
      if (xhr > 0) {
         timeout = (xhr < 10 ? '0' : '') + xhr + blink + timeout;
      }
   }

   this.display(timeout);

   if(this.MINUTE >= 0 || this.HOUR > 0) {
      window.setTimeout(this.ID+".tick()", 1000);
   }
   if(finished) this.callback();
}

Widget.Timer.prototype.display = function (data) {
   if (!data) data = '';
   // rewrite !!!
   if (document.getElementById) {
      document.getElementById(this.TARGET).innerHTML = data;
   }
   else if (document.layers) {
	  var layer = document.layers[this.TARGET];
      layer.document.write(data);
      layer.document.close();
   //}
   //else if (document.all) {
   //   admin_timeout.innerHTML = data;
   }
   else {
      alert("I can not locate the timer container");
      return;
   }
}

/*

*/

