// Gaia Ajax Widgets Copyright (C) 2007  Frost Innovation AS. details at http://ajaxwidgets.com/
/* 
 * Gaia Ajax Widgets, an Ajax Widget Library
 * Copyright (C) 2007  Frost Innovation AS
 * All rights reserved.
 * This program is distributed under either GPL version 2 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Frost Innovation AS
 * read the details at http://ajaxwidgets.com/
 */




/* ---------------------------------------------------------------------------------------------------------------
   A class for periodically raising an event on your page from which you can trap and manipulate the page from
   --------------------------------------------------------------------------------------------------------------- */
Gaia.Timer = function(element, options){
  this.initialize(element, options);
}

// Inheriting from WebControl
Object.extend(Gaia.Timer.prototype, Gaia.Control.prototype);

// Adding custom parts
Object.extend(Gaia.Timer.prototype, {
  // "Constructor"
  initialize: function(element, options){
    // Calling base class constructor
    this.baseInitializeControl(element, options);
    Gaia.Control._itsRegisteredInvisibleControls.push(this);

    // We MUST have an ID for our control to be retrievable...
    // Faking one here!
    this.element = {id:element};

    // Checking to see if we should start it IMMEDIATELY
    if( this.options.enabled )
      this._startTimer();
  },

  setEnabled: function(value){
    var shouldStart = value == true && this.options.enabled != true;
    this.options.enabled = value;
    if( shouldStart )
      this._startTimer();
    return this;
  },

  setVisible: function(value){
    return this;
  },

  destroy: function(){
    this.setEnabled(false);
    this._destroyImpl();
  },

  setMilliseconds: function(value){
    this.options.milliseconds = value;
    return this;
  },

  _startTimer: function(){
    setTimeout(this._tick.bind(this), this.options.milliseconds);
  },

  _tick: function(){
    if( this.options.enabled ){
      Gaia.Control.callControlMethod.bind(this)('TickMethod', null, function(){
        if( this.options.enabled == true )
          this._startTimer();
      }.bind(this));
    }
  },

  _getElementPostValue: function(){
    return '';
  }
});

Gaia.Timer.browserFinishedLoading = true;
