﻿// Register the AjaxTutorial namespace
Type.registerNamespace("AjaxTutorial");

// Timer constructor
AjaxTutorial.Timer = function() 
{
  // initialize base class
  AjaxTutorial.Timer.initializeBase(this);  

  // setup class members
  this._interval = 1000;
  this._enabled = false;
  this._timer = null;
}

// Timer members
AjaxTutorial.Timer.prototype = 
{
  // initialize the timer
  initialize: function() 
  {
    // initialize base class
    AjaxTutorial.Timer.callBaseMethod(this, 'initialize');

    // start the timer if it's enabled
    if(this._enabled) this._startTimer();    
  }, 
  
  // dispose of the timer  
  dispose: function() 
  { 
    // make sure the timer is stopped
    this._stopTimer();            

    // be sure to call base.dispose()
    AjaxTutorial.Timer.callBaseMethod(this, 'dispose');
  },
  
  // interval getter
  get_interval: function() 
  {    
    return this._interval;
  },  

  // interval setter
  set_interval: function(value) 
  {
    if (this._interval !== value) {
      this._interval = value;
      this.raisePropertyChanged('interval');
      // do not restart the timer during the creation phase
      if (!this.get_isUpdating() && this._timer !== null) 
        this._restartTimer();      
    }
  },

  // enabled getter
  get_enabled: function() 
  {    
    return this._enabled;
  },

  // enabled setter
  set_enabled: function(value) 
  {
    if (value !== this._enabled) {
      this._enabled = value;
      this.raisePropertyChanged('enabled');
      // do not enable the timer during the creation phase
      if (!this.get_isUpdating())
        if(this._enabled) 
          this._startTimer();        
        else 
          this._stopTimer();            
    }
  },
  
  // tick event
  add_tick: function(handler) 
  {    
    this.get_events().addHandler("tick", handler);
  },

  remove_tick: function(handler) 
  {
    this.get_events().removeHandler("tick", handler);
  },
  
  // callback function for the tick event
  _onTick: function() 
  {
    this._raiseAjaxEvent("tick");
  },
  
  // start or restart the timer
  restartTimer: function() 
  {
    this._stopTimer();
    this._startTimer();
  },
  
  // start the timer
  _startTimer: function() 
  {
    // save timer cookie 
    this._timer = window.setInterval(Function.createDelegate(this, this._onTick), this._interval);
  },
  
  // stop the timer
  _stopTimer: function() 
  {
    // prevent multiple calls
    if(this._timer) {
      window.clearInterval(this._timer);
      this._timer = null;
    }
  },
    
  // raise an AJAX event
  _raiseAjaxEvent: function(eventName, eventArgs) 
  {
    // obtain the event handler for the specified event name
    var handler = this.get_events().getHandler(eventName);

    // continue only if there is at least one handler for the event
    if (handler) {
      // if no event args have been supplied, create empty EventArgs
      if (!eventArgs) eventArgs = Sys.EventArgs.Empty;
      // call the event handlers
      handler(this, eventArgs);
    }
  }
}

AjaxTutorial.Timer.registerClass('AjaxTutorial.Timer', Sys.Component);
