﻿Type.registerNamespace('ActiveBets');

ActiveBets.ClockExtender = function(element) {
    ActiveBets.ClockExtender.initializeBase(this, [element]);

    this._str_server_time = null;
    this._time_offset = null;

    this._server_time = null;

    //#region [ Timer ]	
    this._timer = null;
    this._tickHandler = null;
}

ActiveBets.ClockExtender.prototype = {
    initialize: function() {
        ActiveBets.ClockExtender.callBaseMethod(this, 'initialize');

        this._server_time = new Date(this._str_server_time);
        this._onTimerTick();

        this._tickHandler = Function.createDelegate(this, this._onTimerTick);
        this._timer = new Sys.Timer();
        this.initializeTimer();
        this._server_time.setSeconds(new Date().getSeconds());
    },

    dispose: function() {
        if (this._timer) {
            this._timer.dispose();
            this._timer = null;
        }

        this._tickHandler = null;
        ActiveBets.ClockExtender.callBaseMethod(this, 'dispose');
    },

    initializeTimer: function() {
        this._timer.set_interval(1000);
        this._timer.add_tick(this._tickHandler);
        this._timer.set_enabled(true);
    },

    _onTimerTick: function(sender, eventArgs) {
        this._server_time.setSeconds(this._server_time.getSeconds() + 1);
        this.get_element().innerHTML = String.localeFormat("<b>{0:d}</b> {0:HH:mm:ss}", this._server_time);
    }
}
ActiveBets.ClockExtender.registerClass("ActiveBets.ClockExtender", Sys.UI.Behavior);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();