﻿
Type.registerNamespace('Core.Components');


// Class ctor
// This code gets called when you instantiate this class
Core.Components.Timer = function Core$Components$Timer(expires, userCallback) 
{
    // Calls the base ctor, if any
    Core.Components.Timer.initializeBase(this);
    
    // Initializes the private members
    this._expires = expires;
    this._raiseTickDelegate = Function.createDelegate(this, this._raiseTick);
    this._userCallback = userCallback;
    this._timer = null;
}

// PROPERTY:: expires: int (milliseconds)
function Core$Components$Timer$get_expires() { 
    if (arguments.length !== 0) throw Error.parameterCount();
    return this._expires;
}
function Core$Components$Timer$set_expires(value) {
    var e = Function._validateParams(arguments, [{name: 'value', type: Int}]);
    if (e) throw e;

    this._expires = value;
}

// PROPERTY:: isActive: bool 
function Core$Components$Timer$get_isActive() { 
    if (arguments.length !== 0) throw Error.parameterCount();
    return (this._timer !== null);
}



// METHOD:: stop()
function Core$Components$Timer$stop(){
   this._stopTimer();
}

// METHOD:: start()
function Core$Components$Timer$start(){
    this._startTimer();
}


///////                     ///////
///////  PRIVATE members    ///////
///////                     ///////


function Core$Components$Timer$_startTimer() {
    this._timer = window.setTimeout(this._raiseTickDelegate, this._expires);
}

function Core$Components$Timer$_stopTimer() {
    if (this._timer !== null) {
 	    window.clearTimeout(this._timer);
	    this._timer = null;
   } 	
}

function Core$Components$Timer$_raiseTick() {
    if (this._userCallback !== null)
        this._userCallback(this);
        
    this._startTimer();
}

Core.Components.Timer.prototype = 
{
    get_isActive:  Core$Components$Timer$get_isActive,
    get_expires:   Core$Components$Timer$get_expires,
    set_expires:   Core$Components$Timer$set_expires,
    stop:          Core$Components$Timer$stop,
    start:         Core$Components$Timer$start,
    _raiseTick:    Core$Components$Timer$_raiseTick,
    _startTimer:   Core$Components$Timer$_startTimer,
    _stopTimer:    Core$Components$Timer$_stopTimer
}

Core.Components.Timer.registerClass('Core.Components.Timer');

// Required if loading the script through the script manager
//Sys.Application.notifyScriptLoaded();


