﻿Type.registerNamespace('AdultShop.Web.Controls');

AdultShop.Web.Controls.Timer = function() {
    AdultShop.Web.Controls.Timer.initializeBase(this);
    
    this._interval = 1000;
    this._enabled = false;
    this._timer = null;
}

AdultShop.Web.Controls.Timer.prototype = {
    dispose: function() {
        this.set_enabled(false);
        AdultShop.Web.Controls.Timer.callBaseMethod(this, 'dispose');
    },
    
    updated: function() {
        AdultShop.Web.Controls.Timer.callBaseMethod(this, 'updated');

        if (this._enabled) {
            this._restartTimer();
        }
    },
    
    get_interval: function() {
        return this._interval;
    },
    
    set_interval: function(value) {
        if (this._interval != value) {
            this._interval = value;
            this.raisePropertyChanged('interval');
            
            if (!this.get_isUpdating() && (this._timer != null)) {
                this._restartTimer();
            }
        }
    },
    
    get_enabled: function() {
        return this._enabled;
    },
    
    set_enabled: function(value) {
        if (this._enabled != value) {
            this._enabled = value;
            this.raisePropertyChanged('enabled');
            
            if (!this.get_isUpdating()) {
                if (value) {
                    this._startTimer();
                }
                else {
                    this._stopTimer();
                }
            }
        }
    },

    add_tick: function(handler) {
        this.get_events().addHandler("tick", handler);
    },
    
    remove_tick: function(handler) {
        this.get_events().removeHandler("tick", handler);
    },

    _restartTimer: function() {
        this._stopTimer();
        this._startTimer();
    },

    _startTimer: function() {
        this._timer = window.setInterval(Function.createDelegate(this, this._timerCallback), this._interval);
    },
    
    _stopTimer: function() {
        window.clearInterval(this._timer);
        this._timer = null;
    },
    
    _timerCallback: function() {
        var handler = this.get_events().getHandler("tick");
        
        if (handler) {
            handler(this, Sys.EventArgs.Empty);
        }
    }
}

AdultShop.Web.Controls.Timer.descriptor = {
    properties: [   {name: 'interval', type: Number},
                    {name: 'enabled', type: Boolean} ],
    events: [ {name: 'tick'} ]
}

AdultShop.Web.Controls.Timer.registerClass('AdultShop.Web.Controls.Timer', Sys.Component);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();